Вопрос

I am making a simple game that uses UDP to send/receive packets. I am going to be sending packets such as "loc 321,108" to move a player to 321,108, but I cannot figure out a good way to check which user to move/update location, since sending "loc myusername 321,108" would greatly increase the bandwidth usage.

Could anyone point me in the right direction?

Thanks.

Это было полезно?

Решение 2

Adding a short string to your packets isn't going to be a big deal unless you're sending them at a very high rate. And for a simple game, you're probably Doing It Wrong if you're sending that many packets. The only thing that you need to be concerned about is exceeding the MTU of your network, which you probably won't because it's about 1500 bytes for most networks these days.

If you really think that you can't afford a few bytes for a peer name, you could try using the remote socket address (IP address and UDP port). Don't use only the remote IP address, because it may not be unique. I'm not sure off hand how to get that in C#.

Другие советы

One suggestion would be to use the senders IP address (assuming one player per PC). Details here:

C# Getting sender address from UDP message

Basically you just use the following:

EndPoint remote_ep = new IPEndPoint(IPAddress.Any, 0);
sendSocket.ReceiveFrom(data, ref remote_ep);   //remote_ep now contains the originators IP

Use recvfrom to get the address of were the data came from. Heres a good reference example:

http://www.beej.us/guide/bgnet/output/html/multipage/recvman.html

From there the server would need a list of users and address linked to that user. Also a means of handling data from addresses with no user tied to them. Like people that haven't logged into the game yet.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top