简体   繁体   English

在Linux中创建套接字时获取错误代码

[英]Getting error code when creating a socket in Linux

I'm doing some socket programming in Linux and am wondering how to get the error code when the function socket(...); 我正在Linux中进行一些套接字编程,我想知道如何在函数套接字(...)时获取错误代码; fails. 失败。

for example for the "getaddrinfo" function i can do this: 例如,对于“getaddrinfo”函数,我可以这样做:

//Resolve the server address and port
    result = (struct addrinfo *) calloc(1, sizeof(struct addrinfo));
    iResult = getaddrinfo("google.com", DEFAULT_PORT, &hints, &result);
    if (iResult != 0){
        printf("%d\n", iResult);
        fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(iResult));
        getchar();
        exit(EXIT_FAILURE);
    }

However I want to do a similar thing using socket(...) function. 但是我想使用socket(...)函数做类似的事情。

According to this: http://linux.die.net/man/2/socket 根据这个: http//linux.die.net/man/2/socket

the function returns -1 on failure, and sets errno to the appropriate error number. 函数在失败时返回-1,并将errno设置为适当的错误号。 How do i access this "errno" though? 我怎么访问这个“错误”? This is my code so far: 到目前为止这是我的代码:

int connectSocket = 0;
connectSocket = socket(AF_INET, SOCK_STREAM, 0);
printf("%d\n", connectSocket);

if (connectSocket == -1){
    printf("socket failed with error: %s\n", error_string); //TODO: HELP DECLARING error_string
    getchar();
    exit(EXIT_FAILURE);
}

errno is a thread-local global variable, defined in <errno.h> . errno是一个线程局部全局变量,在<errno.h>定义。 The man page for many library functions will indicate that they return -1 on error, and set errno . 许多库函数的手册页将指示它们在出错时返回-1,并设置errno

You can convert an errno value to a useful string with the strerror function. 您可以使用strerror函数将errno值转换为有用的字符串。

In general, you should code like this: 通常,您应该像这样编码:

#include <stdio.h>
#include <errno.h>

int main(void) {
    int s;

    s = socket(...);
    if (s < 0) {
        fprintf(stderr, "socket() failed: %s\n", strerror(errno));
        exit(1);
    }
}

Alternatively, glibc's printf and friends support a %m format specifier, which is replaced with strerror(errno) (no argument is needed). 或者,glibc的printf和朋友支持%m格式说明符,它被替换为strerror(errno) (不需要参数)。 So the above example could be replaced with: 所以上面的例子可以替换为:

    if (s < 0) {
        fprintf(stderr, "socket() failed: %m\n");
        exit(1);
    }

And to make it all simpler, there is the perror function, which prints out a message similar to above. 为了使它更简单,有perror函数,它打印出类似于上面的消息。

    if (s < 0) {
        perror("socket");
        exit(1);
    }

Wrapping it all up - error handling need not be complex and verbose. 把它全部包装好 - 错误处理不需要复杂和冗长。 Putting the socket call and the test for < 0 in one statement, the above code could look like this, and you'll be a real UNIX pro: 将套接字调用和< 0的测试放在一个语句中,上面的代码看起来像这样,你将是一个真正的UNIX专家:

#include <stdio.h>
#include <errno.h>

int main(void) {
    int s;

    if ((s = socket(...)) < 0) {
        perror("socket");
        exit(1);
    }
}

Add #include <errno.h> and you'll be able to read the global errno variable. 添加#include <errno.h> ,您将能够读取全局errno变量。

connectSocket = socket(AF_INET, SOCK_STREAM, 0);
if (connectSocket < 0) {
    if (errno == EACCESS) ...

You can use perror in stdio.h to print an error message based on the value of errno or you can use strerror in string.h to access a string describing the error code 您可以在stdio.h使用perror根据errno的值打印错误消息,或者您可以在string.h使用strerror来访问描述错误代码的字符串

connectSocket = socket(AF_INET, SOCK_STREAM, 0);
if (connectSocket < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
}

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

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