2

I currently have a server that runs a game where each client controls a player. I have decided to use UDP because it does not wait for the client to receive the packet and therefore can run smoothly. I would like to assign each client a player, and the only way I believe I can do that is to assign each client a number based on the number of clients that had joined before them. That way, every time a client sends a packet I could identify which client they were and act accordingly.

However, UDP is not a connection-based method of communication, as it sends packets and hopes that someone on the same port is listening. I do not want to identify clients by IP number, for I test my application by running multiple clients on one computer that also holds the server.

So my question is: How does one find out how many clients are listening on a port? Or if that isn't possible, how does one distinguish one client from another and identify them through a number such as 1, 2, 3 or 4? I have not included any code because my current UDP server and client would be of no use, and this is a question about something I have no idea how to do.

Any help would be appreciated.

Anonymous
  • 179
  • 12
  • Not really a response, but this will maybe help you : http://www.developer.com/java/web/socket-programming-udp-clientserver-application.html Also it's quite old, but I did a network game in java, here's the link just in cases : https://bitbucket.org/Augustin_Bocken/helmoctf/src/1f5eeec4d037?at=default – Augustin Bocken Mar 20 '17 at 20:40
  • UDP clients don't 'listen on a port'. The word is 'packet', not 'package'. What does [tag:multicastsocket] have to do with it? – user207421 Mar 20 '17 at 20:57
  • The clients are receiving datagram packets through MulticastSockets. It is used in order for the client to join a group through the clientSocket.joinGroup(group) method. The code is: MulticastSocket clientSocket = new MulticastSocket(3333); – Anonymous Mar 20 '17 at 21:18
  • In that case you will definitely have to implement it yourself. Even the host doesn't know how many multicast group members there are, only whether are more than zero. You can use IP:port to identify the clients, as this is unique. – user207421 Mar 20 '17 at 21:33
  • I do not want to use the IP address. I test my code often and I use the same computer to run the clients and the server. If I identified the clients by IP, every client would be a duplicate. Is there any possible way to identify the client some other way? – Anonymous Mar 21 '17 at 19:36
  • Please read what I actually wrote instead of just repeating yourself. I said you can use ***IP:port*** as the client identifier. In other words use the pair *{IP address, port}* where *port* is the client's port, the source port of the `DatagramPacket`. This pair is unique. – user207421 Mar 23 '17 at 00:54
  • The only port I am familiar with is the number that is plugged into the code, MulticastSocket clientSocket = new MulticastSocket(3333). They are both the same for client and server. Forgive me if I mistook what you meant earlier, but are you talking about a different port that is unique every time the program is run? – Anonymous Mar 23 '17 at 21:14
  • Sigh. Every client has a socket. That socket has a port. If there are two clients in one host, the ports are different. When the client sends a datagram, its IP and port number appear at the receiver in the `DatagramPacket`. This is rather basic. – user207421 Mar 23 '17 at 22:46
  • And you don't need to use multicast sockets in the clients. You can send with an ordinary datagram socket. You onoy need multicast sockets for receiving. – user207421 Mar 24 '17 at 00:58

0 Answers0