我正在尝试用 C++ 创建一个 TCP 服务器,下面是我当前的代码,遵循 Beej 的教程:
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
int main(){
struct addrinfo hints, *res;
int sockfd;
// first, load up address structs with getaddrinfo():
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
getaddrinfo(NULL, "3490", &hints, &res);
// make a socket:
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
// bind it to the port we passed in to getaddrinfo():
bind(sockfd, res->ai_addr, res->ai_addrlen);
}
我正在使用 g++(在 Windows 上)进行编译,并在标题中出现错误。
gcc 版本 6.3.0。
我查看了ws2tcpip文件,可以明显看到getaddrinfo的定义。我尝试在上面添加以下内容:
#define _WIN32_WINNT 0x0600
但这没有任何作用。为什么这段代码无法编译?
对于未来正在为此苦苦挣扎的任何人:
我问题中的代码有效,只是不适用于我的 gcc 版本。