CLIENT SERVER CHAT APPLICATION
EXP of parallel distributed system
SERVER
import java.io.*;
import java.net.*;
public class DatagramServer
{
public static void main(String args[])
{
DatagramSocket sock=null;
try
{
sock=new DatagramSocket(7777);
byte[] buffer= new byte[65536];
DatagramPacket incoming=new DatagramPacket(buffer,buffer.length);
echo("server socket created.waiting for incoming data.....");
while(true)
{
sock.receive(incoming);
byte[] data=incoming.getData();
String s =new String(data,0,incoming.getLength());
echo("Server Says OK....Your message is:"+s);
DatagramPacket dp=new DatagramPacket(s.getBytes(),s.getBytes().length,incoming.getAddress(),incoming.getPort());
sock.send(dp);
}
}
catch(IOException e)
{
System.err.println("IOException" +e);
}
}
public static void echo(String msg)
{
System.out.println(msg);
}
}
CLIENT
import java.io.*;
import java.net.*;
import java.util.*;
public class DatagramClient
{
public static void main(String args[])
{
DatagramSocket sock=null;
int port=7777;
String s;
Scanner cin=new Scanner(System.in);
try
{
sock=new DatagramSocket();
InetAddress host=InetAddress.getByName("localhost");
while(true)
{
echo("enter messsage to send");
s=cin.next();
byte[] b=s.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,host,port);
sock.send(dp);
byte[] buffer=new byte[65536];
DatagramPacket reply=new DatagramPacket(buffer,buffer.length);
sock.receive(reply);
byte[] data=reply.getData();
s=new String(data ,0,reply.getLength());
}
}
catch(IOException e)
{
System.err.println("IOException" +e);
}
}
public static void echo(String msg)
{
System.out.println(msg);
}
}
OUTPUT:-
No comments:
Post a Comment