A computer network is a group of linked computers that use a set of common communication protocols to enable the sharing of data.
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.
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 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.
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.
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.
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.
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.
The Media Access Control (MAC) Address is the hardware address of the Network Interface Card (NIC). It uniquely identifies it.
The global computer network system.
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.
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.
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.
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.
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.
Address Resolution Protocol (ARP) is a protocol that hides the MAC Address associated with an IP Address.
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.
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.
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.
Provides host-to-host communication from one application to another. An application within a host is addressed by a port number.
A port is a communication endpoint. It identifies a specific process to which a network message is to be forwarded to.
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.
User Datagram Protocol (UDP) provides a connectionless delivery service using IP to transport messages between machines. It is a best-effort, unreliable protocol.
Transmission Control Protocol (TCP) provides a reliable, full duplex connection between two machines, allowing them to exchange data efficiently.
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:
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.
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.
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.
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)
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 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>
The Java platform has a java.net package that provides the necessary classes from implementing networking applications.
These classes include:
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.
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; 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; DatagramPacket receivedPacket = new DatagramPacket(recvBuffer, recvBuffer.length); socket.receive(receivedPacket); // blocking method! // CLOSE THE SOCKET socket.close();
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.
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
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
The Single Client Chat