使用Java NIO的异步UDP服务器 - 如何识别客户端?

发布于 2024-12-26 01:32:09 字数 424 浏览 4 评论 0原文

我正在尝试使用 Java NIO 编写异步 UDP 服务器, 我的问题是找到每个客户端的标识符,例如 IP 地址或类似的东西。 根据这篇文章:http://thushw.blogspot.com/2011/06/asynchronous-udp-server-using-java-nio.html 在UDP中,只有一个套接字用于与所有客户端进行通信,我想保存一个Hushtable,它将每个客户端的数据保存在缓冲区中。像(IP,ClientBuffer)之类的东西。 对于特定客户端发送的每次数据接收,我想将其添加到他的缓冲区中。

我注意到在datagramPacket中我可以通过datagramPacket.getSocketAddress()方法接收IP,但我没有找到从chanel获取datagramPacket的方法,我们只获取数据,而不是带有地址的整个包。

提前致谢!!!! 里夫卡:)

I'm trying to write an Asynchronous UDP server using Java NIO ,
My problem is to find an identifier per client like IP address or somthing like that .
according to this article :http://thushw.blogspot.com/2011/06/asynchronous-udp-server-using-java-nio.html
In UDP there is only one socket for communicate with all clients , I want to hold an Hushtable that will save the data per client in buuffers . somthing like ( IP , ClientBuffer ) .
for every receive of data that a spcific client is sending i want to add it to his buffer .

I notice thet in datagramPacket i can recive the IP ,by the method datagramPacket.getSocketAddress() but i did'nt find the way to get the datagramPacket from the chanel ,we are only get the data , not the whole package with the addresse.

Thanks in advance!!!!
Rivka :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

粉红×色少女 2025-01-02 01:32:09

只有连接到客户端时才能使用DatagramChannel.read()方法,因此您必须已经知道远程地址:端口。否则,当您在 DatagramPacket 中获取远程 ip:port 时,您必须调用 receive()。所以无论哪种方式你都拥有它。

You can only use the DatagramChannel.read() method if it is connected to the client, so you must already know the remote address:port. Otherwise you have to call receive(), when you get the remote ip:port in the DatagramPacket. So either way you have it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文