我用 C 使用 BSD 套接字制作了一个基本的客户端和服务器。我能够绑定(bind)到 65535 以上的端口号,并且客户端可以连接到它。我在这里做错了什么?
代码:
int port = 999999;
...
serverAddr.sin_port = htons(port);
最佳答案
因为 htons 接受一个 16 位参数,它的输入将被截断。
试试看你会得到什么:
#include <stdio.h>
#include <arpa/inet.h>
int
main(int argc, char **argv)
{
int foo = 999999;
int bar = ntohs(htons(foo));
printf("%d\n", bar);
return 0;
}
关于c - 为什么我可以绑定(bind)到 C 中的(看似)无效端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13025397/