From 810fb50ddd41354659000aa5b13a3fb6c4d59c07 Mon Sep 17 00:00:00 2001 From: "Simon V. Lejel" Date: Sat, 10 Feb 2024 12:08:37 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=94=20Add=20server=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/main.cc | 49 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/server/main.cc b/src/server/main.cc index 3ccde6b..873c41f 100644 --- a/src/server/main.cc +++ b/src/server/main.cc @@ -1,19 +1,50 @@ +#include #include -#include +#include +#include +#include -using std::cout, std::cin, std::string; +using std::cout, std::endl, std::string; int main() { - while (true) { - string message; - cout << "Enter message: "; - cin >> message; + struct Message { + string content; + string username; + }; - if (message == "exit") { - break; - } + cout << "Initializing server"; + int serverSocket = socket(AF_INET, SOCK_STREAM, 0); + if (serverSocket == -1) { + // TODO error } + sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_port = htons(8080); + serverAddress.sin_addr.s_addr = inet_addr("127.0.0.50"); + + int bindStatus = bind(serverSocket, (struct sockaddr *)&serverAddress, + sizeof(serverAddress)); + if (bindStatus == -1) { + // TODO error + } + + int listenStatus = listen(serverSocket, 5); + if (listenStatus == -1) { + // TODO error + } + + int clientSocket = accept(serverSocket, nullptr, nullptr); + if (clientSocket == -1) { + // TODO error + } + + char buffer[1024] = { 0 }; + recv(clientSocket, buffer, sizeof(buffer), 0); + cout << "Message from client " << buffer << endl; + + close(serverSocket); + return 0; }