import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//发送端没有错误
class UdpSend
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket();
byte[] buf = "fa song xiao xi".getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("117.130.95.90"),10000);
ds.send(dp);
ds.close();
}
}
//接收端运行就报错,请哪位帮我看看
class UdpRece
{
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);
}
}
|
|