我是靠谱客的博主 超级大地,最近开发中收集的这篇文章主要介绍[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("%sn", 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

最后

以上就是超级大地为你收集整理的[Windows] Socket Server Failed to bind, error 10048的全部内容,希望文章能够帮你解决[Windows] Socket Server Failed to bind, error 10048所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部