server upd
added python client
This commit is contained in:
+57
-16
@@ -1,9 +1,20 @@
|
||||
#include "server.hpp"
|
||||
#include <iostream>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/ip.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "server.hpp"
|
||||
#include "net_util.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
|
||||
Server::Server(const Config& config)
|
||||
: address(config.get_address()), port(config.get_port()), sockfd(-1) {
|
||||
@@ -43,18 +54,41 @@ void Server::setup() {
|
||||
std::cout << "Server started on " << address << ":" << port << "\n";
|
||||
}
|
||||
|
||||
void Server::handle_connection(int connectionfd) {
|
||||
char rbuf[64] = {};
|
||||
ssize_t n = read(connectionfd, rbuf, sizeof(rbuf) - 1);
|
||||
if (n < 0) {
|
||||
std::cerr << "Error reading client packet\n";
|
||||
return;
|
||||
int32_t Server::handle_connection(int connectionfd) {
|
||||
std::cout << "Waiting for data from " << connectionfd << "\n";
|
||||
|
||||
uint32_t len_net = 0;
|
||||
errno = 0;
|
||||
if (read_full(connectionfd, &len_net, sizeof(len_net)) != 0) {
|
||||
print_error(errno == 0 ? "EOF from client" : "Error reading length");
|
||||
return -1;
|
||||
}
|
||||
std::cout << "Client says: " << std::string(rbuf, n) << "\n";
|
||||
ssize_t written = write(connectionfd, "Hello, client!\n", 14);
|
||||
if (written < 0) {
|
||||
std::cerr << "Error writing to client\n";
|
||||
|
||||
uint32_t len = ntohl(len_net);
|
||||
|
||||
if (len > k_max_message_size) {
|
||||
print_error("Message length is too long");
|
||||
std::cerr << len << " > " << k_max_message_size << "\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string message(len, '\0');
|
||||
if (len > 0) {
|
||||
if (read_full(connectionfd, message.data(), len) != 0) {
|
||||
print_error("Error reading payload");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Client says: " << message << "\n";
|
||||
|
||||
const std::string reply = "world";
|
||||
uint32_t reply_len_net = htonl(static_cast<uint32_t>(reply.size()));
|
||||
|
||||
if (write_all(connectionfd, &reply_len_net, sizeof(reply_len_net)) != 0) return -1;
|
||||
if (write_all(connectionfd, reply.data(), reply.size()) != 0) return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Server::run() {
|
||||
@@ -67,7 +101,14 @@ void Server::run() {
|
||||
if (connectionfd < 0) {
|
||||
continue;
|
||||
}
|
||||
handle_connection(connectionfd);
|
||||
std::cout << "Accepted connection from " << connectionfd << "\n";
|
||||
while (true) {
|
||||
int32_t err = handle_connection(connectionfd);
|
||||
if (err) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::cout << "Closing connection from " << connectionfd << "\n";
|
||||
close(connectionfd);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user