c++ socket 监听失败
intretVal=0;charbuf[1024]={0};intdwErr=0;WSADATAwsd;SOCKETsTest;SOCKETSClient;if(WSAS...
int retVal =0;
char buf[1024]={0};
int dwErr =0;
WSADATA wsd;
SOCKET sTest;
SOCKET SClient;
if(WSAStartup(MAKEWORD(2,2),&wsd))
{
printf("WSAStartup error" );
}
sTest=socket(AF_INET,SOCK_STREAM,0);
if (sTest==INVALID_SOCKET)
{
printf("Create socket error" );
dwErr = WSAGetLastError();
cout << dwErr << endl;
WSACleanup();
exit(1);
}
SOCKADDR_IN addServer ;
addServer.sin_family=AF_INET;
addServer.sin_port=htons(8888);
addServer.sin_addr.s_addr=inet_addr("192.168.1.8");
retVal = bind(sTest,(LPSOCKADDR)&addServer,sizeof(SOCKADDR_IN));
if(retVal==SOCKET_ERROR)
{
dwErr = WSAGetLastError();
cout << dwErr << endl;
closesocket(sTest);
WSACleanup();
printf("bind socket error\n" );
exit(1);
}
retVal = listen(sTest,1);
if (retVal = SOCKET_ERROR)
{
dwErr = WSAGetLastError();
cout << dwErr << endl;
closesocket(sTest);
WSACleanup();
printf("create listener error\n" );
exit(1);
} 展开
char buf[1024]={0};
int dwErr =0;
WSADATA wsd;
SOCKET sTest;
SOCKET SClient;
if(WSAStartup(MAKEWORD(2,2),&wsd))
{
printf("WSAStartup error" );
}
sTest=socket(AF_INET,SOCK_STREAM,0);
if (sTest==INVALID_SOCKET)
{
printf("Create socket error" );
dwErr = WSAGetLastError();
cout << dwErr << endl;
WSACleanup();
exit(1);
}
SOCKADDR_IN addServer ;
addServer.sin_family=AF_INET;
addServer.sin_port=htons(8888);
addServer.sin_addr.s_addr=inet_addr("192.168.1.8");
retVal = bind(sTest,(LPSOCKADDR)&addServer,sizeof(SOCKADDR_IN));
if(retVal==SOCKET_ERROR)
{
dwErr = WSAGetLastError();
cout << dwErr << endl;
closesocket(sTest);
WSACleanup();
printf("bind socket error\n" );
exit(1);
}
retVal = listen(sTest,1);
if (retVal = SOCKET_ERROR)
{
dwErr = WSAGetLastError();
cout << dwErr << endl;
closesocket(sTest);
WSACleanup();
printf("create listener error\n" );
exit(1);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询