2 Introduction to Networking and Socket API
Pravin Jain
The purpose of networking is to have communication between 2 processes.
- 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.
- 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
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
- public static InetAddress getByName(String name)
- public static InetAddress getByAddress(byte[] address)
- public static InetAddress getByAddress(String host, byte[] address)
- public static InetAddress getLocalHost()
- public static InetAddress[] getAllByName()
- public byte[] getAddress()
- public String getHostAddress()
- public String getCanonicalHostName()
- 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:
- The Server process reserves a port for itself. This is done by calling the constructor of ServerSocket. e.g. ServerSocket ss = new ServerSocket(portno);
- 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:
- 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
- public InputStream getInputStream()
- public OutputStream getOutputStream()
- public InetAddress getHost()
- public int getPort()
- public InetAddress getLocalHost()
- 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:
- Core Java Volume 2 by Cay Horstmann & Gary Cornell, Ninth Edition, Pearson Education.
- The class of JAVA by Pravin Jain, Pearson Education.
- Beginning Java Networking by Alexander V Konstantinou and others, Wrox publication.
- https://docs.oracle.com/javase/tutorial/networking/overview/index.html