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