Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a streambased echo server. The client should keep sending messages (by user prompt) to the server until the user enters quit. Server Code #undef

Write a streambased echo server. The client should keep sending messages (by user prompt) to the server until the user enters "quit".

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Server Code #undef UNICODE #de fine WIN32LEAN AND MEAN - #include #include #include ai family, result-ai socktype, result- ai protocol); if (ListenSocket INVALID SOCKET) printf("socket failed with error: ldin", WSAGetLastError)) freeaddrinfo(result); WSACleanup) return 1; I Setup the TCP listening socket Result . bind(ListenSocket, result->ai-addr, if (Result ". SOCKET_ERROR) { (int)result->ai-addrlen); printf ("bind failed with error : freeaddrinfo(result); closesocket (ListenSocket) WSACleanup) return 1; %d ", WSAGetLastError()); freeaddrinfo(result); Result . listen(ListenSocket, if (Result ". SOCKET_ERROR) { SONAXCONN); printf ("listen failed with closesocket (ListenSocket) WSACleanup) return 1; error: %d ", WSAGetLastError()); I Accept a client socket ClientSocket accept (ListenSocket, NULL, NULL); if (ClientSocket INVALID SOCKET) printf ("accept failed with closesocket (ListenSocket) WSACleanup) return 1; error: %d ", WSAGetLastError()); // No longer need server socket closesocket (ListenSocket) / Receive until the peer shuts down the connection do iResult- recv(ClientSocket, recvbuf, recvbuflen, e) // Resolve the server address and port Resultsul getaddrinfo(NULL, DEFAULT-PORT, &hints, &result); printf ("getaddrinfo failed WSACleanu with error: %din", iResult); return 1; // Create a SOCKET for connecting to server printf( "Bytes received: %d ", Result); // Echo the buffer back to the sender iS endResult send(ClientSocket, r if (SendResult .. SOCKET-ERROR) { recvbuf, Result, e); printf("send failed with error: closesocket(Client Socket); WSACleanup) return 1; %d ", WSAGetLastError()); printf "Bytes sent: 3dIn, iSendResult) else if (Result .. e) printf"Connection closing.In) else printf("recv failed with error: closesocket(Clientsocket); WSACleanup) return 1; %d ", WSAGetLastError()); while (iResult> e) // shutdown the connection since we're done Result . shutdown(Client Socket, SD-SEND); if (Result .. SOCKET_ERROR) { printf ("shutdown failed with error: closesocket (ClientSocket) WSACle anup) return 1; %d ", WSAGetLastError()); I cleanup closesocket(Clientsocket) WSACleanup return e; Client Code #de fine WIN32LEAN AND MEAN - #include #include #include #include #include ai_protocol) if (Connect Socket INVALID SOCKET) printf( "socket failed with error: 1dIn, WSAGetLastError O); WSACleanup) return 1; // Connect to server. iResult- connect (Connectsocket, ptr-ai addr, (int)ptr->ai addrlen); if (iResult sOCKET ERROR) closesocket (ConnectSocket) Connect Socket INVALID_SOCKET; continue break; freeaddrinfo(result); if (ConnectSocket INVALID SOCKET) printf ("Unable to connect to server! ") WSACleanup) return 1; /I Send an initial buffer iResult send (ConnectSocket, sendbuf, (int)strlen(sendbuf), e) if (Result ". SOCKET_ERROR) { printf ("send failed with error : closesocket (ConnectSocket); WSACleanup) %d ", WSAGetLastError()); return 1; printf( Bytes Sent: X1d , iResult); I shutdown the connection since no more data will be sent iResult shutdown (ConnectSocket, SD SEND) if (Result -SocKET ERROR) printf ("shutdown failed with error: closesocket (Connectsocket) WSACleanup); %d ", WSAGetLastError()); return 1; // Receive until the peer closes the connection do t Result recv(ConnectSocket, recvbuf, recvbuflen, e) if (iResult > e) printf( "Bytes received: %d " , iResult); else if (Result .. e) printf"Connection closed n) else printf("recv failed with error: %d -, WSAGetLastError()); } while (Result > e); I/ cleanup closesocket(Connectsocket); WSAC leanup system("pause") return e; Server Code #undef UNICODE #de fine WIN32LEAN AND MEAN - #include #include #include ai family, result-ai socktype, result- ai protocol); if (ListenSocket INVALID SOCKET) printf("socket failed with error: ldin", WSAGetLastError)) freeaddrinfo(result); WSACleanup) return 1; I Setup the TCP listening socket Result . bind(ListenSocket, result->ai-addr, if (Result ". SOCKET_ERROR) { (int)result->ai-addrlen); printf ("bind failed with error : freeaddrinfo(result); closesocket (ListenSocket) WSACleanup) return 1; %d ", WSAGetLastError()); freeaddrinfo(result); Result . listen(ListenSocket, if (Result ". SOCKET_ERROR) { SONAXCONN); printf ("listen failed with closesocket (ListenSocket) WSACleanup) return 1; error: %d ", WSAGetLastError()); I Accept a client socket ClientSocket accept (ListenSocket, NULL, NULL); if (ClientSocket INVALID SOCKET) printf ("accept failed with closesocket (ListenSocket) WSACleanup) return 1; error: %d ", WSAGetLastError()); // No longer need server socket closesocket (ListenSocket) / Receive until the peer shuts down the connection do iResult- recv(ClientSocket, recvbuf, recvbuflen, e) // Resolve the server address and port Resultsul getaddrinfo(NULL, DEFAULT-PORT, &hints, &result); printf ("getaddrinfo failed WSACleanu with error: %din", iResult); return 1; // Create a SOCKET for connecting to server printf( "Bytes received: %d ", Result); // Echo the buffer back to the sender iS endResult send(ClientSocket, r if (SendResult .. SOCKET-ERROR) { recvbuf, Result, e); printf("send failed with error: closesocket(Client Socket); WSACleanup) return 1; %d ", WSAGetLastError()); printf "Bytes sent: 3dIn, iSendResult) else if (Result .. e) printf"Connection closing.In) else printf("recv failed with error: closesocket(Clientsocket); WSACleanup) return 1; %d ", WSAGetLastError()); while (iResult> e) // shutdown the connection since we're done Result . shutdown(Client Socket, SD-SEND); if (Result .. SOCKET_ERROR) { printf ("shutdown failed with error: closesocket (ClientSocket) WSACle anup) return 1; %d ", WSAGetLastError()); I cleanup closesocket(Clientsocket) WSACleanup return e; Client Code #de fine WIN32LEAN AND MEAN - #include #include #include #include #include ai_protocol) if (Connect Socket INVALID SOCKET) printf( "socket failed with error: 1dIn, WSAGetLastError O); WSACleanup) return 1; // Connect to server. iResult- connect (Connectsocket, ptr-ai addr, (int)ptr->ai addrlen); if (iResult sOCKET ERROR) closesocket (ConnectSocket) Connect Socket INVALID_SOCKET; continue break; freeaddrinfo(result); if (ConnectSocket INVALID SOCKET) printf ("Unable to connect to server! ") WSACleanup) return 1; /I Send an initial buffer iResult send (ConnectSocket, sendbuf, (int)strlen(sendbuf), e) if (Result ". SOCKET_ERROR) { printf ("send failed with error : closesocket (ConnectSocket); WSACleanup) %d ", WSAGetLastError()); return 1; printf( Bytes Sent: X1d , iResult); I shutdown the connection since no more data will be sent iResult shutdown (ConnectSocket, SD SEND) if (Result -SocKET ERROR) printf ("shutdown failed with error: closesocket (Connectsocket) WSACleanup); %d ", WSAGetLastError()); return 1; // Receive until the peer closes the connection do t Result recv(ConnectSocket, recvbuf, recvbuflen, e) if (iResult > e) printf( "Bytes received: %d " , iResult); else if (Result .. e) printf"Connection closed n) else printf("recv failed with error: %d -, WSAGetLastError()); } while (Result > e); I/ cleanup closesocket(Connectsocket); WSAC leanup system("pause") return e

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions