#include<iostream>
#include<WinSock2.h>
#pragma warning(disable:4996)
#pragma comment(lib,"ws2_32.lib")
using namespace std;
#define MaxSize 64
SOCKET listensocket, serversocket;
sockaddr_in socketaddr;
HANDLE event;
HANDLE hHandle[3] ;
WSAEVENT Event[MaxSize];
SOCKET Socket[MaxSize];
HANDLE hEvent;
int EventTotal = 0;
DWORD Index;
WSANETWORKEVENTS NetworkEvents;//结构体数组,记录网络事件类型以及相应的错误代码
BOOL SetConsoleColor(WORD wAttributes)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole == INVALID_HANDLE_VALUE)
return FALSE;
return SetConsoleTextAttribute(hConsole, wAttributes);
}
int main(void)
{
char buf[1000];
memset(buf, 0, 1000 * sizeof(char));
int len;
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
memset((char*)&socketaddr, 0, sizeof(socketaddr));
socketaddr.sin_family = AF_INET;//sin_family指代协议族,在socket编程中只能是AF_INET
socketaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//sin_addr存储IP地址,使用in_addr这个数据结构
socketaddr.sin_port = htons(1234);//端口
if ((serversocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
cout << "创建套接字失败!" << endl;
cerr << GetLastError() << endl;
system("pause");
return 0;
}
if (bind(serversocket, (sockaddr*)&socketaddr, sizeof(socketaddr))== SOCKET_ERROR)
{
cout << "绑定失败!!" << endl;
cerr << GetLastError() << endl;
system("pause");
return 0;
}
hEvent = WSACreateEvent();//创建一个监听网络事件对象
WSAEventSelect(serversocket, hEvent, FD_ACCEPT | FD_CLOSE);
// 将事件对象与套接字关联,同时注册事件,使事件对象的工作状态从未传信转变未已传信。
Socket[EventTotal] =serversocket;
Event[EventTotal] = hEvent;
EventTotal++;
listen(serversocket, 10);
cout << "等待客户端连接..." << endl;
while (true)
{
Index = WSAWaitForMultipleEvents(EventTotal, Event, FALSE,WSA_INFINITE, FALSE);
// cout << "监听线程中Index" << Index << endl;
//索引值减去WSA_WAIT_EVENT_0 即为我们所等待的事件
WSAEnumNetworkEvents(Socket[Index - WSA_WAIT_EVENT_0], Event[Index - WSA_WAIT_EVENT_0], &NetworkEvents);
//通过获取是哪个套接字来判断发生了什么网络事件
if (NetworkEvents.lNetworkEvents == FD_ACCEPT)
{
//if (NetworkEvents.iErrorCode[FD_ACCEPT_BIT] != 0)
//continue;
int len = sizeof(socketaddr);
//accept接受后返回的socket作为通信所用的socket,当错误连接时返回 INVALID_SOCKET
//listensocket = accept(Socket[Index - WSA_WAIT_EVENT_0], NULL, NULL);
listensocket = accept(serversocket, (struct sockaddr*)&socketaddr, &len);
cout << "客户端已连接! " << endl;
// WSAResetEvent(Event[Index - WSA_WAIT_EVENT_0]);
hEvent = WSACreateEvent();//创建一个监听网络事件对象
WSAEventSelect(listensocket, hEvent, FD_READ | FD_WRITE | FD_CLOSE);
Socket[EventTotal] = listensocket;
Event[EventTotal] = hEvent;
EventTotal++;
}
if (NetworkEvents.lNetworkEvents == FD_READ)
{
len = recv(Socket[Index - WSA_WAIT_EVENT_0], buf, 1000, 0);
if (len != -1)
{
SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "收到:" << buf << endl;
SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
memset(buf, 0, sizeof(char) * 1000);
len = -1;
}
}
if (NetworkEvents.lNetworkEvents == FD_WRITE)
{
SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
cin >> buf;
len = send(Socket[Index - WSA_WAIT_EVENT_0], buf, 1000, 0);
memset(buf, 0, 1000 * sizeof(char));
}
/*if (NetworkEvents.lNetworkEvents == FD_CLOSE)
{
SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "\n\n客户端断开连接!!\n\n";
system("pause");
return 0;
}*/
}
delete[]buf;
system("pause");
return 0;
}
select模型
最新推荐文章于 2024-02-09 02:44:10 发布