Title Text
Title Text
Title Text
Title Text
Introduction to programming
Network programming
COMPUTER NETWORKS
COMPUTER NETWORKS
A computer network is a group of linked computers that use a set of common communication protocols to enable the sharing of data.
LAN vs. MAN vs. WAN
THE TCP/IP MODEL
TCP-IP Model
Network communication involves a set of protocols that allow application programs to talk with each other with no regard to the hardware and OS where they are run.
This set of protocols is represented as the TCP-IP model.
NETWORK INTERFACE LAYER
Network InterFace Layer
Takes care of hardware addressing; the protocols present in this layer allow for the physical transmission of data.
A network interface is the point of interconnection between a computer and a network. It is usually a card (NIC), but it doesn't have to have a physical form; instead it can be implemented in software.
ETHERNET
Ethernet has been around since the early 70s, and is the most popular protocol in the Network Interface Layer.
It is the foundation on which many LANs are built today.
It is a wired protocol.
Network topologies
Computer networks are installed with different arrangements.
The way of network instalment affects the communication between the devices attached to that network.
One of the simplest types of topologies is the bus topology.
Bus topology
A common communication medium to which all the network nodes are connected. This medium is known as a bus, and has terminators at the ends to absorb the signals.
- A computer connected to the bus broadcasts what it wants to send
- The message contains the destination address
- Each network device checks the address
- The destination device processes the data
- All the others ignore the message
- If a collision occurs, it is handled by CSMA/CD
CSMA/CD
the hub
In this type of network topology, the whole communication process is handled by a central device named hub.
The hub connects all the devices in a network system.
- A computer sends a message to a destination address.
- The hub receives the message and broadcasts it to the other machines connected to the network
the SWITCH
The switch, similarly to the hub, also handles the whole communication process.
However, contrary to the previous device, the switch is capable of using an address table so it can send the intended message to the destination machine alone.
Additionally, the switch allows for full-duplex communication.
MAC ADRESS
The Media Access Control (MAC) Address is the hardware address of the Network Interface Card (NIC). It uniquely identifies it.
ETHERNET FRAME
INTERNET LAYER
What is the internet?
The global computer network system.
Routers
We've covered the basics of how computers connect to each other on local networks, but how do they connect to the Internet?
A router is a piece of network hardware that connects a local network to the Internet. It's basically a computer that forwards packets between different networks.
internet protocol
A set of rules for routing and addressing packets of data so that they can travel across the network and arrive at the intended destination.
IPv4 is the dominant Internet Protocol. IPv6 is its successor.
IP ADRESSES
A unique 32-bit identifier assigned to a device or domain that connects to the Internet.
The IP address does not identify a machine; it identifies the network and a host connected to that network. In other works, an IP address distinguishes a connection on a network. That's why, if a host moves from one network to another, its IP address will change.
IPv4 vs. ipv6
NETWORK MASKS
The amount of bytes that are used to encode the network and the host are not fixed. In order to determine that we need one additional information: a subnet mask.
The first address of a subnetwork is reserved for addressing the network itself. The last one is reserved for broadcasting to all hosts on the network.
RESERVED ADDRESSES
LOOPBACK ADDRESS - All addresses starting with 127 are reserved for testing and interprocess communication on the local machine. When a program uses the loopback address as a destination, the OS returns the data back without sending any traffic across the network.
ARP
Address Resolution Protocol (ARP) is a protocol that hides the MAC Address associated with an IP Address.
IP ROUTING
Routing is the mechanism that determines the path that data follows in order to travel across multiple networks, from its source to its destination.
Routers refer to internal routing tables to make decisions about how to route packets along network paths.
Routing tables
IP DATAGRAM
Datagrams bigger than MTU will be fragmented.
They may be lost, duplicated, delayed, or delivered out of order, and need to be reassembled at the destination.
- Unreliable
- Best-Effort
- Connectionless
ICMP
Internet Control Message Protocol (ICMP) is used by network devices to send error messages indicating, for example, that a requested service is not available or that a host or router could not be reached.
TRANSPORT LAYER
Transport Layer
Provides host-to-host communication from one application to another. An application within a host is addressed by a port number.
POrts
A port is a communication endpoint. It identifies a specific process to which a network message is to be forwarded to.
Well-Known ports
Lower port numbers are reserved for common applications. These are called well-known ports.
The use of well-known ports allows client applications to easily locate the corresponding server application process in another host.
UDP
User Datagram Protocol (UDP) provides a connectionless delivery service using IP to transport messages between machines. It is a best-effort, unreliable protocol.
TCP
Transmission Control Protocol (TCP) provides a reliable, full duplex connection between two machines, allowing them to exchange data efficiently.
Three Way handshake
SOCKET API
Software abstraction used to represent the "terminals" of a connection between two machines.
It is an IPC mechanism provided by the OS.
A socket is characterised by:
- Transport Protocol
- Local IP address and port number
- Remote IP address and port number
APPLICATION LAYER
DHCP
Dynamic Host Configuration Protocol (DHCP) allows for automatic configuration of network information in the devices that use it.
This information includes the IP address, the subnet mask and even the gateway. Asking it from DHCP server eliminates the need for manual configuration.
DNS
Domain Name System (DNS) is an hierarchical distributed naming system.
Each device connected to the Internet has its own IP address. DNS servers eliminate the need for humans to memorize complex numbers by converting the human-friendly hostname into a machine-friendly IP-address.
HTTP
Hypertext Transfer Protocol (HTTP) allows the fetching of resources on the Web, such as HTML documents, images, etc.
We will look into it in further detail in one of the next sections.
OTHER PROTOCOLS
FTP - File Transfer Protocol (used for the transfer of computer files from a server)
SMTP - Simple Mail Transfer Protocol (a standard communication protocol for electronic mail transmission)
CLient-Server MODEL
Client-server Model
Computer network architecture in which a server (host computer) waits for requests to arrive from clients, and then responds to them.
Note that, although the representation above is the most common, server and client can be two applications being run on the same machine.
NETCAT
Netcat is a computer networking utility.
It allows for port scanning and port listening.
// print a list of all of the available commands you can use in Netcat
nc -help
// begin listening for TCP connections and UDP activity on a specific port number
nc -l <port_number>
// establish a TCP connection to the specified host and port
nc <host> <port_number>
// establish a UDP "connection" to the specified host and port
nc -u <host> <port_number>
// port scanning
nc -z -v <host> <range>
Theory WRAP UP
networking in java
java.net
The Java platform has a java.net package that provides the necessary classes from implementing networking applications.
These classes include:
- InetAddress
- DatagramPacket
- DatagramSocket
- ServerSocket
- Socket
- ...
Sockets
Different processes can communicate with each other across the network via sockets.
Java implements both TCP and UDP sockets, allowing the programmer to work with a network connection as if it were just another stream.
udp sockets
DatagramSocket implements UDP sockets, capable of sending or receiving datagram packets. Each packet is individually addressed and routed, which means multiple packets sent to the same machine may be routed differently and arrive in any order.
// OPEN AN UDP SOCKET
DatagramSocket socket = new DatagramSocket(portNumber);
// CREATE A DATAGRAM PACKET AND SEND IT FROM THE SOCKET
byte[] sendBuffer = new byte[1024];
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, InetAddress.getByName(hostName), portNumber);
socket.send(sendPacket);
// CREATE A DATAGRAM PACKET AND RECEIVE DATA FROM THE THE SOCKET
byte[] recvBuffer = new byte[1024];
DatagramPacket receivedPacket = new DatagramPacket(recvBuffer, recvBuffer.length);
socket.receive(receivedPacket); // blocking method!
// CLOSE THE SOCKET
socket.close();
Exercise
The Inspiring Quote Server
Server should be listening to client requests.
If the request is "hit me", server should respond with an inspirational message.
Otherwise, server should send a "unsupported operation" message.
tcp sockets - client
Socket implements TCP client sockets. These sockets are capable of connecting to a specific server and port.
// OPEN A CLIENT SOCKET
Socket clientSocket = new Socket(hostName, portNumber); // blocking method!
// SETUP INPUT AND OUTPUT STREAMS
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// READ FROM / WRITE TO STREAMS
// CLOSE THE STREAMS
// CLOSE THE SOCKETS
tcp sockets - server
ServerSocket implements server sockets. These sockets wait for requests to come in over the network. It performs some operation based on the request and, possibly, returns a result to the requester.
// BIND TO LOCAL PORT AND WAIT FOR CLIENT CONNECTIONS
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept(); // blocking method!
// SETUP INPUT AND OUTPUT STREAMS
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// READ FROM / WRITE TO STREAMS
// CLOSE THE STREAMS
// CLOSE THE SOCKETS
Exercise
The Single Client Chat
Network Programming
By Soraia Veríssimo
Network Programming
- 1,808