PROGRAM
Server Side
import java.io.*;
import java.net.*;
class Server
{
public static
DatagramSocket serversocket;
public static
DatagramPacket dp;
public static
BufferedReader dis;
public static
InetAddress ia;
public static byte
buf[]=new byte[1024];
public static int
cport=789,sport=790;
public static void
main(String a[])throws IOException
{
serversocket=new DatagramSocket(sport);
dp=new
DatagramPacket(buf,buf.length);
dis=new
BufferedReader(new InputStreamReader(System.in));
ia=InetAddress.getLocalHost();
System.out.println("Server
is waiting for data from client");
while(true){
serversocket.receive(dp);
String s=new
String(dp.getData(),0,dp.getLength());
System.out.println(s);
}}}
Client Side
import java.io.*;
import java.net.*;
class Client{
public static
DatagramSocket clientsocket;
public static
BufferedReader dis;
public static
InetAddress ia;
public static byte
buf[]=new byte[1024];
public static int
cport=789,sport=790;
public static void
main(String a[])throws IOException{
clientsocket = new
DatagramSocket(cport);
dis=new
BufferedReader(new InputStreamReader(System.in));
ia=InetAddress.getLocalHost();
System.out.println("Client
is sending data to Server ");
while(true){
String
str=dis.readLine();
buf=str.getBytes();
clientsocket.send(new
DatagramPacket(buf,str.length(),ia,sport));
}}}
No comments:
Post a Comment