程序跑起来 为什么没打印呢??
- Java code
public class UDPtest1 { public UDPtest1() throws Exception { DatagramSocket serversocket = new DatagramSocket(3000); byte[] b = new byte[100]; DatagramPacket packet = new DatagramPacket(b, 100); serversocket.receive(packet); System.out.println(new String(b, 0, packet.getLength())); serversocket.close(); } public static void main(String[] args) throws Exception { new UDPtest1(); }}
- Java code
public class UDPtest2 { public UDPtest2() throws Exception { DatagramSocket clientsocket = new DatagramSocket(); String str = "hello world"; DatagramPacket packet = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("localhost"), 3000); clientsocket.send(packet); clientsocket.close(); } public static void main(String[] args) throws Exception { new UDPtest2(); }}
------解决方案--------------------------------------------------------
我测试可以啊!
先运行UDPtest1,再运行UDPtest2
------解决方案--------------------------------------------------------
top top top