Client-Server Communication using TCP UDP Protocols in Java Networking

 IN SERVER Side :

============================================================

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

class Server
{
    public static void main(String[] args)throws Exception{
        ServerSocket ss = new ServerSocket(3333);
        Socket s = ss.accept();
        
        DataInputStream din = new DataInputStream(s.getInputStream());
        DataOutputStream dout =  new DataOutputStream(s.getOutputStream());
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String str ="",str2="";
        while(!str.equals("Stop"))
        {
            str = din.readUTF();
            System.out.println("\nClient Saying :"+str);
            
            str2 = br.readLine();
            dout.writeUTF(str2);
            
            dout.flush();
        }
        din.close();
        s.close();
        ss.close();
    }
}
In CLIENT Side - 
import java.net.*;
import java.io.*;

class Client
{
    public static void main(String[] args)throws Exception{
        Socket s = new Socket("localhost",3333);
        
        DataInputStream din =  new DataInputStream(s.getInputStream());
        DataOutputStream dout = new DataOutputStream(s.getOutputStream());
        
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        
        String str ="4",str2="";
        while(!str.equals("Stop"))
        { 
            str = br.readLine();
            dout.writeUTF(str);
            dout.flush();
            str2 = din.readUTF();
            System.out.println("Server Saying : " +str2);
        }
        
    }
}   

OUTPUT:

Post a Comment (0)
Previous Post Next Post