2 Introduction to Networking and Socket API

Pravin Jain

epgp books
Networking in Java

What  is  networking?

 

Connecting more than one machines. Machines are hardware. The purpose of networking is to have communication  between  two  processes  which  may  be on different  machines.

 

What is required for Networking?

 

Hardware  like  connectors,  cables,  hub,  NIC  etc. – Physical  Layer

 

What is the role of NIC?

  1. It converts signals to digital data and  data  to  signals  which  is  transmitted  on the cable or any other media like wireless, etc.
  2. The code for converting data to signals and vice versa is in the ROM of NIC – Firmware – DataLink Layer Each NIC has a 48-bit MAC-address by which it is identified. This MAC address is divided into 2 parts. Initial 24-bits identify the Manufacturer and the rest 24- bits are the serial number assigned by the manufacturer. From user’s perspective, the user does not identify the NIC card. The user will identify  the host  Machines.
  3. Some way of identifying hosts which form a network – Network Layer This would be managed by the OS. Various options/protocols are available for Network layer. The most common being IP protocol.

 

There are various version of the IP protocol. The commonly used versions are IPv4 and IPv6. In the IPv4 protocol, each machine is identified by a 32-bit (128-bit in case of IPv6) value assigned to each node participating in the network. This 32-bit value is known as the IP-address of the machine. It is denoted in decimal-dotted notation by writing the values of each byte separated by dot(‘.’) e.g. 192.168.88.1. The 32-bits of an IP-address are divided into 2 parts, Some initial most-significant bits are used to identify the network and rest of the bits are used to identify the host within the network. In the host bits there are two reserved values. All zeros for host is reserved for specifying the network address and all ones is reserved for the broadcast address. The number of bits used for the network is specified by using a subnet-mask. E.g. if in the above IP-address we want to specify 10-bits for network, then the subnet-mask would be 255.192.0.0. The network address for above IP-address would be 192.128.0.0. The broadcast address will be 192.191.255.255.

The purpose of networking is to have communication between 2 processes.

  1. Some means of exchanging bytes between processes on different machines –  Transport  Layer

 

Here there are 2 commonly used protocols: TCP(Transmission Control Protocol) and UDP(User Datagram Protocol). TCP is a stream based protocol whereas UDP is a packet based protocol. In case of UDP the sender prepares a packet containing bytes for the receiver. In case of TCP there is a stream available on which bytes can be written or read from. For a process to start using any of the above protocols, it has to obtain port number (which is a resource with the OS). The port number is a 16-bit value. A valid port number is in the range of 1 to 65535.

  1. The transport layer provides a means for sending and receiving bytes between 2 processes, but that is not sufficient for communication between processes. The communication between 2 processes would require that the bytes of data exchanged between 2 processes can have a meaning for the 2 processes. The bytes of data are according to some format and convey some meaningful data which may be interpreted and used by the receiver. There has to be a pre-decided format according to which these bytes would be exchanged. The layer which defines some kinds of rules and format for the bytes being exchanged between 2 processes – Application Layer

There are many protocols at the application layer which use TCP or UDP at the transport layer. The most common protocols are the HTTP, SMTP, POP3, FTP, DNS, tFTP etc.

 

DNS and tFTP protocols use UDP for  communication  between  the  client  and the server, whereas all the other protocols mentioned use TCP.

 

The HTTP protocol is the most commonly used protocol for communication between the browser and the server. The default port number used by  the HTTP protocol is 80.

 

Protocol Stack

 

JAVA API for networking

Inet Address class.

 

In Java, the classes related to networking are in the java.net package. The class InetAddress is used for representing the Host in any IP based protocol. An instance of InetAddress encapsulates information about an IP address and its  host  name. No public constructor to create any instance of this class are available. Instead, we use the public and static factory methods to get instances of this class.

 

Methods

  1. public static InetAddress getByName(String name)
  2. public static InetAddress getByAddress(byte[] address)
  3. public static InetAddress getByAddress(String host, byte[] address)
  4. public static InetAddress getLocalHost()
  5. public static InetAddress[] getAllByName()
  6. public byte[] getAddress()
  7. public String getHostAddress()
  8. public String getCanonicalHostName()
  9. public String getHostName()

 

The getByName() method may be used to resolve an IP address from the name, e.g. we may use the method as given below to resolve the IP address of any

 

host:InetAddress add = InetAddress.getAddressByName(“mail.yahoo.com”);

System.out.println(Arrays.toString(add.getAddress()));

 

The getByAddress() method may be used to resolve the name of the host by using its IP address, e.g. we may use the method as given below to resolve the name by using its IP address:

 

byte[] ipaddress = byte[]202,164,27,54;

InetAddress add = InetAddress.getByAddress(ipaddress);

 

The getByAddress() with two parameters creates an instance of the InetAddress with the specified host name and IP address. This method does not carry out any check with the name server and does not attempt any kind of name or address resolution. The getLocalHost() creates an instance of the InetAddress class corresponding to the local host. The last static method getAllbyName() returns an array containing the InetAddress instances corresponding to all the IP addresses configured on the specified host name. An instance of InetAddress encapsulates the values of the IP address and the name of the host. The methods getAddress() and getHostName() return the encapsulated values of the IP address and the host name. The method getCanonicalHostName() returns the full domain name of the host whose IP address it represents. The getHostAddress() returns in String from the IP address value. For IPv4 this would be the four byte values in decimal format separated by dots.

 

UsingSocketandServerSocketforClient–ServerCommunicationSequence of steps:

 

In Java, The Socket and the ServerSocket classes are used to have a TCP/IP communication between a client and a Server. The following are the steps required to establishing the connection between the  client  and  a  server  using the  TCP/IP  protocol.

 

Server-Side

 

The steps on the server side are as follows:

  1. The Server process reserves a port for itself. This is done by calling the constructor of ServerSocket. e.g. ServerSocket ss = new ServerSocket(portno);
  2. The Server starts listening for connection from client. This is done by calling the accept() method on the ServerSocket. accept() is a blocking method, it would wait till a client connects. As soon as the client connects the accept() method returns an instance of Socket, which is a connection to the client who  has connected.

 

e.g.   Socket   socket   = ss.accept() // the server is waiting

 

Client-side

 

The Steps on the client side is as follows:

  1. The client connects   to  the  server  by  calling   the  constructor of  Socket class, where he specifies the host and the portno of the server.

 

e.g. Socket socket = new Socket(ipaddress, portno)

 

This would create a Socket on the client side, as well as on the server-side the accept method returns the socket. So two connected sockets are created simultaneously, one on the client side by using a constructor, and other on the server side with the accept method.

 

The connected socket has an InputStream and OutputStream, is used for communicating with the remote  process.

 

Diagram

 

Methods of the Socket class

  1. public InputStream getInputStream()
  2. public OutputStream getOutputStream()
  3. public InetAddress getHost()
  4. public int getPort()
  5. public InetAddress getLocalHost()
  6. public int getLocalPort()

 

The getInputStream() and the getOutputStream() methods are used to obtain the InputStream and the OutputStream of the Socket instance, which are connected to the remote Socket. These are used for sending to and receiving bytes from the connected Socket.

you can view video on Introduction to Networking and Socket API

Suggested Reading:

  1. Core Java Volume 2 by Cay Horstmann & Gary Cornell, Ninth Edition, Pearson Education.
  2. The class of JAVA by Pravin Jain, Pearson Education.
  3. Beginning Java Networking by Alexander V Konstantinou and others, Wrox publication.
  4. https://docs.oracle.com/javase/tutorial/networking/overview/index.html