简体   繁体   中英

is it possible to receive datagrampackets from many clients in one socket in the server side?

i am trying to make a project about sending and receiving sound states from clients to server and the server is ending back acknowledgments,I am using UDP to make the communication much faster,in the server java class i have a unicast class inside a multicast class,the multicast class is for getting the clients joining the multicast group then it will pass the IP and the Port of the client class to run the sound state sending and receiving, when i run the one client every thing seems ok but when i run the second client i obtain and error"Address already in use, cannot bind" and the error is in line 139 which is datagramSocketrecieving=new DataSocket(getsocket());

so my question is can i use the same socket in the server side to receive packets from different clients and of course if you see something wrong or can be modified to be more efficient let me know, thank you

here is the code of the "server class" on github cause its more than 200 lines

https://github.com/kameluo/tfmserver/blob/master/src/projectserver/MulticastthreadRun2.java

Yes, as long as you do not connect the datagram socket to a particular remote address, the same socket can be used to receive packets from multiple clients. Each instance of the DatagramPacket in that case can have a different "remote address" (IP address / port number pair). And you can likewise send from that one socket to any number of different remote addresses. The local (server-side) socket address will always be the same of course (well, technically, the local port will always be the same, but if you have multiple network interfaces, it is possible the local IP address may vary according to from where the packet arrived and/or to where a packet is being sent).

You can also use one "well known" server socket for initial rendezvous, then create a new socket per client. In other words, have your clients initially send a single message to the 20002 port. The server can then note the client's IP address and port, and create a new server side socket (without specifying its local port). Then connect that new socket to the client's IP address and port. A side-effect of connecting the socket is that the OS will choose an unused local port for the new server-side socket.

All subsequent communication between the server and that client can then use this client-specific socket. You would also need the client to pay attention to the address/port in the initial reply message so that it would send subsequent messages to the server's client-specific socket.

If you go the latter route: Since you are using UDP, you will also need to have some timeout mechanism for each per-client socket to handle abnormal terminations (if a client exits without telling you, UDP won't notify you that the client has disappeared, whereas with TCP, you would have gotten an end-of-file on the socket).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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