From 35d6714143b3ca510d8023339e1c9a0cf8c7c236 Mon Sep 17 00:00:00 2001 From: xXIamNoOneXx <143564810+xXIamNoOneXx@users.noreply.github.com> Date: Sun, 3 Sep 2023 18:18:43 -0500 Subject: [PATCH] Create simple_server.cpp Signed-off-by: xXIamNoOneXx <143564810+xXIamNoOneXx@users.noreply.github.com> --- simple_server.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 simple_server.cpp diff --git a/simple_server.cpp b/simple_server.cpp new file mode 100644 index 0000000..122e056 --- /dev/null +++ b/simple_server.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include + +int main() { + int server_socket, client_socket; + struct sockaddr_in server_addr, client_addr; + + // Create a socket + server_socket = socket(AF_INET, SOCK_STREAM, 0); + + // Configure the server address structure + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(12345); // Choose a port + server_addr.sin_addr.s_addr = INADDR_ANY; + + // Bind the socket to the server address + bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)); + + // Listen for incoming connections + listen(server_socket, 5); // Number of queued connections + + std::cout << "Server listening on port 12345..." << std::endl; + + // Accept an incoming connection + socklen_t client_addr_len = sizeof(client_addr); + client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len); + + std::cout << "Accepted connection from " << inet_ntoa(client_addr.sin_addr) << std::endl; + + // Send a welcome message to the client + const char* welcome_message = "Welcome to the server!\n"; + send(client_socket, welcome_message, strlen(welcome_message), 0); + + // Close the sockets + close(client_socket); + close(server_socket); + + return 0; +}