Write a program on datagram socket for client/server to display the messages on client side, typed at the server side.
Sender
import java.util.*;
import java.net.*;
public class Sender
{
public static void main(String args[]) throws Exception
{
System.out.println("sender ");
DatagramSocket
ds=new DatagramSocket();
Scanner s=new Scanner(System.in);
System.out.println("enter the message");
while(true)
{
String msg=s.nextLine();
InetAddress
ip=InetAddress.getByName("127.0.0.1");
DatagramPacket
dp=new DatagramPacket(msg.getBytes(),msg.length(),ip,3000);
ds.send(dp);
}
}
}
Receiver
import java.util.*;
import java.io.IOException;
import java.net.*;
public class Receiver
{
public static void main(String args[]) throws Exception
{
byte[] buf=new byte[1024];
System.out.println("receiver ");
DatagramSocket
ds=new DatagramSocket(3000);
while(true)
{
DatagramPacket
dp=new DatagramPacket(buf,1024);
ds.receive(dp);
String msg=new String(dp.getData(),0,dp.getLength());
System.out.println(msg);
}
}
}
Comments
Post a Comment