👔 Add server logic

This commit is contained in:
Simon V. Lejel 2024-02-10 12:08:37 +01:00
parent 0190c402cd
commit 810fb50ddd
Signed by: sl
GPG key ID: 6544A0430A2CFFAD

View file

@ -1,19 +1,50 @@
#include <arpa/inet.h>
#include <iostream> #include <iostream>
#include <string> #include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
using std::cout, std::cin, std::string; using std::cout, std::endl, std::string;
int main() { int main() {
while (true) { struct Message {
string message; string content;
cout << "Enter message: "; string username;
cin >> message; };
if (message == "exit") { cout << "Initializing server";
break; 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; return 0;
} }