SO_REUSEADDR and UDP behavior in Windows

前端 未结 1 480
余生分开走
余生分开走 2021-01-14 10:17

I\'m aware that using SO_REUSEADDR with UDP in a *NIX environment, behaves like a multicast, where multiple clients bound to the same port can listen and receive broadcast d

相关标签:
1条回答
  • 2021-01-14 11:02

    Multiple UDP sockets on Windows bound to the same port will all receive broadcast packets together.

    Here's a demo program you can build for windows and Linux with GCC and test with Netcat as mentioned. In both systems, only one socket (either A or B) receives each datagram when a unicast address is used as the target. If a broadcast address is used then both sockets will receive the message.

    /* Tested on linux and windows 7.
     * On windows use mingw-gcc:
     *    gcc -Wall -g -o udplisten udplisten.c -lws2_32
     * Test with:
     *    echo hello | netcat -u machinename 9898 (unicast)
     *    echo hello | netcat -u 172.16.255.255 9898 (broadcast)
     */
    #ifdef WIN32
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #else
    #include <sys/select.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #define SOCKET int
    #define INVALID_SOCKET -1
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    
    #undef max
    #define max(x,y) ((x) > (y) ? (x) : (y))
    
    static void
    die(const char *str)
    {
        perror(str);
        exit(1);
    }
    
    static SOCKET
    mksocket(struct sockaddr_in *addr)
    {
        SOCKET sock = INVALID_SOCKET;
        int opt = 1;
        if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
            die("socket");
        if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof(opt)) < 0)
            die("setsockopt");
        if (bind(sock, (struct sockaddr *)addr, sizeof(struct sockaddr_in)) < 0)
            die("bind");
        return sock;
    }
    
    static void
    process(SOCKET sock, const char *label)
    {
        char buffer[8192];
        struct sockaddr_in caddr;
        socklen_t caddr_size = sizeof(caddr);
        memset(&caddr, 0, caddr_size);
        int count = recvfrom(sock, buffer, sizeof(buffer), 0,
                             (struct sockaddr *)&caddr, &caddr_size);
        if (count < 0) die(label);
        printf("%s %d '", label, count);
        fwrite(buffer, 1, count, stdout);
        printf("'\n");
    }
    
    int
    main(int argc, char *argv[])
    {
        struct sockaddr_in addr;
        SOCKET socka = INVALID_SOCKET, sockb = INVALID_SOCKET;
        fd_set read_set;
    #ifdef WIN32
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2,2), &wsaData))
            return -1;
    #endif
        addr.sin_family = AF_INET;
        addr.sin_port = htons(9898);
        addr.sin_addr.s_addr = INADDR_ANY;
    
        socka = mksocket(&addr);
        sockb = mksocket(&addr);
    
        for (;;) {
            FD_ZERO(&read_set);
            FD_SET(socka, &read_set);
            FD_SET(sockb, &read_set);
            if (select(max(socka,sockb)+1, &read_set, NULL, NULL, NULL) < 0)
                die("select");
            if (FD_ISSET(socka, &read_set))
                process(socka, "A");
            if (FD_ISSET(sockb, &read_set))
                process(sockb, "B");
        }
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题