Skip to content

Building Client-Server Communication for Exchanging String Messages

Building Client-Server Communication for Exchanging String Messages

PROBLEM STATEMENT:

Write a program to demonstrate the exchange of messages between a client and a server.

SOURCE CODE

Client.java

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

public class Client
{
   
 static int port;
  static int defaultport=9000;
  static InetAddress host;

Client(int port,InetAddress host)
    {
        this.port=port;
        this.host=host;
    }

public static void main(String args[])
        {
            try{
             int port = defaultport;
             host=InetAddress.getLocalHost();
             Client cl = new Client(port,host);
             cl.start();
            }
           
            catch(Exception e)
                {
                    e.printStackTrace();
                }
        }
 

  public void start()
    {
        try
        {
            DatagramSocket ds=new DatagramSocket();
            DatagramPacket snddp=null;
            byte buf[] =new byte[256];
            String req,res;
            BufferedReader br;
    br=new BufferedReader(new InputStreamReader(System.in));
                   
            do{
            String str = br.readLine();
    snddp = new DatagramPacket(str.getBytes(),str.length(),host,port);
            ds.send(snddp);
   
            //ds = new DatagramSocket(port);
            DatagramPacket dp = new DatagramPacket(buf,buf.length);       
            ds.receive(dp);
            res = new String(dp.getData(),0,dp.getLength());
           
                if(res.equals(“ono”))
                {
                    System.out.println(“Connection closed 🙁 “);
                    System.exit(0);
                }   
                System.out.println(res);
        }while(!(res.equals(“ono”)));   
           
        }
    catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}   

Server.java

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

public class Server
{
   
 static  int port;
  static int defaultport=9000;

Server(int port)
    {
        this.port=port;
    }

public static void main(String args[])
        {
            int port = defaultport;
            Server ser = new Server(port);
            ser.start();
        }

  public void start()
    {
        try
        {
            DatagramSocket ds;
            DatagramPacket dp;
            byte buf[] =new byte[256];
            String req,res;
            InetAddress lclhost;
            int sendport,inport;
            BufferedReader br;

            ds = new DatagramSocket(port);
            dp = new DatagramPacket(buf,buf.length);

            do{
                   
            ds.receive(dp);
            req = new String(dp.getData(),0,dp.getLength());
            lclhost = dp.getAddress();
            inport = dp.getPort();

            if(!(req.equals(“ono”)))
            {
                System.out.println(req);
                br=new BufferedReader(new InputStreamReader(System.in));
                res=br.readLine();
DatagramPacket dp1 = new DatagramPacket(res.getBytes(),res.length(),lclhost,inport);
ds.send(dp1);
            }

else
System.out.println(“CONNECTION CLOSED 🙁 “);
}while(!(req.equals(“ono”)));   
           
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

}
}

OUTPUT

Server.java
F:MSCDCpracsclent_server_prac10>java Server
hello
hi
how r u?
I m doing good
Bye…cya…
Ono

Client.java
F:MSCDCpracsclent_server_prac10>java Client
hello
hi
how r u?
I m doing good
Bye…cya…
Connection closed 🙁

Leave a Reply

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

error: Content is protected !!