黑马程序员技术交流社区
标题:
关于day23-网络编程(Udp-接收端/发送端) xp & win8
[打印本页]
作者:
朴日男
时间:
2014-10-4 10:39
标题:
关于day23-网络编程(Udp-接收端/发送端) xp & win8
import java.net.*;
class UdpSend
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(8888);
byte[] buf = "
udp im coming
".getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.102"),10001);
ds.send(dp);
ds.close();
}
}
//--------------------
class UdpRece
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket();
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();
}
}
//---------------------------------------
一切步骤和按毕老师的一样, 就是接收不到消息, 为什么???????????
毕老师的是 xp系统, 我的是 win8.1 笔记本 wifi, 和这有关系吗???
作者:
WakeUp
时间:
2014-10-4 10:47
接收端监听的端口没写,DatagramSocket ds = new DatagramSocket(10001);
作者:
朴日男
时间:
2014-10-4 11:13
WakeUp 发表于 2014-10-4 10:47
接收端监听的端口没写,DatagramSocket ds = new DatagramSocket(10001);
:handshake 万分感谢!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2