简体   繁体   English

带有c ++和windows API的udp套接字

[英]udp socket with c++ and windows API

I'm writting an UDP server for a game. 我正在为游戏编写UDP服务器。 Do you know if is possible to have multisocket in UDP on one port ? 你知道在一个端口上是否可以在UDP中使用多串口? or I must use dynamic port (one socket = one port) ? 或者我必须使用动态端口(一个插槽=一个端口)?

thanks 谢谢

It make no large sense to create multiple socket on one UDP port. 在一个UDP端口上创建多个套接字没有多大意义。 UDP is not point 2 point protocol like TCP so using one "server" socket bind to specific port you can handle hundreds of clients. UDP不是像TCP那样的点2点协议,所以使用一个“服务器”套接字绑定到特定端口,可以处理数百个客户端。

See Using SO_REUSEADDR... : 请参阅使用SO_REUSEADDR ...

Using SO_REUSEADDR 使用SO_REUSEADDR

The SO_REUSEADDR socket option allows a socket to forcibly bind to a port in use by another socket . SO_REUSEADDR套接字选项允许套接字强制绑定到另一个套接字使用的端口 The second socket calls setsockopt with the optname parameter set to SO_REUSEADDR and the optval parameter set to a boolean value of TRUE before calling bind on the same port as the original socket. 第二个套接字调用setsockopt,其optname参数设置为SO_REUSEADDR,optval参数设置为布尔值TRUE,然后在与原始套接字相同的端口上调用bind。 Once the second socket has successfully bound, the behavior for all sockets bound to that port is indeterminate. 第二个套接字成功绑定后,绑定到该端口的所有套接字的行为都是不确定的。 For example, if all of the sockets on the same port provide TCP service, any incoming TCP connection requests over the port cannot be guaranteed to be handled by the correct socket — the behavior is non-deterministic. 例如,如果同一端口上的所有套接字都提供TCP服务,则无法保证端口上的任何传入TCP连接请求都由正确的套接字处理 - 该行为是不确定的。 A malicious program can use SO_REUSEADDR to forcibly bind sockets already in use for standard network protocol services in order to deny access to those service. 恶意程序可以使用SO_REUSEADDR强制绑定已用于标准网络协议服务的套接字,以拒绝访问这些服务。 No special privileges are required to use this option. 使用此选项无需特殊权限。

Bonus reading: What exactly does SO_REUSEADDR do? 额外阅读: SO_REUSEADDR究竟做了什么? .

Sure you can have multiple UDP sockets on one port if SO_REUSEADDR is specified via setsockopt. 如果通过setsockopt指定SO_REUSEADDR,当然可以在一个端口上有多个UDP套接字。 However, I doubt that what you really need is using one UDP socket to communicate with multiple clients, which is also feasible. 但是,我怀疑你真正需要的是使用一个UDP套接字与多个客户端通信,这也是可行的。 UDP is not connection-oriented, UDP APIs like sendto and recvfrom could distinguish different peers on one socket. UDP不是面向连接的,像sendto和recvfrom这样的UDP API可以区分一个套接字上的不同对等体。

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

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