Last active
October 8, 2024 12:20
-
-
Save sunmeat/02b60c8a3eaef3b8a0fb3c249d8686fd to your computer and use it in GitHub Desktop.
client server UDP C++ example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CLIENT SIDE: | |
#include <iostream> | |
#include <winsock2.h> | |
using namespace std; | |
#pragma comment(lib,"ws2_32.lib") | |
#pragma warning(disable:4996) | |
#define SERVER "127.0.0.1" // or "localhost" - ip address of UDP server | |
#define BUFLEN 512 // max length of answer | |
#define PORT 8888 // the port on which to listen for incoming data | |
class UDPClient { | |
public: | |
UDPClient() { | |
// initialise winsock | |
cout << "Initialising Winsock...\n"; | |
if (WSAStartup(MAKEWORD(2, 2), &ws) != 0) { | |
cout << "Failed. Error Code: " << WSAGetLastError() << "\n"; | |
exit(EXIT_FAILURE); | |
} | |
cout << "Initialised.\n"; | |
// create socket | |
if ((client_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR) { | |
cout << "socket() failed with error code: " << WSAGetLastError() << "\n"; | |
exit(EXIT_FAILURE); | |
} | |
// setup address structure | |
memset((char*)&server, 0, sizeof(server)); | |
server.sin_family = AF_INET; | |
server.sin_port = htons(PORT); | |
server.sin_addr.S_un.S_addr = inet_addr(SERVER); | |
} | |
~UDPClient() { | |
closesocket(client_socket); | |
WSACleanup(); | |
} | |
void start() { | |
while (true) { | |
char message[BUFLEN]; | |
cout << "Enter message: "; | |
cin.getline(message, BUFLEN); | |
// send the message | |
if (sendto(client_socket, message, strlen(message), 0, (sockaddr*)&server, sizeof(sockaddr_in)) == SOCKET_ERROR) { | |
cout << "sendto() failed with error code: " << WSAGetLastError() << "\n"; | |
exit(EXIT_FAILURE); | |
} | |
// receive a reply and print it | |
// clear the answer by filling null, it might have previously received data | |
char answer[BUFLEN] = {}; | |
// try to receive some data, this is a blocking call | |
int slen = sizeof(sockaddr_in); | |
int answer_length; | |
if ((answer_length = recvfrom(client_socket, answer, BUFLEN, 0, (sockaddr*)&server, &slen)) == SOCKET_ERROR) { | |
cout << "recvfrom() failed with error code: " << WSAGetLastError() << "\n"; | |
exit(EXIT_FAILURE); | |
} | |
cout << "Server's response: " << answer << "\n"; | |
} | |
} | |
private: | |
WSADATA ws; | |
SOCKET client_socket; | |
sockaddr_in server; | |
}; | |
int main() { | |
system("title UDP Client"); | |
MoveWindow(GetConsoleWindow(), 50, 50, 500, 500, true); | |
UDPClient udpClient; | |
udpClient.start(); | |
} | |
========================================================================================================================== | |
SERVER SIDE: | |
#include <iostream> | |
#include <winsock2.h> | |
using namespace std; | |
#pragma comment(lib,"ws2_32.lib") // Winsock Library | |
#pragma warning(disable:4996) | |
#define BUFLEN 512 | |
#define PORT 8888 | |
class UDPServer { | |
public: | |
UDPServer() { | |
// initialise winsock | |
cout << "Initialising Winsock...\n"; | |
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { | |
cout << "Failed. Error Code: " << WSAGetLastError() << "\n"; | |
exit(0); | |
} | |
cout << "Initialised.\n"; | |
// create a socket | |
if ((server_socket = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) { | |
cout << "Could not create socket: " << WSAGetLastError() << "\n"; | |
exit(EXIT_FAILURE); | |
} | |
cout << "Socket created.\n"; | |
// prepare the sockaddr_in structure | |
server.sin_family = AF_INET; | |
server.sin_addr.s_addr = INADDR_ANY; | |
server.sin_port = htons(PORT); | |
// bind | |
if (bind(server_socket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) { | |
cout << "Bind failed with error code: " << WSAGetLastError() << "\n"; | |
exit(EXIT_FAILURE); | |
} | |
cout << "Bind done.\n"; | |
} | |
~UDPServer() { | |
closesocket(server_socket); | |
WSACleanup(); | |
} | |
void start() { | |
while (!exitRequested) { | |
cout << "Waiting for data...\n"; | |
char message[BUFLEN] = {}; | |
// try to receive some data, this is a blocking call | |
int message_len; | |
int slen = sizeof(sockaddr_in); | |
if ((message_len = recvfrom(server_socket, message, BUFLEN, 0, (sockaddr*)&client, &slen)) == SOCKET_ERROR) { | |
cout << "recvfrom() failed with error code: " << WSAGetLastError() << "\n"; | |
exit(0); | |
} | |
// print details of the client/peer and the data received | |
cout << "Received packet from " << inet_ntoa(client.sin_addr) << " " << ntohs(client.sin_port) << "\n"; | |
cout << "Data: " << message << "\n"; | |
cout << "Enter response (exit to stop server process): "; | |
cin.getline(message, BUFLEN); | |
// reply to the client with the same data | |
if (sendto(server_socket, message, strlen(message), 0, (sockaddr*)&client, sizeof(sockaddr_in)) == SOCKET_ERROR) { | |
cout << "sendto() failed with error code: " << WSAGetLastError() << "\n"; | |
exit(EXIT_FAILURE); | |
} | |
if (strcmp(message, "exit") == 0) { | |
cout << "Exiting server...\n"; | |
exitRequested = true; | |
break; | |
} | |
} | |
} | |
private: | |
WSADATA wsa{}; | |
SOCKET server_socket = 0; | |
sockaddr_in server{}, client{}; | |
bool exitRequested = false; | |
}; | |
int main() { | |
system("title UDP Server"); | |
MoveWindow(GetConsoleWindow(), 650, 50, 500, 500, true); | |
UDPServer udpServer; | |
ShellExecuteA(GetConsoleWindow(), "open", "\"C:/Users/Alex/Desktop/ClientServer/x64/Debug/Client.exe\"", 0, 0, SW_NORMAL); | |
udpServer.start(); | |
} | |
int main() { | |
system("title UDP Server"); | |
UDPServer udpServer; | |
udpServer.start(); | |
} |
It seems line 157-158 is unreachable, right?
Hi, @wirekang! I made a check so that the server can stop its work now :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems line 157-158 is unreachable, right?