Java Socket Programming (part 1)

Socket Communication

We plan to learn the following from these Blog:

  • What is a socket?
  • The client-server model
  • java.net package
  • illustration of InetAddress Class

Socket

Socket is a combination of IP Address and Port Number.

IP Address is a unique address of a host machine by which it can be uniquely identified in the INTERNET or INTRANET.

Port Number belongs to particular service or protocol. Every service or protocol is mapped with a port in in server. Also for communication Client and Server uses one port number.

IANA ( The Internet Assigned Numbers Authority) has standards for three type of port number.
Well known port numbers ( Numbers 0 to 1023) These are reserved for Service and applications.
Registered ports (Numbers 1024 to 4951) These port numbers are assigned to user processes and or applications
Dynamic or Private Ports ( Numbers 49152 to 65535) These are usually assigned dynamically to client applications when initiating a connection.

The client-server model

The client-server model is one of the most used communication paradigms in networked systems. Clients normally communicates with one server at a time. From a server’s perspective, at any point in time, it is not unusual for a server to be communicating with multiple clients. Client need to know of the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client prior to the connection being established.

Client and server on the same Ethernet communicating using TCP/IP.

ethernet
Figure. 1 Client and Server on the same Ethernet using TCP/IP

The client and the server may be in different LANs, with both LANs connected to a Wide Area Network (WAN) by means of routers. The largest WAN is the Internet, but companies may have their own WANs. This scenario is depicted in Figure 2.

wan
Figure 2. Client and Server on different LANs connected through WAN/Internet

java.net package 

Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server.

The java.net.Socket class represents a socket, and the java.net.ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them.

The following steps occur when establishing a TCP connection between two computers using sockets:

  • The server instantiates a ServerSocket object, denoting which port number communication is to occur on.
  • The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port.
  • After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to.
  • The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket object capable of communicating with the server.
  • On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client’s socket.

After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client’s OutputStream is connected to the server’s InputStream, and the client’s InputStream is connected to the server’s OutputStream.

InetAddress Class: –

This class represents an Internet Protocol (IP) address.

InetAddress class does not have any visible constructor because it is a factory class. Using static methods to return a factory instance makes sense when there are a small number of situations in which you will want to create such an object.

You can create an object with one of method:

getLocalHost(), getting your own address;

getByName(), getting the “primary” address for a site; or

getAllByName(), getting a list of the addresses.

You will get a brief description of all methods of InetAddress Class here:-

https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html

Sample Program to illustrate Use of InetAddress Class: –

import java.io.*;
import java.net.*;
public class InetDemo{
public static void main(String[] args){
try{
//Getting Ip address of http://www.mnk.org.in
InetAddress ip=InetAddress.getByName(“www.mnk.org.in”); //getByName returns InetAddress object of website
System.out.println(“Host Name: “+ip.getHostName());          //getHostName for getting host name
System.out.println(“IP Address: “+ip.getHostAddress()); //getHostAddress for getting IP Adress
//Localhost
InetAddress ip1 = InetAddress.getLocalHost();
System.out.println(“Host Name: “+ip1.getHostName()); //getHostName for getting host name
System.out.println(“IP Address: “+ip1.getHostAddress()); //getHostAddress for getting IP Adress
//Loopback Address
System.out.println(“Loopback Address: -“+ip1.getLoopbackAddress());
//HashCode of IP
String ip2=ip1.getHostAddress();
System.out.println(“Hash Code of Address: -“+ip2.hashCode());
}
catch(Exception e){System.out.println(e);}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s