WinSock2 是否有 API 来定义客户端使用哪个 TCP 端口来 connect() 到服务器?
我们需要 connect() 到 UNIX 服务器,该服务器仅在客户端 TCP 端口为 < 1024 (Reserved ports, which are accessible only to UNIX user "root", are in the range 1 to 1023. Non-reserved are in the range 1024 to 65535, read https://www.beyondtrust.com/docs/privilege-management/unix-linux/admin/settings/port-usage 时响应。嗯)。
参见上面,如果我们使用普通的 WinSock2 API,我们会得到一个随机的 TCP 端口号,如果它高于 1023,服务器将不会响应。
只需将客户端套接字
bind()
连接到所需的本地IP/端口,然后再connect()
将其连接到服务器。