基于c++的C/S网络通信 从入门到精通(四)

为了实现一个服务器与多个客户端的通讯,需要再引入2个线程,一个用来连接客户端,一个用来释放客户端的连接

由于使用阻塞模式,所以需要设置接收超时,以便随时接受客户端的信息,不然需要客户端顺序发送信息

 

客户端代码如下:没有改变

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

using namespace std;

SOCKET clientsocket;
HANDLE hMutex;//互斥使用输出窗体
int flag_connect = 1;

DWORD WINAPI Fun_Recv(LPVOID);
DWORD WINAPI Fun_Send(LPVOID);

int main()
{
	//加载套接字库
	WSADATA wsadata;
	int iRet = WSAStartup(MAKEWORD(2, 2), &wsadata);
	if (iRet != 0)
	{
		cout << "套接字库加载失败" << endl;
		return -1;
	}
	else
	{
		cout << "套接字库加载成功" << endl;
	}
	//创建套接字
	clientsocket = socket(AF_INET, SOCK_STREAM, 0);
	if (clientsocket == INVALID_SOCKET)
	{
		cout << "套接字创建失败" << endl;
		return -1;
	}
	else
	{
		cout << "套接字创建成功" << endl;
	}

	//初始化客户端地址族变量
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	addrSrv.sin_port = htons(8000);

	//连接
	iRet = connect(clientsocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
	if (iRet == SOCKET_ERROR)
	{
		cout << "连接失败" << endl;
		return -1;
	}
	else
	{
		cout << "连接成功" << endl;
	}

	HANDLE Send_Thread = CreateThread(NULL, 0, Fun_Send, 0, 0, NULL);//创建发送线程
	HANDLE Recv_Thread = CreateThread(NULL, 0, Fun_Recv, 0, 0, NULL);//创建接受线程
	CloseHandle(Send_Thread);//关闭句柄
	CloseHandle(Recv_Thread);//关闭句柄
	hMutex = CreateMutex(NULL, FALSE, L"mutex");//创建互斥量

	while (flag_connect)
	{
		Sleep(100);//必须使用sleep
	}
	return 0;

}

DWORD WINAPI Fun_Send(LPVOID)
{
	while (flag_connect)
	{
		char sendBuff[255];

		cin >> sendBuff;

		int iRet = send(clientsocket, sendBuff, sizeof(sendBuff), 0);
		if (iRet <= 0)
		{
			cout << "服务器关闭连接" << endl;
			closesocket(clientsocket);
			WSACleanup();
			flag_connect = 0;
			return -1;
		}

	}
	return 0;
}

DWORD WINAPI Fun_Recv(LPVOID)
{
	while (flag_connect)
	{
		char recvBuff[255];
		int iRet = recv(clientsocket, recvBuff, 255, 0);
		if (iRet <= 0)
		{
			cout << "服务器关闭连接" << endl;
			closesocket(clientsocket);
			WSACleanup();
			flag_connect = 0;
			return -1;
		}
		cout << recvBuff << endl;
	}
	return 0;
}


                服务器端代码如下

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

using namespace std;

SOCKET connectSocket[100];
SOCKET serversocket;
HANDLE hMutex;//互斥使用输出窗体
int flag_connect = 1;
int num_connect = 0;

DWORD WINAPI Fun_Recv(LPVOID);
DWORD WINAPI Fun_Send(LPVOID);
DWORD WINAPI Fun_Open(LPVOID);
DWORD WINAPI Fun_Close(LPVOID);
int main()
{
	//加载套接字库
	WSADATA wsadata;
	int iRet = WSAStartup(MAKEWORD(2, 2), &wsadata);
	if (iRet != 0)
	{
		cout << "套接字库加载失败" << endl;
		return -1;
	}
	//创建套接字

	serversocket = socket(AF_INET, SOCK_STREAM, 0);
	if (serversocket == INVALID_SOCKET)
	{
		cout << "套接字创建失败" << endl;
		return -1;
	}
	else
	{
		cout << "套接字创建成功" << endl;
	}
	//初始化服务器地址族变量
	SOCKADDR_IN addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	addrSrv.sin_port = htons(8000);

	//绑定
	iRet = bind(serversocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
	if (iRet == SOCKET_ERROR)
	{
		cout << "绑定失败" << endl;
		return -1;
	}
	else
	{
		cout << "绑定成功" << endl;
	}
	//监听
	iRet = listen(serversocket, 10);
	if (iRet == SOCKET_ERROR)
	{
		cout << "开启监听失败" << endl;
		return -1;
	}
	else
	{
		cout << "开启监听成功" << endl;
	}


	HANDLE Open_Thread = CreateThread(NULL, 0, Fun_Open, 0, 0, NULL);//创建连接线程
	HANDLE Close_Thread = CreateThread(NULL, 0, Fun_Close, 0, 0, NULL);//创建断开线程


	//接收数据

	HANDLE Send_Thread = CreateThread(NULL, 0, Fun_Send, 0, 0, NULL);//创建发送线程
	HANDLE Recv_Thread = CreateThread(NULL, 0, Fun_Recv, 0, 0, NULL);//创建接受线程
	CloseHandle(Send_Thread);//关闭句柄
	CloseHandle(Recv_Thread);//关闭句柄
	hMutex = CreateMutex(NULL, FALSE, L"mutex");//创建互斥量

	int i = 0;
	while (flag_connect)
	{
		Sleep(100);//必须使用Sleep
	}
	return 0;
}

DWORD WINAPI Fun_Open(LPVOID)
{
	while (true)
	{
		SOCKADDR_IN addrClient;
		int len = sizeof(SOCKADDR);
		connectSocket[num_connect] = accept(serversocket, (SOCKADDR*)&addrClient, &len);
		if (INVALID_SOCKET == connectSocket[num_connect])
		{
			cout << "接受连接失败" << endl;
			return -1;
		}
		else
		{
			cout << "客户端:" << num_connect << "号已连接" << endl;
			num_connect++;
		}
		Sleep(100);
	}

	return 0;
}

int close_thread = -1;
DWORD WINAPI Fun_Close(LPVOID)
{
	while (true)
	{
		Sleep(200);
		if (close_thread == -1)
		{
			continue;
		}
		else
		{
			connectSocket[close_thread] = 0;
			close_thread = -1;
		}
		
	}

	return 0;
}



DWORD WINAPI Fun_Send(LPVOID)
{
	while (flag_connect)
	{
		char sendBuff[255];
		cin >> sendBuff;
		for (int i = 0; i <num_connect; i++)
		{
			if (connectSocket[i] != 0)
			{
				int iRet = send(connectSocket[i], sendBuff, sizeof(sendBuff), 0);
				if (iRet <= 0)
				{
					cout << "客户端:" << i << "号已关闭" << endl;
					close_thread = i;
				}
			}
		}
		Sleep(100);
	}
	return 0;
}

DWORD WINAPI Fun_Recv(LPVOID)
{
	while (flag_connect)
	{
		char recvBuff[255];
		for (int i = 0; i <num_connect; i++)
		{
			int nNetTimeout = 10;//0.01秒,
			//设置发送超时
			//setsockopt(connectSocket[i], SOL_SOCKET, SO_SNDTIMEO, (char *)&nNetTimeout, sizeof(int));
			//设置接收超时
			setsockopt(connectSocket[i], SOL_SOCKET, SO_RCVTIMEO, (char *)&nNetTimeout, sizeof(int));
			int iRet = recv(connectSocket[i], recvBuff, 255, 0);
			if (iRet == 0)
			{
				cout << "客户端:" << i << "已关闭" << endl;
			}
			else if (iRet > 0)
			{
				cout << "客户端" << i <<":"<< recvBuff << endl;
			}
		}
		Sleep(100);
	}
	return 0;
}

阻塞式C/S通信,多客户端模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值