zoukankan      html  css  js  c++  java
  • [Windows] Socket Server Failed to bind, error 10048

    Address already in use.


    Typically, only one usage of each socket address (protocol/IP address/port) is permitted.

    This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that wasn't closed properly, or one that is still in the process of closing.

    For server applications that need to bind multiple sockets to the same port number, 例如client进程被taskkill,再次启动的情况,consider using setsockopt(SO_REUSEADDR).

    Client applications usually need not call bind at all—connect chooses an unused port automatically. When bind is called with a wildcard address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is committed. This could happen with a call to another function later, including connect, listen, WSAConnect, or WSAJoinLeaf.

            BOOL bOptVal = TRUE;
            int bOptLen = sizeof(BOOL);
            iResult = setsockopt(ListenSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&bOptVal, bOptLen);
            if (iResult == SOCKET_ERROR)
            {
                char m[500];
                snprintf(m, 500, "%s %d: setsockopt failed, port %d failed with error: %d",
                    __func__, __LINE__, port_number, WSAGetLastError());
                printf("%s
    ", m);
                freeaddrinfo(result);
                closesocket(ListenSocket);
                WSACleanup();
                return -1;
            }

    https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-setsockopt

  • 相关阅读:
    【sqlserver】sqlserver表中导入大批量数据
    java中json结果检查
    spoj375Query on a tree树链剖分
    Hdu4737 ( A Bit Fun ) 线段树
    CodeForces 274E. Riding in a LiftDp
    D. Red-Green Towers Dp
    Hdu5067旅行商
    Hdu5068线段树
    uva11992线段树
    uva1400线段树
  • 原文地址:https://www.cnblogs.com/liujx2019/p/10811330.html
Copyright © 2011-2022 走看看