👔 Add server logic
This commit is contained in:
parent
0190c402cd
commit
810fb50ddd
1 changed files with 40 additions and 9 deletions
|
@ -1,19 +1,50 @@
|
|||
#include <arpa/inet.h>
|
||||
#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() {
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Reference in a new issue