TCP Socket Applications



TCP Socket Application
a.    Chat Server

// TCP Sockets : Chat server and Chat client
// Server Program

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

public class ChatServer
{
     ServerSocket ss;
     Socket conc;
     ObjectOutputStream out;
     ObjectInputStream in;
    
     ChatServer()
     {
     }   
    
     public void run() throws IOException
     {
        try
          {
              // Creating Server Socket
              ss = new ServerSocket(1500,10);
             
              // Accepting Connection from Client at port 1500
              conc = ss.accept();
                                     
              out = new ObjectOutputStream(conc.getOutputStream());
              in = new ObjectInputStream(conc.getInputStream());
             
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             
              while(true)
              {
                   // Reading and displaying data from client
                   String c_str = (String)in.readObject();
                   System.out.println("Client : "+c_str);
                            
                   // Creating response
                   System.out.print("Server : ");
                   String s_str = br.readLine().trim();
                            
                   // Sending response
                   out.writeObject(s_str);
              }
             
          }                 
          catch(Exception e)
          {  
          }
          finally
          {
              in.close();
              out.close();
              ss.close();
          }
    }
    
    public static void main(String args[]) throws IOException
     {
        ChatServer cs = new ChatServer();
          cs.run();
     }
}




// TCP Sockets : Chat server and Chat client
// Client Program

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

public class ChatClient
{  
     Socket cs;
     ObjectOutputStream out;
     ObjectInputStream in;
    
     ChatClient()
     {
     }
     public void run() throws IOException
     {
        try
          {
              // Creating Client socket at port 1500
              cs = new Socket("localhost",1500);
         
              out = new ObjectOutputStream(cs.getOutputStream());
              in = new ObjectInputStream(cs.getInputStream());
           
              // Reading input from Client and sending to Server
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             
              while(true)
              {
                   System.out.print("Client : ");
                   String c_str = br.readLine().trim();
                   out.writeObject(c_str);
                            
                   // Reading and displaying response from server
                   String s_str = (String)in.readObject();
                   System.out.println("Server : "+s_str);
              }  
          }            
          catch(Exception e)
          {
          }
          finally
          {       
              in.close();
              out.close();
              cs.close();
          }
     }
     public static void main(String args[]) throws IOException
     {
          ChatClient cc = new ChatClient();
          cc.run();
     }
}




b.    Echo Server and Echo Client

// TCP Sockets : Echo server and Echo client
// Server Program

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

public class EchoServer
{
     ServerSocket ss;
     Socket conc;
     ObjectOutputStream out;
     ObjectInputStream in;
    
     EchoServer()
     {
     }   
    
     public void run() throws IOException
     {
        try
          {
              // Creating Server Socket
              ss = new ServerSocket(1500,10);
             
              // Accepting Connection from Client at port 1500
              conc = ss.accept();
                                     
              out = new ObjectOutputStream(conc.getOutputStream());
              in = new ObjectInputStream(conc.getInputStream());
             
              // Reading data from client
              String Name = (String)in.readObject();
             
              // Creating response
              Date dt = new Date();
              String res = "Welcome " + Name + "\n";
              res = res.concat("Current date and time is " + dt);
             
              // Sending response
              out.writeObject(res);
              System.out.println(res);
          }                 
          catch(Exception e)
          {  
          }
          finally
          {
              in.close();
              out.close();
              ss.close();
          }
    }
    
    public static void main(String args[]) throws IOException
     {
        EchoServer es = new EchoServer();
          es.run();
     }
}



// TCP Sockets : Echo server and Echo client
// Client Program

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

public class EchoClient
{  
     Socket cs;
     ObjectOutputStream out;
     ObjectInputStream in;
    
     EchoClient()
     {
     }
     public void run() throws IOException
     {
        try
          {
              // Creating Client socket at port 1500
              cs = new Socket("localhost",1500);
         
              out = new ObjectOutputStream(cs.getOutputStream());
              in = new ObjectInputStream(cs.getInputStream());
           
              // Reading input from Client and sending to Server
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
             
            System.out.print("Enter name : ");
              String Name = br.readLine().trim();
             
              out.writeObject(Name);
              System.out.println(Name);
             
              // Reading and displaying response from server
              String res = (String)in.readObject();
              System.out.println(res);                          
          }            
          catch(Exception e)
          {
          }
          finally
          {       
              in.close();
              out.close();
              cs.close();
          }
     }
     public static void main(String args[]) throws IOException
     {
          EchoClient ec = new EchoClient();
          ec.run();
     }
}



c.    File Transfer


// TCP Sockets : File Transfer
// Server Program

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

public class FT_Server
{
     ServerSocket ss;
     ObjectOutputStream out;
     ObjectInputStream in;
     Socket conc;
     FileInputStream fin;
    
     FT_Server()
     {
     }   
    
     public void run() throws IOException
     {
        try
          {
              // Creating Server Socket
              ss = new ServerSocket(1500,10);
             
              // Accepting Connection from Client at port 1500
              conc = ss.accept();
                                     
              out = new ObjectOutputStream(conc.getOutputStream());
              in = new ObjectInputStream(conc.getInputStream());
             
              // Reading file name from client
              String Name = (String)in.readObject();
             
              System.out.println(Name);
             
              // Sending file
              fin = new FileInputStream(Name);
              while(true)
              {
                   int i = fin.read();
                   if(i==-1)
                        break;
                   out.writeObject((char)i);     
              }
                       
                       
          }                 
          catch(Exception e)
          {  
          }
          finally
          {
              in.close();
              out.close();
              ss.close();
              fin.close();
          }
    }
    
    public static void main(String args[]) throws IOException
     {
        FT_Server s = new FT_Server();
          s.run();
     }
}



// TCP Sockets : File Transfer
// Client Program


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

public class FT_Client
{       
          Socket cs;
          ObjectOutputStream out;
          ObjectInputStream in;
          FileOutputStream fout;
          FT_Client()
          {
          }
          public void run() throws IOException
          {
        try
                   {
                             // Creating Client socket at port 1500
                             cs = new Socket("localhost",1500);
                  
                             out = new ObjectOutputStream(cs.getOutputStream());
                             in = new ObjectInputStream(cs.getInputStream());
           
                             // Reading input from Client and sending to Server
                             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                            
            System.out.print("Enter source file name : ");
                             String ipt = br.readLine().trim();
                             out.writeObject(ipt);
                            
                             System.out.print("Enter destination file name : ");
                             String opt = br.readLine().trim();
                             fout = new FileOutputStream(opt);
                             while(true)
                             {
                                      // Reading and displaying response from server
                                      char c = (char)in.readObject();
                                      fout.write(c);        
                             }
                   }                          
                   catch(Exception e)
                   {
                   }
                   finally
                   {                 
                             in.close();
                             out.close();
                             cs.close();
                             fout.close();
                   }
          }
          public static void main(String args[]) throws IOException
          {
                   FT_Client c = new FT_Client();
                   c.run();
          }
}