发布网友 发布时间:2022-04-20 05:56
共1个回答
热心网友 时间:2023-09-02 19:43
这个可以
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpRecv {
public static void main(String a[])throws Exception{
DatagramSocket ds=new DatagramSocket(3000);
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
ds.receive(dp);
String info=new String(buf,0,dp.getLength());
System.out.println(info);
System.out.println(dp.getAddress());
System.out.println(dp.getPort());
ds.close();
}
}
import java.net.*;
import java.net.SocketException;
public class UdpSend {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
DatagramSocket ds=null;
try {
ds=new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
String info="hello 程胜 udp";
DatagramPacket dp=new DatagramPacket(info.getBytes(),info.getBytes().length,InetAddress.getByName("172.168.42.249"),3000);
ds.send(dp);
ds.close();
}
}
把ip改了