简单的Windows Socket编程实例

Server(多线程)

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

using namespace std;

#define PORT 4000
#define IP_ADDRESS "127.0.0.1"
DWORD WINAPI ClientThread(LPVOID lpParameter)
{
	SOCKET ClientSocket=(SOCKET)lpParameter;
	int ret=0;
	char RecvBuffer[MAX_PATH];
	while(true)
	{
		memset(RecvBuffer,0x00,sizeof(RecvBuffer));
		ret=recv(ClientSocket,RecvBuffer,MAX_PATH,0);
		if(ret==0||ret==SOCKET_ERROR)
		{
			cout<<"Exit"<<endl;
			break;
		}
		cout<<"收到的信息为:"<<RecvBuffer<<endl;
	}
	return 0;
}

int main()
{
	WSADATA Ws;
	SOCKET ServerSocket,ClientSocket;
	struct sockaddr_in LocalAddr,ClientAddr;
	int ret=0;
	int AddrLen=0;
	HANDLE hThread=NULL;

	//Init
	if((WSAStartup(MAKEWORD(2,2),&Ws)!=0))
	{
		cout<<"Init Failed:"<<GetLastError()<<endl;
		return -1;
	}

	//Create Socket
	ServerSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(ServerSocket==INVALID_SOCKET)
	{
		cout<<"Create Socket Failed:"<<GetLastError()<<endl;
		return -1;
	}

	LocalAddr.sin_family=AF_INET;
	LocalAddr.sin_addr.S_un.S_addr=inet_addr(IP_ADDRESS);
	LocalAddr.sin_port=htons(PORT);
	memset(LocalAddr.sin_zero,0x00,8);

	ret=bind(ServerSocket,(struct sockaddr *)&LocalAddr,sizeof(LocalAddr));

	if(ret!=0)
	{
		cout<<"Bind Socket Failed:"<<GetLastError()<<endl;
		return -1;
	}

	ret=listen(ServerSocket,10);

	if(ret!=0)
	{
		cout<<"Listen Socket Failed"<<GetLastError()<<endl;
		return -1;
	}
	
	cout<<"Server Start"<<endl;

	while(true)
	{
		AddrLen=sizeof(ClientAddr);
		ClientSocket=accept(ServerSocket,(struct sockaddr *)&ClientAddr,&AddrLen);
		if(ClientSocket==INVALID_SOCKET)
		{
			cout<<"Accept Failed:"<<GetLastError()<<endl;
			break;
		}
		cout<<"客户端连接::"<<inet_ntoa(ClientAddr.sin_addr)<<":"<<ClientAddr.sin_port<<endl;

		hThread=CreateThread(NULL,0,ClientThread,(LPVOID)ClientSocket,0,NULL);
		if(hThread==NULL)
		{
			cout<<"Create Thread Failed"<<endl;
			break;
		}
		CloseHandle(hThread);
	}
	closesocket(ServerSocket);
	closesocket(ClientSocket);
	WSACleanup();
	return 0;
}


Client

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

using namespace std;

#define PORT 4000
#define IP_ADDRESS "127.0.0.1"
int main()
{
	WSADATA Ws;
	SOCKET ClientSocket;
	struct sockaddr_in ServerAddr;
	int ret=0;
	int AddrLen=0;
	//HANDLE hThread=NULL;
	char SendBuffer[MAX_PATH];

	//Init
	if(WSAStartup(MAKEWORD(2,2),&Ws)!=0)
	{
		cout<<"Init Failed"<<endl;
		return -1;
	}

	//Create
	ClientSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(ClientSocket==INVALID_SOCKET)
	{
		cout<<"Socket Failed:"<<GetLastError()<<endl;
		return -1;
	}
	ServerAddr.sin_family=AF_INET;
	ServerAddr.sin_addr.S_un.S_addr=inet_addr(IP_ADDRESS);
	ServerAddr.sin_port=htons(PORT);
	memset(ServerAddr.sin_zero,0x00,8);
	ret=connect(ClientSocket,(struct sockaddr *)&ServerAddr,sizeof(ServerAddr));
	if(ret==SOCKET_ERROR)
	{
		cout<<"Connect Error:"<<GetLastError()<<endl;
		return -1;
	}
	else
	{
		cout<<"Connect Success"<<endl;
	}

	while(true)
	{
		cin.getline(SendBuffer,sizeof(SendBuffer));
		ret=send(ClientSocket,SendBuffer,(int)strlen(SendBuffer),0);
		if(ret==SOCKET_ERROR)
		{
			cout<<"Send Error:"<<GetLastError()<<endl;
			break;
		}
	}
	closesocket(ClientSocket);
	WSACleanup();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值