import java.net.*;
class SendData
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(1456);
byte[] buf="发送一个数据".getBytes();
DatagramPacket ap=new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10000);
ds.send(ap);
ds.close();
}
}
class ReceiveData
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds=new DatagramSocket(10000);
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip=dp.getAddress().getHostAddress();
String data=new String(dp.getData(),0,dp.getLength());
int port=dp.getPort();
System.out.println(ip+"::"+data+"::"+port);
ds.close();
}
}
|
|