winsock服务端

2021-05-07 21:27

阅读:487

标签:cep   error   end   eof   ror   close   erro   stream   invalid   

#include "winsock2.h"
#pragma comment(lib,"WS2_32.lib")

SOCKET s;
sockaddr_in remoteAddr;
SOCKET client;
int nAddrLen;
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s==INVALID_SOCKET)
{
          return;
}
sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(80);
sin.sin_addr.S_un.S_addr=INADDR_ANY;
if(bind(s,(LPSOCKADDR)&sin,sizeof(sin))==SOCKET_ERROR)
{
          return;
}
if(listen(s,2)==SOCKET_ERROR)
{
          return;
}
nAddrLen=sizeof(remoteAddr);
 
while(true)
{
client=accept(s,(SOCKADDR*)&remoteAddr,&nAddrLen);
if(client==INVALID_SOCKET)
{
          continue;
}
TCHAR buffer[125];
ZeroMemory(&buffer,sizeof(buffer));
wsprintf(buffer,TEXT("端口接收到一个连接:IP:%s"),inet_ntoa(remoteAddr.sin_addr));
//recv
//send
closesocket(client);
}

  

winsock服务端

标签:cep   error   end   eof   ror   close   erro   stream   invalid   

原文地址:http://www.cnblogs.com/hatsusakana/p/7639590.html


评论


亲,登录后才可以留言!