简体   繁体   English

socket()返回-1但是errno 0

[英]socket() return -1 but errno 0

I tried to create a UDP socket on mingw, but socket() returns -1 , with errno = 0 . 我尝试在mingw上创建UDP套接字,但socket()返回-1 ,errno = 0 Strange. 奇怪。 I have included winsock2.h. 我已经包含了winsock2.h。 Initially I had compilation error undefined reference to socket@12 , after setting -lws2_32 and -lwsock32 to Linker Settings of Code::Block, compilation success. 在将-lws2_32-lwsock32设置为Code :: Block的链接器设置,编译成功之后,我最初编译错误undefined reference to socket@12

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
RDF_LOG(kDEBUG, "sockfd %d ", sockfd);
if (sockfd < 0){
    RDF_LOG(kERROR, "ERROR: %s , errno %d\n", strerror(errno), errno);
}

Result --> sockfd -1 ERROR: No error , errno 0 结果 - > sockfd -1错误:没有错误,错误0


OK, I change RDF_LOG to fprintf instead. 好的,我改为将RDF_LOG更改为fprintf。

int tmp = 0;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
tmp = errno;
fprintf(stderr, "sockfd %d ", sockfd);
if (sockfd < 0){
    fprintf(stderr, "socket: %s , errno %d\n", strerror(tmp), tmp);
}

The result returned is, still, --> sockfd -1 socket: No error , errno 0 Is it possible that mingw does not support errno?? 返回的结果仍然是 - > sockfd -1 socket:没有错误,错误0是否有可能mingw不支持errno?

The first thing I would do is this: 我要做的第一件事是:

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
int tmp = errno;
RDF_LOG(kDEBUG, "sockfd %d ", sockfd);
if (sockfd < 0){
    RDF_LOG(kERROR, "ERROR: %s , errno %d\n", strerror(tmp), tmp);
}

I have no idea what RDF_LOG may be doing to the errno variable and this will tell you whether it changes it or not. 我不知道RDF_LOG可能对errno变量做了什么,这会告诉你它是否改变了它。

Another thing to look for is that you have successfully performed your WSAStartup . 另一个需要注意的是您已成功执行WSAStartup The following minimal program should hopefully show you how to do this, and provide a starting point for debugging: 以下最小程序应该有希望向您展示如何执行此操作,并提供调试的起点:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <winsock2.h>

int main (void) {
    WSADATA wsaData;
    int listenFd;

    if (WSAStartup(MAKEWORD(1,1), &wsaData) == SOCKET_ERROR) {
        printf ("Error initialising WSA.\n");
        return -1;
    }

    listenFd = socket (AF_INET, SOCK_STREAM, 0);
    if (listenFd  < 0) {
        printf ("Error %d opening socket.\n", errno);
        return -1;
    }

    return 0;
}

Because you are working with Windows sockets, you will need to use the WSAGetLastError() function to check the error code. 因为您使用的是Windows套接字,所以需要使用WSAGetLastError()函数来检查错误代码。 Windows sockets functions do not set errno, which is why you are seeing errno with a value of 0. Windows套接字函数不设置errno,这就是为什么你看到errno的值为0。

This old MinGW wiki page has a list of differences between UNIX sockets and Windows sockets, mentioning the errno vs WSAGetLastError() difference in item 5: http://oldwiki.mingw.org/index.php/sockets 这个旧的MinGW wiki页面有一个UNIX套接字和Windows套接字之间的差异列表,提到了第5项中的errno vs WSAGetLastError()差异: http//oldwiki.mingw.org/index.php/sockets

函数/宏RDF_LOG可能正在调用正在将errno重置为0的C运行时库中的其他函数。您需要在socket失败后立即捕获errno的值,以使其准确。

Didn't see this mentioned in another answer, but there's another problem with the code above. 在另一个答案中没有看到这个,但上面的代码还有另一个问题。 Winsock socket descriptors are of type SOCKET, which in my MinGW winsock2.h is defined as unsigned int. Winsock套接字描述符的类型为SOCKET,在我的MinGW winsock2.h中定义为unsigned int。

If you are assuming Winsock sockets are of type int like Unix file descriptors, checking for a negative error return status may lead to false error reporting, since Winsock makes no guarantee that a socket descriptor value will map to a positive signed integer value. 如果您假设Winsock套接字类型为类型为Unix文件描述符,则检查否定错误返回状态可能会导致错误报告,因为Winsock不保证套接字描述符值将映射到正有符号整数值。

Winsock functions that return a socket descriptor (socket(), accept(), etc.) instead return SOCKET_INVALID on an error, which is defined as (SOCKET) ~0. 返回套接字描述符(socket(),accept()等)的Winsock函数在错误上返回SOCKET_INVALID,该错误定义为(SOCKET)~0。 As mentioned above, you should then use WSAGetLastError() to get the system error number. 如上所述,您应该使用WSAGetLastError()来获取系统错误号。

https://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms740516%28v=vs.85%29.aspx

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

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