[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\n", m);
            freeaddrinfo(result);
            closesocket(ListenSocket);
            WSACleanup();
            return -1;
        }

 

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

转载于:https://www.cnblogs.com/liujx2019/p/10811330.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值