本帖最后由 谢威 于 2013-7-22 14:26 编辑
为什么我的代码编译时候没有问题,运行时却出错了。- import java.net.*;
- class UdpSend
- {
- public static void main(String[] args) throws Exception
- {
- DatagramSocket ds = new DatagramSocket(10000);
- byte[] buf = "Udp xie wei Hello!".getBytes();
- DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.101"),1000);
- 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);
- }
- }
复制代码 |