Introduction to Socket Programming

OK so this post is going to be on Socket Programming in Java. A socket is one endpoint of a two-way communication link between two programs running on the network.
A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.

A socket looks something like this 128.1.21.11:3866.
Here 3866 helps the TCP layer identify the application that data is destined to be sent.

Socket Programming is generally used for developing Client Server applications. The Server program is always waiting to listen for a client request.
So firstly, we create the server program and run it. We will be using two classes, Socket and ServerSocket. Both these classes can be found in the package java.net.*;.
ServerSocket object binds the program to a certain port and Socket object is used to interact with the client.

Server Program:

Client Program:

Server Program Output:

Client Program Output:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">