question

Daniel Javadi Moth avatar image
Daniel Javadi Moth asked

Having one unique UDP port per client in game server

Hello, My game uses both TCP and UDP for communication. The TCP connection is established first, where a new UDP socket will be created on the server for this client only.

The port of this socket is chosen by the operating system (with the .NET UdpClient (int) constructor with a port number of 0). The port given is then sent back to the client via the TCP connection, which the client then uses to send UDP packets to the server.

The client will send a packet to the server, which will identify the client with the Socket.ReceiveFrom (byte[], ref EndPoint) method, where the EndPoint will contain the remote endpoint of the client.

This works locally and on an Azure VM with all ports open. It does not work in a game server allocated by through PlayFab. A client can connect with TCP, but the UDP "connection" fails. As I do not know this port before the client connects, I cannot specify this port in the build.

I have thought of two possible solutions: 1. As at most ~20 clients will be connected to the server at a time, I could specify 20 ports in the build for UDP communication, and then have the server allocate these ports to clients connecting, and reusing a port when a client disconnects. 2. Use just one port for UDP communication. The reason I don't do this in the first place, is that in order to get address that the server should send a UDP response back to, I have to use the Socket.ReceiveFrom method, which will create a new EndPoint object whenever an EndPoint different from the last is received from. As multiple clients will be sending data multiple ~60 times per seconds, this will generate a some amount of garbage. It might be insignificant, but I would still prefer to be without it.

How would I best solve this problem? Thanks in advance.

multiplayer
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

The two methods you mentioned should also be the solutions I can think of at present. But I prefer the second. “I have to use the Socket.ReceiveFrom method, which will create a new EndPoint object whenever an EndPoint different from the last is received from.” Why not use the same EndPoint object to call the ReceiveFrom method? Refer to Socket.ReceiveFrom Method (System.Net.Sockets) | Microsoft Learn.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Daniel Javadi Moth avatar image Daniel Javadi Moth commented ·

Thank you for the response. I have done some more testing, and it seems that on .NET 7, Socket.ReceiveFrom does create a new IPEndPoint object on each call GitHub Gist. This also makes sense since the class is immutable. I also looked through the source code at source.dot.net and referencesource.microsoft.com, and there is no indication that EndPoint objects are cached and reused.

It's not as elegant, but for the time being it seems like I will be using solution 1.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.