Home > OS >  "Error Setting TCP Socket Options" error with setsockopt()
"Error Setting TCP Socket Options" error with setsockopt()

Time:08-01

This snippet is taken from server.c, which is supposed to be receiving data from a client machine:

int main() {
    int sock, client_socket;
    char buffer[1024];
    char response[18384];
    struct sockaddr_in server_address, client_address;
    int i = 0;
    int optval = 1;
    socklen_t client_length;

    sock = socket(AF_INET, SOCK_STREAM, 0);

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval)) < 0) {
        printf("Error Setting TCP Socket Options!\n");
        return 1;
    }

    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = inet_addr("10.0.0.188");
    server_address.sin_port = htons(12345);

    bind(sock, (struct sockaddr *) &server_address, sizeof(server_address));
    listen(sock, 5);
    client_length = sizeof(client_address);
    client_socket = accept(sock, (struct sockaddr *) &client_address, &client_length);

When I compile and run this on Kali Linux, it sets the TCP socket fine and works, but when I compile and run this on Windows with gcc gcc server.c -o server.exe -lws2_32, I get the error message from setsockopt():

Error Setting TCP Socket Options!

Below this snippet is the bind() and listen() function, along with if/else block deciding what to do with the data received from the client.

CodePudding user response:

On Windows, you must call WSAStartup() before using any other socket functions (and call WSACleanup() when you are done using the Winsock library before exiting). Other platforms don't have similar functions to initialize/cleanup their socket libraries.

And, you should always check the return values of socket functions (or any system call) for failure, and use WSAGetLastError() (errno on other platforms) to retrieve error codes to tel, you why the functions failed.

You are not checking if socket() fails (which would report a WSANOTINITIALISED error), so you are then passing an invalid socket to setsockopt() (which would report a WSAENOTSOCK error).

Try this:

int main() {
    WSADATA wsa;
    SOCKET sock, ...;
    ...
    BOOL optval = TRUE;
    ...

    int err = WSAStartup(MAKEWORD(2,0), &wsa);
    if (err != 0) {
        printf("Error Initializing WinSock! Error: %d\n", err);
        return 1;
    }

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == INVALID_SOCKET) {
        err = WSAGetLastError();
        printf("Error Creating TCP Socket! Error: %d\n", err);
        WSACleanup();
        return 1;
    }

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &optval, sizeof(optval)) < 0) {
        err = WSAGetLastError();
        printf("Error Setting TCP Socket Options! Error: %d\n", err);
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    ...
  • Related