Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Socket Programming in C/C++ Client: The user is asked to enter a weight (measured in lbs) as a positive float number. Once entered, the client

Socket Programming in C/C++

Client:

  • The 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 converts them to Kg and displays the value on the screen.

client.cpp

#include #include #pragma comment(lib, "Ws2_32.lib")

int main() { WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { std::cout << "Could not start DLLs" << std::endl; return 0; }

SOCKET ClientSocket; ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ClientSocket == INVALID_SOCKET) { std::cout << "Could not create socket" << std::endl; WSACleanup(); return 0; }

struct sockaddr_in SvrAddr; SvrAddr.sin_family = AF_INET; SvrAddr.sin_port = htons(27000); SvrAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); if ((connect(ClientSocket, (struct sockaddr*)&SvrAddr, sizeof(SvrAddr))) == SOCKET_ERROR) { std::cout << "Failed to connect" << std::endl; closesocket(ClientSocket); WSACleanup(); return 0; }

char TxBuffer[128] = {}; std::cout << "Enter a String to transmit" << std::endl;

//std::cin >> TxBuffer; std::cin.getline(TxBuffer, sizeof(TxBuffer));

send(ClientSocket, TxBuffer, sizeof(TxBuffer), 0);

char RxBuffer[128] = {}; recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0); std::cout << "Response: " << RxBuffer << std::endl;

closesocket(ClientSocket); WSACleanup();

return 0; }

server.CPP #include #include #pragma comment(lib, "Ws2_32.lib")

int main() {

WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { std::cout << "Could not start DLLs" << std::endl; return 0; }

SOCKET ListenSocket; ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ListenSocket == INVALID_SOCKET) { std::cout << "Could not create socket" << std::endl; WSACleanup(); return 0; }

struct sockaddr_in SvrAddr; SvrAddr.sin_family = AF_INET; SvrAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); SvrAddr.sin_port = htons(27000); if (bind(ListenSocket, (struct sockaddr*)&SvrAddr, sizeof(SvrAddr)) == SOCKET_ERROR) { std::cout << "Could not bind socket to address" << std::endl; closesocket(ListenSocket); WSACleanup(); return 0; }

if (listen(ListenSocket, 1) == SOCKET_ERROR) { std::cout << "Could not start to listen" << std::endl; closesocket(ListenSocket); WSACleanup(); return 0; }

std::cout << "Waiting for client connection" << std::endl;

SOCKET ClientSocket; ClientSocket = accept(ListenSocket, NULL, NULL); if (ClientSocket == INVALID_SOCKET) { std::cout << "Failed to accept connection" << std::endl; closesocket(ListenSocket); WSACleanup(); return 0; }

closesocket(ListenSocket); std::cout << "Connection Established" << std::endl;

char RxBuffer[128] = {}; recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0); std::cout << "Msg Rx: " << RxBuffer << std::endl;

char TxBuffer[128] = "Thanks for your message!"; send(ClientSocket, TxBuffer, sizeof(TxBuffer), 0);

closesocket(ClientSocket); WSACleanup();

return 0; }

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

Step: 3

blur-text-image

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

Samsung Galaxy S23 Ultra Comprehensive User Manual

Authors: Leo Scott

1st Edition

B0BVPBJK5Q, 979-8377286455

More Books

Students also viewed these Databases questions

Question

Which form of proof do you find most persuasive? Why?

Answered: 1 week ago