Question
C++ Socket Data Communication (UCP/IP) PART A Update the clients source code in order for the client to be able to keep transmitting messages to
C++ Socket Data Communication (UCP/IP)
PART A
Update the clients source code in order for the client to be able to keep transmitting messages to the server (and receiving feedback messages) until the client sends a message containing only: "QUIT".
PART B
Update the servers source code in order for server to be able to keep receiving messages from the clients, as well as sending replies, with the message Thanks for your message!, after each received message, until a client sends a message containing only: QUIT.
PART C
Update both the clients and the servers source code from parts A and B, so that when a client sends a message QUIT, the client quits and the server sends a message containing the string Goodbye, while remaining ready to receive messages from other clients. Moreover, if the client sends a message [x], both the client as well as the server quit. In this scenario, the server sends a Goodbye message to the client, and displays message Terminated based on clients request on its own console.
UDP Client Code
#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_DGRAM, IPPROTO_UDP); | |
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(27020); | |
SvrAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
char TxBuffer[128] = {}; | |
std::cout << "Enter a string to transmit:" << std::endl; | |
std::cin >> TxBuffer; | |
sendto(ClientSocket, TxBuffer, sizeof(TxBuffer), 0, | |
(struct sockaddr*)&SvrAddr, sizeof(SvrAddr)); | |
char RxBuffer[128] = {}; | |
int addr_len = sizeof(SvrAddr); | |
recvfrom(ClientSocket, RxBuffer, sizeof(RxBuffer), 0, | |
(struct sockaddr*)&SvrAddr, &addr_len); | |
std::cout << "Msg Rx: " << RxBuffer << std::endl; | |
closesocket(ClientSocket); | |
WSACleanup(); | |
} |
UDP Server Code
#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; | |
} | |
//create server socket | |
SOCKET ServerSocket; | |
ServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
if (ServerSocket == 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 = INADDR_ANY; | |
SvrAddr.sin_port = htons(27020); | |
if (bind(ServerSocket, (struct sockaddr*)&SvrAddr, | |
sizeof(SvrAddr)) == SOCKET_ERROR){ | |
std::cout << "Could not bind socket to port" << std::endl; | |
closesocket(ServerSocket); | |
WSACleanup(); | |
return 0; | |
} | |
char RxBuffer[128] = {}; | |
struct sockaddr_in CltAddr; | |
int addr_len = sizeof(CltAddr); | |
std::cout << "Ready to receive messages." << std::endl; | |
recvfrom(ServerSocket, RxBuffer, sizeof(RxBuffer), 0, | |
(struct sockaddr*)&CltAddr, &addr_len); | |
std::cout << "Msg Rx: " << RxBuffer << std::endl; | |
char TxBuffer[128] = { "Got It" }; | |
sendto(ServerSocket, TxBuffer, sizeof(TxBuffer), 0, | |
(struct sockaddr*)&CltAddr, sizeof(CltAddr)); | |
std::cin.get(); | |
closesocket(ServerSocket); | |
WSACleanup(); | |
return 0; | |
} |
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