简体   繁体   English

在Winsock2中使用C ++创建套接字时出现问题

[英]Problem creating socket with C++ in winsock2

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. 发生的事情是receive_socket没有绑定,我收到SOCKET_ERROR。 When I debug the program and check receive_socket, it appears to just be garbled crap. 当我调试程序并检查receive_socket时,它似乎只是乱码。 I put a breakpoint on the 'std::map' line. 我在“ std :: map”行上设置了一个断点。 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. 当我进入上述代码的每一行时,即使我使用了进入(F11)的步骤,调试光标也会直接从“无符号短端口”行跳到第一条“ local_address.sin”行,但它不会停止在struct,int,char或SOCKET行,它直接跳过它们。

At this point I hover my mouse over local_address, from_address, result, buffer and receive_socket. 此时,我将鼠标悬停在local_address,from_address,结果,buffer和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? 我还注意到,当我到达以上代码的底部时,local_address.sin_port设置为19992,但是应该为6222?

Edit: here is my binding code which is failing because the if statement is true: 编辑:这是我的绑定代码失败,因为if语句为true:

if(bind( receive_socket, (SOCKADDR*) &local_address, sizeof(local_address)) == SOCKET_ERROR) 
    {
        closesocket(receive_socket);
        return 1;
    }

I figured out the answer! 我想出了答案! The problem was I was not calling WSAStartup anywhere in my program. 问题是我没有在程序的任何地方调用WSAStartup。 The following code at the beginning fixed it: 开头的以下代码对其进行了修复:

WSADATA wsaData;
    if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) 
    {
        return 1; 
    }

I found this out by getting the error number from WSAGetLastError() and looking it up on msdn. 我是通过从WSAGetLastError()获取错误号并在msdn上查找错误号来发现的。

Try changing SOCK_DGRAM to SOCK_STREAM 尝试将SOCK_DGRAM更改为SOCK_STREAM

According to MSDN, 根据MSDN,

SOCK_STREAM - A socket type that provides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. SOCK_STREAM-一种套接字类型,可通过OOB数据传输机制提供顺序的,可靠的,双向的,基于连接的字节流。 This socket type uses the Transmission Control Protocol (TCP) for the Internet address family (AF_INET or AF_INET6). 此套接字类型将传输控制协议(TCP)用于Internet地址系列(AF_INET或AF_INET6)。

SOCK_DGRAM - A socket type that supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. SOCK_DGRAM-支持数据报的套接字类型,这些数据报是无连接的,不可靠的最大长度固定(通常很小)的缓冲区。 This socket type uses the User Datagram Protocol (UDP) for the Internet address family (AF_INET or AF_INET6). 此套接字类型对Internet地址系列(AF_INET或AF_INET6)使用用户数据报协议(UDP)。

And as far as the port goes... 就港口而言...

local_address.sin_port is set to 19992, but it should be 6222? local_address.sin_port设置为19992,但应为6222?

htons converts a port number in host byte order to network byte order (see here ) htons将主机号顺序中的端口号转换为网络字节顺序(请参阅此处

local_address.sin_port = htons(port);

I found that rather weird. 我发现那很奇怪。 Also, why htons() the port? 另外,为什么要使用htons()端口? That makes no sense. 这是没有意义的。 Couldn't you just use getaddrinfo() or something like that, or does winsock require to manually fill in info? 您不能只使用getaddrinfo()或类似的东西,还是winsock需要手动填写信息吗?

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

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