当前位置: 代码迷 >> J2SE >> 关于UDP,很迷惑
  详细解决方案

关于UDP,很迷惑

热度:4462   发布时间:2013-02-25 00:00:00.0
关于UDP,很纳闷
程序跑起来 为什么没打印呢??

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
  相关解决方案