关闭

winsocket c++ 阻塞方式

标签: winsocket
138人阅读 评论(0) 收藏 举报
分类:
#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;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:374次
    • 积分:32
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档