Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help on Simple Socket Programming in C++. Question: The client enters a weight in pounds and sends it to the server as a positive

Need help on Simple Socket Programming in C++.

Question:

The client enters a weight in pounds and sends it to the server as a positive float number and the server converts it into kg and displays it. This process continues until the user provides a negative value, in this case, the application should end.

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; }

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; }

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

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

a. What is the purpose of the team?

Answered: 1 week ago

Question

b. What are its goals and objectives?

Answered: 1 week ago