Client
import java.net.*; import java.io.*; class UDPClient { publicstaticvoid main(String args[]) throws Exception { DatagramSocket dsoc=new DatagramSocket(24); byte buff[]=newbyte[1024]; DatagramPacket dpack=new DatagramPacket(buff,buff.length); dsoc.receive(dpack); System.out.println(new String(dpack.getData())); } }
Server
import java.net.*; import java.io.*; import java.util.*; class UDPServer { publicstaticvoid main(String args[]) throws Exception { DatagramSocket dsoc=new DatagramSocket(5217); InetAddress host=InetAddress.getLocalHost(); String str=(new Date()).toString(); byte buf[]=str.getBytes(); dsoc.send(new DatagramPacket(buf,buf.length,host,24)); dsoc.close(); } }
No comments