winsocket c++ 阻塞方式

#include <WinSock2.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")


int main()
{
//加载socket动态库
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2,2);
auto err = WSAStartup(wVersionRequested, &wsaData);
if(err != 0)
{
int c = 0;
}
if(LOBYTE(wsaData.wVersion) != 2 ||
  HIBYTE(wsaData.wVersion) != 2)
{
int c = 0;
}




SOCKET socketSer = socket(AF_INET, SOCK_STREAM, 0);


SOCKADDR_IN serverAddr;
    memset(&serverAddr,0,sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(22000);       
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); /*有IP*/


bind(socketSer,(struct sockaddr *)&serverAddr,sizeof(serverAddr));
listen(socketSer,5);


SOCKADDR_IN  socketClient;
int socketLength = sizeof(SOCKADDR);


while(true)
{
SOCKET Command_Sock = accept(socketSer, (SOCKADDR*)&socketClient,&socketLength);
char data[50];
printf("IP:%s",inet_ntoa(socketClient.sin_addr));
recv(Command_Sock,data,sizeof(data),0);
   printf("%s\n",data);
closesocket(Command_Sock);
//WSACleanup();
}




    WSACleanup();
return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/myt0929/article/details/52350865
文章标签: winsocket
个人分类: winsocket
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭