Thursday, May 13, 2021
Home College Assignments Socket Programming in Java

Socket Programming in Java

A Socket is the gate way of a two way communication link established between two programs running on the network. Socket consists of IP address and the port identifying the application that the data is destined to be sent.

We take two end systems, the server (response) and the client (request). As for the client perspective, it requests data to the server via connection socket. Server set up a listening socket for client connection sockets. Whatever the application is, it must use TCP or UDP service.

If you are familiar with socket stuff, then answer why we need java sockets for our application data mostly?

Yes, the beauty of java sockets is that they are quite simple and easy to manage. Java socket wraps the details of the transfer protocols, providing an easy interface with the network.

For two end systems, we need to code two applications, one for the server that listens for the client, another for the client to request data from the server.

Server code:

//Server Code

class server {
   public static void main(String args[]) 
      String data = "HASTA LA VISTA, Baby";
      ServerSocket serverSock=null;
      Socket skt=null;
      PrintWriter out=null;
      try {
         serverSock = new ServerSocket(1234);
         skt = serverSock.accept();
         System.out.print("Server has connected!\n");
         out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending string: '" + data + "'\n");
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");

Client code:

//Client Code

class client {
public static void main(String args[]) {
      try {
         Socket skt = new Socket("localhost", 1234);
         BufferedReader in = new BufferedReader(new
         System.out.print("Received string: '");

         while (!in.ready()) {}
         System.out.println(in.readLine()); // Read one line and output it

      catch(Exception e) {
         System.out.print("Server Down!\n");

Above two programs are running on a single machine, that’s we did not specify IP field. For better understanding, you need two end systems, one for server and the other for the client. Now you have to set the IP field as specified by the server network properties. For example your server has IP address, then your client IP field must be

Now you know the basics, you can make your own free chat rooms and messengers.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....