Question: server #include #include #include #include #include #include #pragma comment ( lib , Ws 2 _ 3 2 . lib ) #define DEFAULT _ BUFLEN
server
#include
#include
#include
#include
#include
#include
#pragma commentlibWslib"
#define DEFAULTBUFLEN
#define DEFAULTPORT
#define MAXCLIENTS
std::vector ClientSockets;
std::mutex mtx;
int cdecl mainvoid
WSADATA wsaData;
int iResult;
SOCKET ListenSocket INVALIDSOCKET;
struct addrinfo result NULL, hints;
char recvbufDEFAULTBUFLEN;
int recvbuflen DEFAULTBUFLEN;
fdset readfds;
int maxsd sd activity;
iResult WSAStartupMAKEWORD &wsaData;
if iResult
printfWSAStartup failed with error: d
iResult;
return ;
ZeroMemory&hints, sizeofhints;
hints.aifamily AFINET;
hints.aisocktype SOCKSTREAM;
hints.aiprotocol IPPROTOTCP;
hints.aiflags AIPASSIVE;
iResult getaddrinfoNULL DEFAULTPORT, &hints, &result;
if iResult
printfgetaddrinfo failed with error: d
iResult;
WSACleanup;
return ;
ListenSocket socketresultaifamily, resultaisocktype, resultaiprotocol;
if ListenSocket INVALIDSOCKET
printfsocket failed with error: ld
WSAGetLastError;
freeaddrinforesult;
WSACleanup;
return ;
ulong mode ; to enable nonblocking mode
iResult ioctlsocketListenSocket FIONBIO, &mode;
if iResult SOCKETERROR
printfioctlsocket failed with error: d
WSAGetLastError;
closesocketListenSocket;
WSACleanup;
return ;
iResult bindListenSocket resultaiaddr, intresultaiaddrlen;
if iResult SOCKETERROR
printfbind failed with error: d
WSAGetLastError;
freeaddrinforesult;
closesocketListenSocket;
WSACleanup;
return ;
freeaddrinforesult;
iResult listenListenSocket SOMAXCONN;
if iResult SOCKETERROR
printflisten failed with error: d
WSAGetLastError;
closesocketListenSocket;
WSACleanup;
return ;
printfServer is running. Waiting for connections...
;
while
FDZERO&readfds;
FDSETListenSocket &readfds;
maxsd ListenSocket;
Lock the mutex before accessing the shared list
mtxlock;
for SOCKET &sock : ClientSockets
sd sock;
if sd FDSETsd &readfds;
if sd maxsd maxsd sd;
mtxunlock; Unlock the mutex after accessing the shared list
activity selectmaxsd &readfds, NULL, NULL, NULL;
if activity SOCKETERROR
printfselect failed with error: d
WSAGetLastError;
break;
if FDISSETListenSocket &readfds
SOCKET ClientSocket acceptListenSocket NULL, NULL;
if ClientSocket INVALIDSOCKET
printfaccept failed with error: d
WSAGetLastError;
continue;
ulong mode ; to enable nonblocking mode
iResult ioctlsocketClientSocket FIONBIO, &mode;
if iResult SOCKETERROR
printfioctlsocket failed with error: d
WSAGetLastError;
closesocketClientSocket;
continue;
printfNew connection accepted
;
Lock the mutex before modifying the shared list
mtxlock;
ClientSockets.pushbackClientSocket;
mtxunlock; Unlock the mutex after modification
mtxlock;
for auto it ClientSockets.begin; it ClientSockets.end;
sd it;
if FDISSETsd &readfds
iResult recvsd recvbuf, recvbuflen, ;
if iResult
recvbufiResult;
printfClient d: s
intit ClientSockets.begin recvbuf;
sendsd recvbuf, iResult, ;
else if iResult
printfClient d disconnected
intit ClientSockets.begin;
closesocketsd;
it ClientSockets.eraseit; Erase the client from the list
continue;
else
printfrecv failed with error: d
WSAGetLastError;
closesocketsd;
it ClientSockets.eraseit; Erase the client from the list
continue;
it;
mtxunlock;
closesocketListenSocket;
mtxlock;
for SOCKET &sock : ClientSockets
if sock INVALIDSOCKET
closesocketsock;
mtxunlock;
WSACleanup;
return ;
client
#include
#include
#include
#include
#include
#pragma commentlibWslib"
#define DEFAULTBUFLEN
#define DEFAULTPORT
int cdecl mainint argc, char argv
WSADATA wsaData;
SOCKET ConnectSocket INVALIDSOCKET;
struct addrinfo result NULL, ptr NULL, hints;
char recvbufDEFAULTBUFLEN;
int iResult;
int recvbuflen DEFAULTBUFLEN;
if argc
printfusage: s servername
argv;
return ;
iResult WSAStartupMAKEWORD &wsaData;
if iResult
printfWSAStartup failed with error: d
iResult;
return ;
ZeroMemory&hints, sizeofhints;
hints.aifamily AFUNSPEC;
hints.aisocktype SOCKSTREAM;
hints.aiprotocol IPPROTOTCP;
iResult getaddrinfoargv DEFAULTPORT, &hints, &result;
if iResult
printfgetaddrinfo failed with error: d
iResult;
WSACleanup;
return ;
for ptr result; ptr NULL; ptr ptrainext
ConnectSocket socketptraifamily, ptraisocktype, ptraiprotocol;
if ConnectSocket INVALIDSOCKET
printfsocket failed with error: ld
WSAGetLastError;
WSACleanup;
return ;
ulong mode ; to enable nonblocking mode
iResult ioctlsocketConnectSocket FIONBIO, &mode;
if iResult SOCKETERROR
printfioctlsocket failed with error: d
WSAGetLastError;
closesocketConnectSocket;
WSACleanup;
return
