JAVA Socket Programing


What is Socket?

Socket is the transport layer entity which deals with process to process communication.

A system attachment is an endpoint of a between procedure correspondence stream over a workstation system. Today, generally correspondence between workstations is dependent upon the Internet Protocol; accordingly generally organize attachments are Internet attachments.  An attachment API is a requisition modifying interface (API), for the most part gave by the working framework, which permits provision projects to control and use system attachments. Web attachments Api’s are ordinarily dependent upon the Berkeley attachments standard.   An attachment address is the fusion of an IP location and a port number, much similar to one end of a phone association is the blending of a telephone number and a specific augmentation. In light of this address, web attachments convey approaching information parcels to the fitting provision process or string.

Java abstract outs most of the low level aspects of socket programming.

  • JAVA.net PACKAGE   consist all the classes required to create network enabled application. Server Socket and Socket are also part of this package. Apart from these classes it also contains classes to connect to the web server.
  • SERVERSOCKET class provides a server socket at server side such socket wait for request from client. Once such request arrive a server process the request.
  • SOCKET this class provides client side socket. They are at the client side connecting to the server. They reside on user machine sends request to the server.
  • Steps involved in creating socket-based applications

 

Socket Programming

 

The server side Code

The servers main function is to serve incoming requests when they come in

  • First of all create a socket that monitors a particular port.  For that you need to create a object of the socket class. There are four different ways to create an instance of Serversocket

ServerSocket() this simply sets the implemented that name all parameters are default.

ServerSocket(int port) this creates socket and  monitors the particular port number.

ServerSocket(int port, int backlog) this not only binds the created socket the port number but also create a waiting queue of the length specified.

ServerSocket(int port ,int backlog, InetAddress bindAddr)

This creates a server side socket that is bounded to the specified port number.

Example

ServerSocket  s= new ServerSocket (5555,5,”localhost”);

 

Next step is to listen indefinitely and  accept incoming connection. For that you need to call accept() method of ServerSocket

Socket in s.accept();

To send and receive data using the socket object to communicate using socket you have obtain Input and Output stream this can be done by getInputStream()  and getoutputStream()

The Client Side Code

Use of client side socket is to connect to the server and communicate.

First step in client side socket is to create a socket  which needs to know the server name here are some constructors used to create socket

Socket()

This constructor simply creates a new socket without connecting to the host

Socket(InetAddress add, int port) This constructor creates the new socket and connects to the specified port.

Socket(java.lanag.String host , int port) this functions like Socket() here instead of address host name is used.

So to connect to the localhost at port 55555

The code will be

Socket  s= new socket();

s.connect(new  socketAddress(“localhost”,5555);

 

Facebooktwittergoogle_plusredditlinkedinmailFacebooktwittergoogle_plusredditlinkedinmail

About Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe.

Professional Skills: Programming – Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc.

Leave a comment