永发信息网

如果一个tcp服务器一直不调用accept会产生什么后果

答案:1  悬赏:70  手机版
解决时间 2021-11-16 07:20
  • 提问者网友:浪荡绅士
  • 2021-11-15 19:55
如果一个tcp服务器一直不调用accept会产生什么后果
最佳答案
  • 五星知识达人网友:逃夭
  • 2021-11-15 21:34
#ifndef UNICODE
#defineUNICODE
#endif
#include
#include
#include
//NeedtolinkwithWs2_32.lib
#pragmacomment(lib,"Ws2_32.lib")
int wmain(void)
{
WSADATAwsaData ;
intiResult=WSAStartup(MAKEWORd(2,2),&wsaData);
if(iResult!=NO_ERROR)
{
wprintf(L "WSAStartupfailedwitherror:%ld\n",iResult);
return 1 ;
}
SOCKETListenSocket ;
ListenSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(ListenSocket==INVALID_SOCKET)
{
wprintf(L "socketfailedwitherror:%ld\n",WSAGetLastError());
WSACleanup();
return 1 ;
}
sockaddr_inservice ;
service.sin_family=AF_INET ;
service.sin_addr.s_addr=inet_addr("127.0.0.1");
service.sin_port=htons(27015);
if(bind(ListenSocket,
(SOCKADDR*)&service,sizeof(service))==SOCKET_ERROR)
{
wprintf(L"bindfailedwitherror:%ld\n",WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1 ;
}
if(listen(ListenSocket,1)==SOCKET_ERROR)
{
wprintf(L"listenfailedwitherror:%ld\n",WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1 ;
}
SOCKETAcceptSocket ;
wprintf(L "Waitingforclienttoconnect...\n");
//Accepttheconnection.
AcceptSocket=accept(ListenSocket,NULL,NULL);
if(AcceptSocket==INVALID_SOCKET)
{
wprintf(L "acceptfailedwitherror:%ld\n",WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1 ;
}
else
wprintf(L "Clientconnected.\n");
closesocket(ListenSocket);
WSACleanup();
return 0 ;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯