WORD wVersion=MAKEWORD(2,0);
WSADATA wsData;
int nResult= WSAStartup(wVersion,&wsData);
if(nResult !=0)
{
MessageBox("版本不对","ok",MB_OK);
return;
}
SOCKET sock=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);
if(sock==INVALID_SOCKET)
{
MessageBox("socker err","err",MB_OK);
return;
}
sockaddr_in addr;
addr. sin_family=AF_INET;
addr. sin_port= htons(6000); //保证字节顺序
addr. sin_addr.s_addr= inet_addr("192.168.0.152");
nResult=bind(sock,(sockaddr*)&addr,sizeof(sockaddr));
if(nResult==SOCKET_ERROR)
{
MessageBox("绑定套接字时出错","错误",MB_OK);
return;
}
nResult=listen(sock,5);//最多5个连接
if(nResult==SOCKET_ERROR)
{
MessageBox("listen err","错误",MB_OK);
return;
}
int len;
sockaddr_in addrclient;
SOCKET s_d=accept(sock,(sockaddr*)&addrclient,&(len=sizeof(sockaddr)));
if(s_d==INVALID_SOCKET)
{
MessageBox("accept err","错误",MB_OK);
return;
}
char mess[1000];
nResult =recv(s_d,mess,1000,0);
if(nResult==SOCKET_ERROR)
{
int t=WSAGetLastError();
MessageBox("rexv err","错误",MB_OK);
return;
}
MessageBox("rexv ok","ok",MB_OK);