Implementing Socket Programming in Java

TCPServer.java

import java.io.*;
import java.net.*;

class TCPServer 
{
   public static void main(String argv[]) throws Exception
      {
         String fromclient;
         String toclient;
          
         ServerSocket Server = new ServerSocket (5000);
         
         System.out.println ("TCPServer Waiting for client on port 5000");

         while(true) 
         {
         	Socket connected = Server.accept();
            System.out.println( " THE CLIENT"+" "+
            connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED ");
            
            BufferedReader inFromUser = 
            new BufferedReader(new InputStreamReader(System.in));    
     
            BufferedReader inFromClient =
               new BufferedReader(new InputStreamReader (connected.getInputStream()));
                  
            PrintWriter outToClient =
               new PrintWriter(
                  connected.getOutputStream(),true);
            
            while ( true )
            {
            	
            	System.out.println("SEND(Type Q or q to Quit):");
            	toclient = inFromUser.readLine();
            	
            	if ( toclient.equals ("q") || toclient.equals("Q") )
            	{
            		outToClient.println(toclient);
            		connected.close();
            		break;
            	}
            	else
            	{
            	outToClient.println(toclient);
                }
            	
            	fromclient = inFromClient.readLine();
            	
                if ( fromclient.equals("q") || fromclient.equals("Q") )
                {
                	connected.close();
                	break;
                }
                	
		        else
		        {
		         System.out.println( "RECIEVED:" + fromclient );
		        } 
			    
			}  
			
          }
      }
}

TCPClient.java

import java.io.*;
import java.net.*;

class TCPClient 
{
 public static void main(String argv[]) throws Exception
 {
  String FromServer;
  String ToServer;
  
  Socket clientSocket = new Socket("localhost", 5000);
  
  BufferedReader inFromUser =
                 new BufferedReader(new InputStreamReader(System.in));
     
  PrintWriter outToServer = new PrintWriter(
     clientSocket.getOutputStream(),true);
     
  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
     clientSocket.getInputStream()));
     
  while (true)
  {
    
    FromServer = inFromServer.readLine();
    
    if ( FromServer.equals("q") || FromServer.equals("Q"))
        {
        clientSocket.close();
        break;
        }
    
    else
    
        {
         System.out.println("RECIEVED:" + FromServer);
         System.out.println("SEND(Type Q or q to Quit):");
         
         ToServer = inFromUser.readLine();
         
         if (ToServer.equals("Q") || ToServer.equals("q"))
         {
         outToServer.println (ToServer) ;
         clientSocket.close();
            break;
         }
         
        else
        {
            outToServer.println(ToServer);
        }
      }
    }   
  }
}

Output

Client Server Programming Output
Client Server Programming Output

Leave a Reply

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