栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

select模型

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

select模型

//#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
using namespace std;
//#pragma pack(8)
int main() {
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
		cout << "wsastart faild<ulong
	//inet_ntoa(in_addr in)->char
	//htons short
	//htonl long
	//ntohs short
	//
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);


	errCode = bind(Listen, (SOCKADDR*)&addr, addr_len);
	if (errCode == SOCKET_ERROR) {
		cout << "bind failed" << endl;
		closesocket(Listen);
		return 1;
	}
	
	if (listen(Listen, 5) == SOCKET_ERROR) {
		cout << "listen error" << endl;
		exit(2);
	}
	SOCKET sock[64];

	fd_set rd_set;
	struct timeval st;
	st.tv_sec = 3;
	st.tv_usec = 0;
	//fd_set wr_set;
	int cnt = 0;
	FD_ZERO(&rd_set);
	//FD_ZERO(&wr_set);
	//FD_SET(Listen, &rd_set);

	while (true) {
		FD_ZERO(&rd_set);
		FD_SET(Listen, &rd_set);
		for (int i = 0; i < cnt; i++) {
			FD_SET(sock[i], &rd_set);
			//FD_SET(sock[i], &wr_set);
		}
		int nRes = select(0, &rd_set, NULL, NULL, NULL);
		SOCKET_ERROR;
		if (0 == nRes) {
			continue;
		}
		else {
			if (FD_ISSET(Listen, &rd_set)) {
				cout << "accept" << endl;
				sock[cnt]=accept(Listen, NULL, NULL);
				cnt++;
			}
			for (int i = 0; i < cnt; i++) {
				if (FD_ISSET(sock[i], &rd_set)) {
					char szRecvBuffer[1500] = { 0 };
					int result = recv(sock[i], szRecvBuffer, 1500, MSG_PEEK);
					if (result > 0) {
						cout << szRecvBuffer << endl;
					}
				}
			}
		}
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312134.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号