windows下网络编程(一)——SDK socket

 

客户端代码

#include 
#pragma comment(lib, "ws2_32.lib") 

int _tmain(int argc, _TCHAR* argv[])
{
	WSADATA wsaData;
	SOCKET client;

	int port = 5000;
	char buf[24];
	memset(buf, 0, sizeof(buf));

	struct sockaddr_in serv;  //服务器端地址

	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
	{
		printf("winsock load failed\n");
		return 0;
	}

	serv.sin_family = AF_INET;
	serv.sin_port = htons(port);
	serv.sin_addr.s_addr = inet_addr("127.0.0.1");//htonl(INADDR_ANY);

	client = socket(AF_INET, SOCK_STREAM,0);  //建立客户端流套接字
	if (client == INVALID_SOCKET)
	{
		printf("socket failed:%d\n", WSAGetLastError());
		return 0;
	}
	if (connect(client, (struct sockaddr*)&serv,sizeof(serv)) == INVALID_SOCKET)
	{
		printf("connect failed:%d\n", WSAGetLastError());
		return 0;
	}
	else
	{
		while(1)
		{
			/*int iLen = recv(client, buf, sizeof(buf), 0);
			if (iLen == 0)
			{
				return 0;
			}
			else if (iLen == SOCKET_ERROR)
			{
				printf("recv failed:%d\n", WSAGetLastError());
				return 0;
			}
			printf("recv data from server:%s\n",buf);*/
			memset(buf,0,sizeof(buf));
			std::cin>>buf;
			int iSend = send(client,buf,sizeof(buf),0);
			if (iSend == SOCKET_ERROR)
			{
				printf("send failed:%d\n",WSAGetLastError());
				break;
			} 
			else if(iSend == 0)
			{
				break;
			}
			else
			{
				printf("send byte :%d\n",iSend);
			}
		}
	}
	closesocket(client);
	WSACleanup();

	system("pause");

	return 0;
}

 

服务端代码

int _tmain(int argc, _TCHAR* argv[])
{
	WSADATA wssaData;
	SOCKET sListen, sAccept;

	unsigned int uiAddressLen = 0;
	int iSend = 0;
	char buf[] = "hello, how are you?";

	struct sockaddr_in serv, client; //用于指定ip和port
	if (WSAStartup(MAKEWORD(2,2), &wssaData) != 0)
	{
		printf("winsocket load failed!\n");
		return 0;
	}

	sListen = socket(AF_INET, SOCK_STREAM, 0);
	if (sListen == INVALID_SOCKET)
	{
		printf("socket failed:%d\n",WSAGetLastError());
		return 0;
	}

	serv.sin_family = AF_INET;
	serv.sin_port = htons(5000);
	serv.sin_addr.s_addr = htonl(INADDR_ANY);//inet_addr("127.0.0.1");

	if (bind(sListen, (LPSOCKADDR)&serv, sizeof(serv)) == SOCKET_ERROR)
	{
		printf("bind failed:%d \n",WSAGetLastError());
		return 0;
	}

	if (listen(sListen,5) == SOCKET_ERROR)
	{
		printf("listen failed:%d \n",WSAGetLastError());
		return 0;
	}

	int  iLen  = sizeof(client);

	sAccept = accept(sListen, (struct sockaddr*)&client, &iLen);
	if (sAccept == INVALID_SOCKET)
	{
		printf("accept failed:%d\n",WSAGetLastError());
		//break;
	}

	printf("accepted client ip:[%s],port:[%d]\n",inet_ntoa(client.sin_addr), ntohs(client.sin_port));

	while (1)
	{
		

		/*int iSend = send(sAccept, buf,sizeof(buf),0);
		if (iSend == SOCKET_ERROR)
		{
			printf("send failed:%d\n",WSAGetLastError());
			break;
		} 
		else if(iSend == 0)
		{
			break;
		}
		else
		{
			printf("send byte :%d\n",iSend);
		}*/

		int iRecv = recv(sAccept,buf,sizeof(buf),0);
		if (iRecv == SOCKET_ERROR)
		{
			printf("recv failed:%d\n", WSAGetLastError());
			break;
		} 
		else if(iRecv == 0)
		{
			break;
		}
		else
		{
			printf("recv byte:%d,%s\n",iRecv,buf);
		}

		//closesocket(sAccept);
	}
	closesocket(sListen);
	WSACleanup();
	return 0;
}



 

你可能感兴趣的:(windows下网络编程(一)——SDK socket)