简体   繁体   English

如何在绑定DatagramSocket类远程套接字地址

[英]How to bind remote socket address in DatagramSocket

How to Set Remote Machine address in the UDP socket, That should be returned while the receiver getting the socket address? 如何在UDP套接字中设置远程计算机地址,接收方获取套接字地址时应返回该地址?

My Code: 我的代码:

DatagramSocket socket = new DatagramSocket();

packet = new DatagramPacket(new byte[10],10);

packet.setAddress(InetAddress.getByName(hostName));

packet.setPort(portNum);

byte[] data = message.getBytes();

packet.setData(data);

packet.setLength(data.length);

socket.send(packet);
socket.close();

But this create a socket with my local address, receiver receives my address instead of that remote host address. 但这会使用我的本地地址创建一个套接字,接收方会收到我的地址,而不是该远程主机地址。

What is the solution for this? 有什么解决方案?

You are the one making the request, so your address will be the one that gets submitted for the response. 您是发出请求的人,因此您的地址将是为响应提交的地址。 If you are wanting the remote machine to receive the response, the UDP connection (Datagram Socket Connection) must be established by the remote machine and not your own. 如果你想要在远程机器接收响应,UDP连接(数据报套接字连接)必须由远程计算机来建立,而不是你自己的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM