Program to chat using UDP Protocol
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class udpchs
{
public static void main(String ar[]) throws Exception
{
DatagramSocket ssoc=new DatagramSocket(3232);
BufferedReader dt=new BufferedReader(new InputStreamReader(System.in));
InetAddress ip=InetAddress.getLocalHost();
byte buf[]=new byte[1024];
DatagramPacket dpac=new DatagramPacket(buf,buf.length);
while(true)
{
ssoc.receive(dpac);
String str1=new String(dpac.getData(),0,dpac.getLength());
System.out.println("Received: "+str1);
String str=new String(dt.readLine());
buf=str.getBytes();
ssoc.send(new DatagramPacket(buf,buf.length,ip,32));
}
}
}
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class udpchc
{
public static void main(String ar[]) throws Exception
{
DatagramSocket ssoc=new DatagramSocket(32);
InetAddress ip=InetAddress.getLocalHost();
BufferedReader dt=new BufferedReader(new InputStreamReader(System.in));
byte buf[]=new byte[1024];
DatagramPacket dpac=new DatagramPacket(buf,buf.length);
//String str=new String(dpac.getData());
while(true)
{
String str=new String(dt.readLine());
buf=str.getBytes();
ssoc.send(new DatagramPacket(buf,buf.length,ip,3232));
ssoc.receive(dpac);
String str2=new String(dpac.getData(),0,dpac.getLength());
System.out.println("Received: "+str2);
}
}
}
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class udpchs
{
public static void main(String ar[]) throws Exception
{
DatagramSocket ssoc=new DatagramSocket(3232);
BufferedReader dt=new BufferedReader(new InputStreamReader(System.in));
InetAddress ip=InetAddress.getLocalHost();
byte buf[]=new byte[1024];
DatagramPacket dpac=new DatagramPacket(buf,buf.length);
while(true)
{
ssoc.receive(dpac);
String str1=new String(dpac.getData(),0,dpac.getLength());
System.out.println("Received: "+str1);
String str=new String(dt.readLine());
buf=str.getBytes();
ssoc.send(new DatagramPacket(buf,buf.length,ip,32));
}
}
}
Client:
import java.io.*;
import java.net.*;
import java.util.*;
class udpchc
{
public static void main(String ar[]) throws Exception
{
DatagramSocket ssoc=new DatagramSocket(32);
InetAddress ip=InetAddress.getLocalHost();
BufferedReader dt=new BufferedReader(new InputStreamReader(System.in));
byte buf[]=new byte[1024];
DatagramPacket dpac=new DatagramPacket(buf,buf.length);
//String str=new String(dpac.getData());
while(true)
{
String str=new String(dt.readLine());
buf=str.getBytes();
ssoc.send(new DatagramPacket(buf,buf.length,ip,3232));
ssoc.receive(dpac);
String str2=new String(dpac.getData(),0,dpac.getLength());
System.out.println("Received: "+str2);
}
}
}
0 comments:
Post a Comment