|
package com.renxing.r0038_socket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//接收端
public class SocketDatagramSocketDemo2 {
public static void main(String[] args) throws IOException
{
//邮局接到邮包
DatagramSocket ds=new DatagramSocket(10000);
//分解成小的邮包
byte [] bys=new byte[1024];
//邮递员疯了 全部都得需要接受
DatagramPacket dp=new DatagramPacket(bys,bys.length);
//邮递员接受了
ds.receive(dp);
//邮递员看了下地址:和相关的内容! 汗了!- -!
String ip=dp.getAddress().getHostAddress();
String context=new String(bys,0,bys.length);
System.out.println(ip+":::"+context);
ds.close();
}
}
package com.renxing.r0038_socket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//Udp发送端
public class SocketDatapramSocketDemo
{
public static void main(String[] args) throws IOException
{
//建立服务端
DatagramSocket ds=new DatagramSocket();
//建立数据包
byte []bys="任星的数据包".getBytes();
DatagramPacket dp=new DatagramPacket(bys,bys.length,InetAddress.getByName("localhost"),10000);
ds.send(dp);
ds.close();
}
} |
|