使用 C++ 创建套接字时出现问题在winsock2中
我遇到了最奇怪的问题,让我头疼。考虑以下代码:
// Create and bind socket
std::map<Connection, bool> clients;
unsigned short port=6222;
struct sockaddr_in local_address, from_address;
int result;
char buffer[10000];
SOCKET receive_socket;
local_address.sin_family = AF_INET;
local_address.sin_addr.s_addr = INADDR_ANY;
local_address.sin_port = htons(port);
receive_socket = socket(AF_INET,SOCK_DGRAM,0);
发生的情况是 receive_socket 未绑定,我收到 SOCKET_ERROR。当我调试程序并检查 receive_socket 时,它似乎只是乱码。我在“std::map”行上放置了一个断点。当我单步执行上述代码的每一行时,调试光标直接从“无符号短端口”行跳到第一个“local_address.sin”行,即使我使用单步执行(F11),它也不会停止在struct、int、char 或 SOCKET 行,它会直接跳过它们。
此时,我将鼠标悬停在 local_address、from_address、result、buffer 和 receive_socket 上。它们全都是乱码。这是因为我还没有定义这些变量吗?我还注意到,当我到达上面代码的底部时,local_address.sin_port 设置为 19992,但它应该是 6222?
编辑:这是我的绑定代码,它失败了,因为 if 语句为 true:
if(bind( receive_socket, (SOCKADDR*) &local_address, sizeof(local_address)) == SOCKET_ERROR)
{
closesocket(receive_socket);
return 1;
}
I'm having the weirdest problem causing me headaches. Consider the following code:
// Create and bind socket
std::map<Connection, bool> clients;
unsigned short port=6222;
struct sockaddr_in local_address, from_address;
int result;
char buffer[10000];
SOCKET receive_socket;
local_address.sin_family = AF_INET;
local_address.sin_addr.s_addr = INADDR_ANY;
local_address.sin_port = htons(port);
receive_socket = socket(AF_INET,SOCK_DGRAM,0);
What's happening is receive_socket is not binding, I get SOCKET_ERROR. When I debug the program and check receive_socket, it appears to just be garbled crap. I put a breakpoint on the 'std::map' line. When I step into each line of the above code, the debug cursor jumps straight from the 'unsigned short port' line to the first 'local_address.sin' line, even though I am using step into (F11), it does not stop at struct, int, char or SOCKET lines, it jumps straight over them.
At this point I hover my mouse over local_address, from_address, result, buffer and receive_socket. They are all full of garbled crap. Is this because I have not defined these variables yet? I've also noticed that when I reach the bottom of the above code, local_address.sin_port is set to 19992, but it should be 6222?
Edit: here is my binding code which is failing because the if statement is true:
if(bind( receive_socket, (SOCKADDR*) &local_address, sizeof(local_address)) == SOCKET_ERROR)
{
closesocket(receive_socket);
return 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到答案了!问题是我没有在程序中的任何地方调用 WSAStartup 。下面的代码在开头修复了这个问题:
我通过从 WSAGetLastError() 获取错误号并在 msdn 上查找来发现了这一点。
I figured out the answer! The problem was I was not calling WSAStartup anywhere in my program. The following code at the beginning fixed it:
I found this out by getting the error number from WSAGetLastError() and looking it up on msdn.
尝试将
SOCK_DGRAM
更改为SOCK_STREAM
根据 MSDN,
就港口而言...
htons
将主机字节顺序的端口号转换为网络字节顺序(请参阅 此处)Try changing
SOCK_DGRAM
toSOCK_STREAM
According to MSDN,
And as far as the port goes...
htons
converts a port number in host byte order to network byte order (see here)我发现这很奇怪。另外,为什么 htons() 端口?这没有任何意义。难道你不能只使用 getaddrinfo() 或类似的东西吗?或者 winsock 是否需要手动填写信息?
I found that rather weird. Also, why htons() the port? That makes no sense. Couldn't you just use getaddrinfo() or something like that, or does winsock require to manually fill in info?