简体   繁体   English

检查谁发送了UDP数据包?

[英]Check who sent a UDP packet?

I am making a simple game that uses UDP to send/receive packets. 我正在制作一个使用UDP发送/接收数据包的简单游戏。 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. 我将发送诸如“ loc 321,108”之类的数据包以将播放器移动到321,108,但是我无法找出一种检查哪个用户移动/更新位置的好方法,因为发送“ loc myusername 321,108”会大大增加带宽用法。

Could anyone point me in the right direction? 有人能指出我正确的方向吗?

Thanks. 谢谢。

One suggestion would be to use the senders IP address (assuming one player per PC). 一种建议是使用发件人的IP地址(假设每台PC有一个播放器)。 Details here: 详细信息在这里:

C# Getting sender address from UDP message C#从UDP消息获取发件人地址

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. 使用recvfrom获取数据来自的地址。 Heres a good reference example: 这是一个很好的参考示例:

http://www.beej.us/guide/bgnet/output/html/multipage/recvman.html 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. 就像尚未登录游戏的人一样。

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. 您唯一需要担心的是超出网络的MTU,您可能不会这样做,因为如今大多数网络的MTU约为1500字节。

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). 如果您确实认为对不起同一个名称,那么您可以尝试使用远程套接字地址(IP地址和UDP端口)。 Don't use only the remote IP address, because it may not be unique. 不要仅使用远程IP地址,因为它可能不是唯一的。 I'm not sure off hand how to get that in C#. 我不确定如何在C#中获得它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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