Socket Programming in Java

0
843
Writing Factorial program in java
Java GUI Calculator Source Code

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:

//PROGRAMMING TUNNES
//Server Code
import java.io.*;
import java.net.*;

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");
         out.print(data);
         out.close();
         skt.close();
         serverSock.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
    
   }
}

Client code:

//PROGRAMMING TUNNES
//Client Code
import java.io.*;
import java.net.*;

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

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

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

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 172.16.100.98, then your client IP field must be 172.16.100.98

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

Writing Factorial program in java
Java GUI Calculator Source Code

Leave a Reply