Search code examples
clinuxsocketsunixtcp

How to disable Nagle algorithm on Unix Socket in C language?


I want to disable Nagle's Algorithm in my Unix Socket, is it possible?

Because all i can find in tutorials for disabling it is just for TCP socket. This is my unix socket initialization code.

    struct sockaddr_un local, remote;
    u8 str[0xFFFF];
    struct client_info cl;

    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    {
        perror("socket");
        exit(1);
    }

    local.sun_family = AF_UNIX;
    strcpy(local.sun_path, SOCK_PATH);
    unlink(local.sun_path);
    len = strlen(local.sun_path) + sizeof(local.sun_family);
    if (bind(s, (struct sockaddr *)&local, len) == -1)
    {
        perror("bind");
        exit(1);
    }

    if (listen(s, 5) == -1)
    {
        perror("listen");
        exit(1);
    }

And here is the code that i found to disable nagle algorithm, but i don't have any clue about the parameter to apply it for my unix socket.

int result = setsockopt(sock,            /* socket affected */
                                 IPPROTO_TCP,     /* set option at TCP level */
                                 TCP_NODELAY,     /* name of option */
                                 (char *) &flag,  /* the cast is historical
                                                         cruft */
                                 sizeof(int));    /* length of option value */
         if (result < 0)
         // ...

Solution

  • The Nagle algorithm is a TCP/IP specific algorithm used to reduce packet overhead. There is no TCP/IP involved in a UNIX socket and thus the Nagle algorithm cannot be applied there. Which also means that it cannot be disabled.