Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error code when creating a socket in Linux

Tags:

c

linux

sockets

I'm doing some socket programming in Linux and am wondering how to get the error code when the function socket(...); fails.

for example for the "getaddrinfo" function i can do this:

//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.

According to this: http://linux.die.net/man/2/socket

the function returns -1 on failure, and sets errno to the appropriate error number. 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);
}
like image 213
Yahya Uddin Avatar asked Nov 24 '14 02:11

Yahya Uddin


People also ask

Why do we get socket error?

The client and server can now communicate by writing to or reading from their sockets. A socket error can occur if one or more of the above conditions are not met or something is blocking communication between the client and server (e.g., firewall, anti-virus).

How do you find socket errors?

On Windows, you can retrieve the last error by calling the WSAGetLastError() function. Since your program will almost certainly need to check for errors multiple times, I suggest you wrap these methods in a macro. The following code defines a macro called GETSOCKETERRNO() which will return the last error code.

What is socket return error?

socket() on Linux and macOS On unix-like platforms (Berkeley sockets), socket() returns an int . This return value will be 0 or positive if the call succeeded, and it will be negative if it failed.


1 Answers

errno is a thread-local global variable, defined in <errno.h>. The man page for many library functions will indicate that they return -1 on error, and set errno.

You can convert an errno value to a useful string with the strerror function.

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). 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.

    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:

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

int main(void) {
    int s;

    if ((s = socket(...)) < 0) {
        perror("socket");
        exit(1);
    }
}
like image 156
Jonathon Reinhart Avatar answered Oct 10 '22 17:10

Jonathon Reinhart