Question: Apply non - blocking and multiple client connection to this Client Server codes by using Select ( ) method. The client and server should be
Apply nonblocking and multiple client connection to this Client Server codes by using Select method. The client and server should be able to send messages to each other whenever they want. They shouldn't have to send them in order. I don't prefer it for now, but if necessary, you can use a single thread structure. The kick and quit should continue to work as below.
Client
#define WINLEANANDMEAN
#include
#include
#include
#include
Need to link with Wslib
#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 ;
iResult connectConnectSocket ptraiaddr, intptraiaddrlen;
if iResult SOCKETERROR
closesocketConnectSocket;
ConnectSocket INVALIDSOCKET;
continue;
break;
freeaddrinforesult;
if ConnectSocket INVALIDSOCKET
printfUnable to connect to server!
;
WSACleanup;
return ;
char sendbufDEFAULTBUFLEN;
printfConnected to server. Type 'quit' to exit.
;
while
printfEnter message: ;
fgetssendbuf DEFAULTBUFLEN, stdin;
sendbufstrcspnsendbuf
;
iResult sendConnectSocket sendbufintstrlensendbuf;
if iResult SOCKETERROR
printfsend failed with error: d
WSAGetLastError;
closesocketConnectSocket;
WSACleanup;
return ;
if strcmpsendbuf "quit"
printfClient exiting...
;
break;
iResult recvConnectSocket recvbuf, recvbuflen, ;
if iResult
recvbufiResult;
printfServer: s
recvbuf;
if strcmprecvbuf "kick"
printfKicked by server.
;
break;
else if iResult
printfConnection closed
;
break;
else
printfrecv failed with error: d
WSAGetLastError;
break;
closesocketConnectSocket;
WSACleanup;
return ;
Server
#undef UNICODE
#define WINLEANANDMEAN
#include
#include
#include
#include
Need to link with Wslib
#pragma commentlibWslib"
#define DEFAULTBUFLEN
#define DEFAULTPORT
int cdecl mainvoid
WSADATA wsaData;
int iResult;
SOCKET ListenSocket INVALIDSOCKET;
SOCKET ClientSocket INVALIDSOCKET;
struct addrinfo result NULL, hints;
char recvbufDEFAULTBUFLEN;
int recvbuflen DEFAULTBUFLEN;
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 ;
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 ;
printfWaiting for client to connect...
;
ClientSocket acceptListenSocket NULL, NULL;
if ClientSocket INVALIDSOCKET
printfaccept failed with error: d
WSAGetLastError;
closesocketListenSocket;
WSACleanup;
return ;
closesocketListenSocket;
char sendbufDEFAULTBUFLEN;
printfClient connected. Type 'kick' to disconnect the client.
;
while
iResult recvClientSocket recvbuf, recvbuflen, ;
if iResult
recvbufiResult;
printfClient: s
recvbuf;
if strcmprecvbuf "quit"
printfClient disconnected.
;
break;
printfEnter message: ;
fgetssendbuf DEFAULTBUFLEN, stdin;
sendbufstrcspnsendbuf
;
iResult sendClientSocket sendbufintstrlensendbuf;
if iResult SOCKETERROR
printfsend failed with error: d
WSAGetLastError;
closesocketClientSocket;
WSACleanup;
return ;
if strcmpsendbuf "kick"
printfClient kicked.
;
break;
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
