java远程UDP端口监听,为什么接收不到消息 20
DatagramSocketdSocket=null;dSocket=newDatagramSocket();dSocket.connect(InetAddress.ge...
DatagramSocket dSocket = null;
dSocket = new DatagramSocket();
dSocket.connect(
InetAddress.getByName(arrays[arrays.length - 2]),
UDP);
byte buf[] = new byte[4096];
DatagramPacket dPacket = new DatagramPacket(buf, buf.length);
dSocket.receive(dPacket); // 阻塞语句
byte[] b = dPacket.getData();
String info = new String(b, 0, b.length, "UTF-8");
out.print(info.trim());
System.out.println(info.trim());
就一个接收的部分,这样写对吗?来看下为什么接收不到消息 展开
dSocket = new DatagramSocket();
dSocket.connect(
InetAddress.getByName(arrays[arrays.length - 2]),
UDP);
byte buf[] = new byte[4096];
DatagramPacket dPacket = new DatagramPacket(buf, buf.length);
dSocket.receive(dPacket); // 阻塞语句
byte[] b = dPacket.getData();
String info = new String(b, 0, b.length, "UTF-8");
out.print(info.trim());
System.out.println(info.trim());
就一个接收的部分,这样写对吗?来看下为什么接收不到消息 展开
2个回答
展开全部
//发送端
DatagramSocket ds=new DatagramSocket(5050);
byte[] b="client".getByte("UTF-8");
DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getByName ("127.0.0.1"),6060);
ds.send(dp);
//接收端
DatagramSocket ds=new DatagramSocket(6060);
byte[] b=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
System.out.println(new String(dp.getData(),dp.getOffset(),dp.getLength));
DatagramSocket ds=new DatagramSocket(5050);
byte[] b="client".getByte("UTF-8");
DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getByName ("127.0.0.1"),6060);
ds.send(dp);
//接收端
DatagramSocket ds=new DatagramSocket(6060);
byte[] b=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
System.out.println(new String(dp.getData(),dp.getOffset(),dp.getLength));
追问
现在是用的eyebeam设置的端口,所以发送端不用管,打开eyebeam之后就一直有消息发送,可以设置端口和ip,现在的要求是监听远程端口的消息,不要本机的,本机的是正确的,只要换成远程的UDP端口就没有消息了,还不报错,求监听远程端口的代码
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家国内优质海外HTTP代理商,拥有一个庞大的IP资源池,覆盖200多个地区,IP数量大且匿名度高。其优点还包括超高并发、稳定高效、技术服务等特点,同时提供HTTP、HTTPS以及SOCKS5协议支持。此外,Sto...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询