#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
using namespace std;
#define MAXDATASIZE 200
int main()
{
WSAEVENT eventArray[WSA_MAXIMUM_WAIT_EVENTS];
SOCKET sockArray[WSA_MAXIMUM_WAIT_EVENTS];
int nEventTotal = 0;
WSADATA wsadata;
WSAStartup(0x0202, &wsadata);
SOCKET sListen = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(27105);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
bind(sListen, (SOCKADDR*)&sin, sizeof(sin));
listen(sListen, 5);
WSAEVENT myevent = WSACreateEvent();
WSAEventSelect(sListen, myevent, FD_ACCEPT | FD_CLOSE);
eventArray[nEventTotal] = myevent;
sockArray[nEventTotal] = sListen;
nEventTotal++;
printf("Server listening...\n");
int nSent;
while (true)
{
int nIndex = WSAWaitForMultipleEvents(nEventTotal, eventArray, FALSE, WSA_INFINITE, FALSE);
nIndex -= WSA_WAIT_EVENT_0;
for (int i = 0; i < nEventTotal; ++i)
{
/*int ret = WSAWaitForMultipleEvents(1, &eventArray[i], TRUE, 1000, FALSE);
if (ret == WSA_WAIT_EVENT_0 || ret == WSA_WAIT_TIMEOUT)
{
continue;
}
else
{*/
WSANETWORKEVENTS event1;
WSAEnumNetworkEvents(sockArray[i], eventArray[i], &event1);
if (event1.lNetworkEvents & FD_ACCEPT)
{
if (event1.iErrorCode[FD_ACCEPT_BIT] == 0)
{
if (nEventTotal > WSA_MAXIMUM_WAIT_EVENTS)
{
printf("Too many connecting.\n");
continue;
}
SOCKET sNew = accept(sockArray[i], NULL, NULL);
WSAEVENT newEvent = WSACreateEvent();
WSAEventSelect(sNew, newEvent, FD_READ | FD_CLOSE | FD_WRITE);
eventArray[nEventTotal] = newEvent;
sockArray[nEventTotal] = sNew;
nEventTotal++;
printf("accept() successfully.\n");
}
}
else if (event1.lNetworkEvents & FD_READ)
{
if (event1.iErrorCode[FD_ACCEPT_BIT] == 0)
{
char szText[MAXDATASIZE];
int nRecv = recv(sockArray[i], szText, MAXDATASIZE, 0);
if (nRecv > 0)
{
szText[nRecv] = '\0';
printf("recv : %s\n", szText);
strcpy(szText, "msg receive.");
nSent = send(sockArray[i], szText, strlen(szText), 0);
}
}
}
else if (event1.lNetworkEvents & FD_CLOSE)
{
if (event1.iErrorCode[FD_ACCEPT_BIT] == 0)
{
closesocket(sockArray[i]);
for (int j = i; j < nEventTotal-1; j++)
{
sockArray[i] = sockArray[i + 1];
}
nEventTotal--;
}
}
else if (event1.lNetworkEvents & FD_WRITE)
{
if (event1.iErrorCode[FD_ACCEPT_BIT] == 0)
{
char szText[] = "msg received.";
nSent = send(sockArray[i], szText, strlen(szText), 0);
}
}
//}
}
}
closesocket(sListen);
WSACleanup();
return 0;
}
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
using namespace std;
#define MAXDATASIZE 200
int main()
{
WSAEVENT eventArray[WSA_MAXIMUM_WAIT_EVENTS];
SOCKET sockArray[WSA_MAXIMUM_WAIT_EVENTS];
int nEventTotal = 0;
WSADATA wsadata;
WSAStartup(0x0202, &wsadata);
SOCKET sListen = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(27105);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
bind(sListen, (SOCKADDR*)&sin, sizeof(sin));
listen(sListen, 5);
WSAEVENT myevent = WSACreateEvent();
WSAEventSelect(sListen, myevent, FD_ACCEPT | FD_CLOSE);
eventArray[nEventTotal] = myevent;
sockArray[nEventTotal] = sListen;
nEventTotal++;
printf("Server listening...\n");
int nSent;
while (true)
{
int nIndex = WSAWaitForMultipleEvents(nEventTotal, eventArray, FALSE, WSA_INFINITE, FALSE);
nIndex -= WSA_WAIT_EVENT_0;
for (int i = 0; i < nEventTotal; ++i)
{
/*int ret = WSAWaitForMultipleEvents(1, &eventArray[i], TRUE, 1000, FALSE);
if (ret == WSA_WAIT_EVENT_0 || ret == WSA_WAIT_TIMEOUT)
{
continue;
}
else
{*/
WSANETWORKEVENTS event1;
WSAEnumNetworkEvents(sockArray[i], eventArray[i], &event1);
if (event1.lNetworkEvents & FD_ACCEPT)
{
if (event1.iErrorCode[FD_ACCEPT_BIT] == 0)
{
if (nEventTotal > WSA_MAXIMUM_WAIT_EVENTS)
{
printf("Too many connecting.\n");
continue;
}
SOCKET sNew = accept(sockArray[i], NULL, NULL);
WSAEVENT newEvent = WSACreateEvent();
WSAEventSelect(sNew, newEvent, FD_READ | FD_CLOSE | FD_WRITE);
eventArray[nEventTotal] = newEvent;
sockArray[nEventTotal] = sNew;
nEventTotal++;
printf("accept() successfully.\n");
}
}
else if (event1.lNetworkEvents & FD_READ)
{
if (event1.iErrorCode[FD_ACCEPT_BIT] == 0)
{
char szText[MAXDATASIZE];
int nRecv = recv(sockArray[i], szText, MAXDATASIZE, 0);
if (nRecv > 0)
{
szText[nRecv] = '\0';
printf("recv : %s\n", szText);
strcpy(szText, "msg receive.");
nSent = send(sockArray[i], szText, strlen(szText), 0);
}
}
}
else if (event1.lNetworkEvents & FD_CLOSE)
{
if (event1.iErrorCode[FD_ACCEPT_BIT] == 0)
{
closesocket(sockArray[i]);
for (int j = i; j < nEventTotal-1; j++)
{
sockArray[i] = sockArray[i + 1];
}
nEventTotal--;
}
}
else if (event1.lNetworkEvents & FD_WRITE)
{
if (event1.iErrorCode[FD_ACCEPT_BIT] == 0)
{
char szText[] = "msg received.";
nSent = send(sockArray[i], szText, strlen(szText), 0);
}
}
//}
}
}
closesocket(sListen);
WSACleanup();
return 0;
}