码迷,mamicode.com
首页 > Windows程序 > 详细

winsock服务端

时间:2017-10-09 09:54:36      阅读:202      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!