简体   繁体   English

在localhost上测试UDP

[英]Testing UDP on localhost

I'm currently trying to make an online video game, which currently uses TCP to send packets. 我目前正在尝试制作一款在线视频游戏,目前使用TCP发送数据包。 I want to use UDP, but I'm extremely new to UDP and I'm having some issues. 我想使用UDP,但我对UDP非常新,我遇到了一些问题。

When I test my server/client(s) on the same computer (localhost), the packets seem to be interfering with each other. 当我在同一台计算机(localhost)上测试我的服务器/客户端时,数据包似乎互相干扰。

For example, if the client sends a packet to the server (UDP, 127.0.0.1:1338), the client ends up receiving the packet, since the client is also listening for packets from 127.0.0.1 on port 1338. Similarly, when I try to test 2 clients and 1 server, the clients may send out packets which are meant for the server, but are picked up by the other client. 例如,如果客户端将数据包发送到服务器(UDP,127.0.0.1:1338),则客户端最终将接收到该数据包,因为该客户端还在端口1338上侦听来自127.0.0.1的数据包。尝试测试2个客户端和1个服务器,客户端可能会发送出适用于服务器的数据包,但是被其他客户端接收。

How can I test UDP on localhost since all packets are being received from/sent to 127.0.0.1:1338? 我如何在localhost上测试UDP,因为所有数据包都是从127.0.0.1:1338接收/发送的? Do I need to implement some sort of layer in my packets that distinguishes if the packet is meant for the server, or a specific client? 我是否需要在数据包中实现某种层,以区分数据包是用于服务器还是特定客户端?

Only your Server should listen on the defined port number (1338). 只有您的服务器应该侦听定义的端口号(1338)。 Each client should select a free port number and send the server this port number. 每个客户端都应选择一个空闲端口号,并向服务器发送此端口号。 The server has to store the client information and send then the packets to the clients in this client list. 服务器必须存储客户端信息,然后将数据包发送到此客户端列表中的客户端。

The clients should also send a goodby packet when the client is closing to know on the server side which clients are still available and which aren't participating anymore. 当客户端关闭时,客户端还应该发送一个goodby数据包,以便在服务器端知道哪些客户端仍然可用以及哪些客户端不再参与。

You should also implement some kind of housekeeping in this client list. 您还应该在此客户列表中实施某种内务管理。 For example store the timestamp of the last received packet from the client and remove clients that haven't sent data for some time (crashed client, lost connection ...) from the list. 例如,存储从客户端接收到的最后一个数据包的时间戳,并从列表中删除一段时间未发送数据的客户端(客户端崩溃,连接丢失...)。

An additional layer will not help - because the server may never actually get the packet. 附加层无济于事 - 因为服务器可能永远不会真正获取数据包。

Make the ports on the server and client configurable. 使服务器和客户端上的端口可配置。 That way you could have different ports on the same machine for testing and change it when going to production. 这样,您可以在同一台计算机上使用不同的端口进行测试,并在进入生产时进行更改。 Just remember you need to configure both ports in both the client and server. 只要记住您需要同时在客户端和服务器中配置两个端口即可。 This is a good practice anyway. 无论如何,这是一个好习惯。

You still have another problem - of several clients residing on the same machine and listening to the same port. 您还有另一个问题 - 多个客户端驻留在同一台计算机上并侦听同一端口。 You can have a random port for each client (the client selects one in random and then notifies the server). 您可以为每个客户端提供一个随机端口(客户端随机选择一个端口,然后通知服务器)。 Or you can try binding to different IP addresses (one will use 127.0.0.1 an another will use the real IP of the PC), but it's not extensible. 或者您可以尝试绑定到不同的IP地址(一个将使用127.0.0.1,另一个将使用PC的真实IP),但它不可扩展。

If you are going to use this in a LAN setup, then you can always filter the received packet based on the sender's address -- recvmsg() of UDP allows you to retrieve sender's address and port number. 如果要在LAN设置中使用它,则可以始终根据发送方的地址过滤接收的数据包 - UDP的recvmsg()允许您检索发送方的地址和端口号。 If you are going to use this in a WAN setup, then this likely would not be an issue unless you are trying to do multicast. 如果要在WAN设置中使用它,那么除非尝试进行多播,否则这可能不会成为问题。 And, even with multicast, duplciate packets are not send back to the receiver. 而且,即使使用多播,也不会将重复数据包发送回接收方。 You can use IP_MULTICAST_LOOP to disable this option. 您可以使用IP_MULTICAST_LOOP禁用此选项。

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

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