To implement communication between Client and Server using the UDP
protocol and datagrams
TServer
import java.net.*;
import java.io.*;
import java.util.*;
public class TServer {
public static void main(String[] args) throws Exception{
DatagramSocket ss = new DatagramSocket(1234);
while(true){
System.out.println("Server is up....");
byte[] rd=new byte[100];
byte[] sd=new byte[100];
DatagramPacket rp = new DatagramPacket(rd,rd.length);
ss.receive(rp);
InetAddress ip= rp.getAddress();
int port=rp.getPort();
Date d=new Date(); // getting system time
String time= d + ""; // converting it to String
sd=time.getBytes(); // converting that String to byte
System.out.println(port);
System.out.println(ip);
DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,port);
ss.send(sp);
rp=null;
System.out.println("Done !! ");
}
}
}
TClient
import java.net.*;
import java.io.*;
public class TClient {
public static void main(String[] args) throws Exception{
System.out.println("Server Time >>>>");
DatagramSocket cs = new DatagramSocket();
InetAddress ip=InetAddress.getByName("localhost");
byte[] rd=new byte[100];
byte[] sd=new byte[100];
DatagramPacket sp=new DatagramPacket(sd,sd.length,ip,1234);
DatagramPacket rp=new DatagramPacket(rd,rd.length);
System.out.println(ip);
cs.send(sp);
cs.receive(rp);
data=new String(rp.getData());
System.out.println(data);
cs.close();
}
}
Thanks for sharing that such good information.
ReplyDeleteOffshore dedicated server