Question: Client code #define WIN 3 2 _ LEAN _ AND _ MEAN #include #include #include #include #include / / Need to link with Ws 2
Client code
#define WINLEANANDMEAN
#include
#include
#include
#include
#include
Need to link with Wslib, Mswsock.lib, and Advapilib
#pragma comment libWslib"
#pragma comment lib "Mswsock.lib"
#pragma comment lib "AdvApilib"
#define DEFAULTBUFLEN
#define DEFAULTPORT
int cdecl mainint argc, char argv
WSADATA wsaData;
SOCKET ConnectSocket INVALIDSOCKET;
struct addrinfo result NULL,
ptr NULL,
hints;
const char sendbuf "this is a test";
char recvbufDEFAULTBUFLEN;
int iResult;
int recvbuflen DEFAULTBUFLEN;
Validate the parameters
if argc
printfusage: s servername
argv;
return ;
Initialize Winsock
iResult WSAStartupMAKEWORD &wsaData;
if iResult
printfWSAStartup failed with error: d
iResult;
return ;
ZeroMemory &hints, sizeofhints;
hints.aifamily AFUNSPEC;
hints.aisocktype SOCKSTREAM;
hints.aiprotocol IPPROTOTCP;
Resolve the server address and port
iResult getaddrinfoargv DEFAULTPORT, &hints, &result;
if iResult
printfgetaddrinfo failed with error: d
iResult;
WSACleanup;
return ;
Attempt to connect to an address until one succeeds
forptrresult; ptr NULL ;ptrptrainext
Create a SOCKET for connecting to server
ConnectSocket socketptraifamily, ptraisocktype,
ptraiprotocol;
if ConnectSocket INVALIDSOCKET
printfsocket failed with error: ld
WSAGetLastError;
WSACleanup;
return ;
Connect to server.
iResult connect ConnectSocket, ptraiaddr, intptraiaddrlen;
if iResult SOCKETERROR
closesocketConnectSocket;
ConnectSocket INVALIDSOCKET;
continue;
break;
freeaddrinforesult;
if ConnectSocket INVALIDSOCKET
printfUnable to connect to server!
;
WSACleanup;
return ;
Send an initial buffer
iResult send ConnectSocket, sendbufintstrlensendbuf;
if iResult SOCKETERROR
printfsend failed with error: d
WSAGetLastError;
closesocketConnectSocket;
WSACleanup;
return ;
printfBytes Sent: ld
iResult;
shutdown the connection since no more data will be sent
iResult shutdownConnectSocket SDSEND;
if iResult SOCKETERROR
printfshutdown failed with error: d
WSAGetLastError;
closesocketConnectSocket;
WSACleanup;
return ;
Receive until the peer closes the connection
do
iResult recvConnectSocket recvbuf, recvbuflen, ;
if iResult
printfBytes received: d
iResult;
else if iResult
printfConnection closed
;
else
printfrecv failed with error: d
WSAGetLastError;
while iResult ;
cleanup
closesocketConnectSocket;
WSACleanup;
return ;
Server code
#undef UNICODE
#define WINLEANANDMEAN
#include
#include
#include
#include
#include
Need to link with Wslib
#pragma comment libWslib"
#pragma comment lib "Mswsock.lib"
#define DEFAULTBUFLEN
#define DEFAULTPORT
int cdecl mainvoid
WSADATA wsaData;
int iResult;
SOCKET ListenSocket INVALIDSOCKET;
SOCKET ClientSocket INVALIDSOCKET;
struct addrinfo result NULL;
struct addrinfo hints;
int iSendResult;
char recvbufDEFAULTBUFLEN;
int recvbuflen DEFAULTBUFLEN;
Initialize Winsock
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;
Resolve the server address and port
iResult getaddrinfoNULL DEFAULTPORT, &hints, &result;
if iResult
printfgetaddrinfo failed with error: d
iResult;
WSACleanup;
return ;
Create a SOCKET for the server to listen for client connections.
ListenSocket socketresultaifamily, resultaisocktype, resultaiprotocol;
if ListenSocket INVALIDSOCKET
printfsocket failed with error: ld
WSAGetLastError;
freeaddrinforesult;
WSACleanup;
return ;
Setup the TCP listening socket
iResult bind ListenSocket, 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 ;
Accept a client socket
ClientSocket acceptListenSocket NULL, NULL;
if ClientSocket INVALIDSOCKET
printfaccept failed with error: d
WSAGetLastError;
closesocketListenSocket;
WSACleanup;
return ;
No longer need server socket
closesocketListenSocket;
Receive until the peer shuts down the connection
do
iResult recvClientSocket recvbuf, recvbuflen, ;
if iResult
printfBytes received: d
iResult;
Echo the buffer back to the sender
iSendResult send ClientSocket, recvbuf, iResult, ;
if iSendResult SOCKETERROR
printfsend failed with error: d
WSAGetLastError;
closesocketClientSocket;
WSACleanup;
return ;
printfBytes sent: d
iSendResult;
else if iResult
printfConnection closing...
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
