Socket error 10053,10054究竟是怎么引起的

 我来答
雨过天晴日丶
2016-03-10 · TA获得超过2636个赞
知道大有可为答主
回答量:1879
采纳率:91%
帮助的人:1665万
展开全部
WORD VersionRequested;
WSADATA WsaData;

VersionRequested = MAKEWORD(2, 2);

if (WSAStartup(VersionRequested, &WsaData))
return -1;

SOCKET SocketServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN AddrServer;
AddrServer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
AddrServer.sin_port = htons(9999);
AddrServer.sin_family = AF_INET;

// 连接服务器
if (0 != connect(SocketServer, (SOCKADDR *)&AddrServer, sizeof(SOCKADDR)))
{
closesocket(SocketServer);
return false;
}

int err = 0;
char temp[] = "hello, server";
int rs = send(SocketServer, temp, sizeof(temp) + 1, 0);

char buff[1024] = {0};
rs = recv(SocketServer, buff, sizeof(buff), 0);

// 下面这2句代码如果注释掉,后面的recv就能正解的返回0。如果不注释掉,recv就会返回-1,
// 并得到10053这个错误
rs = send(SocketServer, temp, sizeof(temp) + 1, 0);
err = WSAGetLastError();

rs = recv(SocketServer, buff, sizeof(buff), 0);
err = WSAGetLastError();

system("pause");
return 0;
这个是服务器的:
[cpp] view plaincopy
WORD VersionRequested;
WSADATA WsaData;

VersionRequested = MAKEWORD(2, 2);

if (WSAStartup(VersionRequested, &WsaData))
{
printf("加载socket库失败!\n");
return -1;
}

// 监听来自浏览器的请求
SOCKET SockServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN AddrClient;
AddrClient.sin_family = AF_INET;
AddrClient.sin_addr.s_addr = INADDR_ANY;
AddrClient.sin_port = htons(9999);

if (SockServer == INVALID_SOCKET)
{
printf("socket初始化失败!\n");
closesocket(SockServer);
WSACleanup();
return -1;
}

if (bind(SockServer, (sockaddr*)&AddrClient, sizeof(AddrClient)) != 0)
{
printf("socket绑定失败!\n");
closesocket(SockServer);
WSACleanup();
return -1;
}

if (listen(SockServer, 10) != 0)
{
printf("socket监听失败!\n");
closesocket(SockServer);
WSACleanup();
return -1;
}

while (1)
{
SOCKET NewSocket = accept(SockServer, NULL, NULL);
if (INVALID_SOCKET == NewSocket)
{
closesocket(NewSocket);
continue;
}
else
{
struct linger so_linger;
so_linger.l_onoff = 1;
so_linger.l_linger = 30;
setsockopt(NewSocket ,SOL_SOCKET, SO_LINGER, (const char*)&so_linger, sizeof(so_linger));
}

char buff[1024];
int rs = recv(NewSocket, buff, sizeof(buff), 0);

char temp[] = "hello, client";
rs = send(NewSocket, temp, sizeof(temp) + 1, 0);

closesocket(NewSocket);
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式