使用Java NIO的异步UDP服务器 - 如何识别客户端?
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只有连接到客户端时才能使用
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 callreceive()
, when you get the remote ip:port in theDatagramPacket
. So either way you have it.