Search code examples
asynchronousudpipnio

Asynchronous UDP server using Java NIO - How to identify the client?


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 :)


Solution

  • 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.