本帖最后由 王宝龙 于 2012-10-2 17:30 编辑
UdpSend类中的DatagramSocket ds = new DatagramSocket(8888)指定了端口
在下面的DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.0.103"),10000)这个的10000也是在指定端口吗??
然后在UdpRece类中DatagramSocket ds = new DatagramSocket(10000);设置端口为10000,那上面如果设置了8888,这里还能接受吗?
问的可能有点不清楚 !!大家帮忙给看一下!!谢谢了!!- import java.net.*;
- class UdpSend
- {
- public static void main(String[] arge) throws Exception
-
- {
- <font color="red">DatagramSocket ds = new DatagramSocket(8888);//这里也指定了端口为8888</font>
-
- byte[] buf = "udp 哥们来了".getBytes();
- <font color="red">DatagramPacket dp =
- new DatagramPacket(buf,buf.length,
- InetAddress.getByName("192.168.0.103"),10000);//这里已指定了端口为10000
- </font>
- ds.send(dp);
-
- ds.close();
- }
- }
- class UdpRece
- {
- public static void main(String[] arge) throws Exception
- {
- <font color="red">DatagramSocket ds = new DatagramSocket(10000);</font>
-
- 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);
-
- ds.close();
- }
- }
复制代码 |