Answered step by step
Verified Expert Solution
Question
1 Approved Answer
modify these files as follows: Client: User is asked to enter a weight (measured in lbs) as a positive float number. Once entered, the client
modify these files as follows:
Client:
User is asked to enter a weight (measured in lbs) as a positive float number.
Once entered, the client application transmits the weight to the server
This process continues until the user provides a negative value, in this case, the application should end.
Server:
The server simply receives the weights in lb and converts them to Kg and display the value on the screen.
Server.cpp
#include#include #pragma comment(lib, "Ws2_32.lib") using namespace std; void main() { //starts Winsock DLLs WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) return; //create server socket SOCKET ServerSocket; ServerSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ServerSocket == INVALID_SOCKET) { WSACleanup(); return; } //binds socket to address sockaddr_in SvrAddr; SvrAddr.sin_family = AF_INET; SvrAddr.sin_addr.s_addr = INADDR_ANY; SvrAddr.sin_port = htons(27000); if (bind(ServerSocket, (struct sockaddr *)&SvrAddr, sizeof(SvrAddr)) == SOCKET_ERROR) { closesocket(ServerSocket); WSACleanup(); return; } //listen on a socket if (listen(ServerSocket, 1) == SOCKET_ERROR) { closesocket(ServerSocket); WSACleanup(); return; } cout << "Waiting for client connection " << endl; //accepts a connection from a client SOCKET ConnectionSocket; ConnectionSocket = SOCKET_ERROR; if ((ConnectionSocket = accept(ServerSocket, NULL, NULL)) == SOCKET_ERROR) { closesocket(ServerSocket); WSACleanup(); return; } cout << "Connection Established" << endl; while (1) { //receives RxBuffer char RxBuffer[128] = {}; recv(ConnectionSocket, RxBuffer, sizeof(RxBuffer), 0); cout << "Msg Rx: " << RxBuffer << endl; } closesocket(ConnectionSocket); //closes incoming socket closesocket(ServerSocket); //closes server socket WSACleanup(); //frees Winsock resources }
Client.cpp
#include#pragma comment(lib, "Ws2_32.lib") #include using namespace std; void main() { //starts Winsock DLLs WSADATA wsaData; if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) { return; } //initializes socket. SOCK_STREAM: TCP SOCKET ClientSocket; ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ClientSocket == INVALID_SOCKET) { WSACleanup(); return; } //Connect socket to specified server sockaddr_in SvrAddr; SvrAddr.sin_family = AF_INET; //Address family type itnernet SvrAddr.sin_port = htons(27000); //port (host to network conversion) SvrAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //IP address if ((connect(ClientSocket, (struct sockaddr *)&SvrAddr, sizeof(SvrAddr))) == SOCKET_ERROR) { closesocket(ClientSocket); WSACleanup(); return; } //receives Rxbuffer while (1) { //sends Txbuffer char TxBuffer[128] = {}; cout << "Enter a String to transmit" << endl; cin >> TxBuffer; send(ClientSocket, TxBuffer, sizeof(TxBuffer), 0); } //closes connection and socket closesocket(ClientSocket); //frees Winsock DLL resources WSACleanup(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started