client
import java.io.*;
import java.net.*;
import java.util.*;
class Client13
{
public static void main(String args[])
{
try
{
DatagramSocket client=new DatagramSocket();
while(true)
{
InetAddress addr=InetAddress.getByName("127.0.0.1");
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("You:");
String str=in.readLine();
sendbyte=str.getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,1309);
client.send(sender);
if(str.equals("bye") )
{
break;
}
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
client.receive(receiver);
String s=new String(receiver.getData());
String s1=s.trim();
System.out.println("Server:"+s1);
if(s1.equals("bye") )
{
break;
}
}
client.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Server
import java.io.*;
import java.net.*;
import java.util.*;
class Server13
{
public static void main(String args[])
{
try
{
DatagramSocket server=new DatagramSocket(1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new DatagramPacket(receivebyte,receivebyte.length);
server.receive(receiver);
String str=new String(receiver.getData());
String str2=str.trim();
System.out.println("Client:"+str2);
if(str2.equals("bye"))
{
break;
}
InetAddress addr=receiver.getAddress();
int port=receiver.getPort();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("You:");
String str1=in.readLine();
sendbyte=str1.getBytes();
DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
server.send(sender);
if(str1.equals("bye"))
{
break;
}
}
server.close();
}
catch(Exception e)
{
System.out.println(e);
}s}
}
Output
E:\networks>javac Client13.java
E:\networks>javac Server13.java
E:\networks>java Client13
You:hai
Server:hello
You:how r u
Server:fine
You:u
Server:bye
E:\networks>java Server13
Client:hai
You:hello
Client:how r u
You:fine
Client:u
You:bye
No comments:
Post a Comment