C++ 网络编程客户端

#include<WinSock2.h>
#include<stdio.h>
#pragma comment(lib,"ws2_32.lib")
void main()
{
	//加载套接字库
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;
	wVersionRequested=MAKEWORD(1,1);
	err=WSAStartup(wVersionRequested,&wsaData);
	if(err!=0)
	{
		return;
	}
	if(LOBYTE(wsaData.wVersion)!=1||HIBYTE(wsaData.wVersion)!=1)
	{
		WSACleanup();
		return;
	}
	SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);

	SOCKADDR_IN addSrv;
	addSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
	addSrv.sin_family=AF_INET;
	addSrv.sin_port=htons(6000);
	//向服务器发出连接请求
	connect(sockClient,(SOCKADDR *)&addSrv,sizeof(SOCKADDR));

	//接收数据
	char recvBuf[100];
	recv(sockClient,recvBuf,100,0);
	printf("%s\n",recvBuf);
	//发送数据
	send(sockClient,"This is lisi",strlen("This is lisi")+1,0);
	//关闭套接字
	closesocket(sockClient);
	WSACleanup();
	system("pause");
}

你可能感兴趣的:(网络编程,客户端)