Question: Server #undef UNICODE #define WIN 3 2 _ LEAN _ AND _ MEAN #include #include #include #include #pragma comment ( lib , Ws 2
Server
#undef UNICODE
#define WINLEANANDMEAN
#include
#include
#include
#include
#pragma commentlibWslib"
#define DEFAULTBUFLEN
#define DEFAULTPORT
#define MAXCLIENTS
int cdecl mainvoid
WSADATA wsaData;
int iResult;
SOCKET ListenSocket INVALIDSOCKET;
SOCKET ClientSocketsMAXCLIENTS INVALIDSOCKET ;
struct addrinfo result NULL, hints;
char recvbufDEFAULTBUFLEN;
int recvbuflen DEFAULTBUFLEN;
fdset readfds;
int maxsd sd activity, i;
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;
for i ; i MAXCLIENTS; i
sd ClientSocketsi;
if sd
FDSETsd &readfds;
if sd maxsd
maxsd sd;
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
;
for i ; i MAXCLIENTS; i
if ClientSocketsi INVALIDSOCKET
ClientSocketsi ClientSocket;
break;
for i ; i MAXCLIENTS; i
sd ClientSocketsi;
if FDISSETsd &readfds
iResult recvsd recvbuf, recvbuflen, ;
if iResult
recvbufiResult;
printfClient d: s
i recvbuf;
sendsd recvbuf, iResult, ;
else if iResult
printfClient d disconnected
i;
closesocketsd;
ClientSocketsi INVALIDSOCKET;
else
printfrecv failed with error: d
WSAGetLastError;
closesocketsd;
ClientSocketsi INVALIDSOCKET;
closesocketListenSocket;
for i ; i MAXCLIENTS; i
if ClientSocketsi INVALIDSOCKET
closesocketClientSocketsi;
WSACleanup;
return ;
client
#define WINLEANANDMEAN
#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 ;
iResult connectConnectSocket ptraiaddr, intptraiaddrlen;
if iResult SOCKETERROR
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
