tcp-server/client a bit improved

This commit is contained in:
2021-11-05 15:27:21 +01:00
parent 159845bb9b
commit 865e198a5b
6 changed files with 85 additions and 47 deletions

View File

@@ -23,7 +23,7 @@ TcpNet::TcpNet() {}
#define TCPNET_BUFFER_SIZE 16256
int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::string)> process_request) {
int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::string)> process_request) const {
int sockfd, newsockfd;
socklen_t clilen;
@@ -40,6 +40,15 @@ int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::s
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
// this allows immediate bind after exit of ml
int reuse = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
error("setsockopt(SO_REUSEADDR) failed");
#ifdef SO_REUSEPORT
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0)
error("setsockopt(SO_REUSEPORT) failed");
#endif
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
@@ -53,22 +62,26 @@ int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::s
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,TCPNET_BUFFER_SIZE);
n = read(newsockfd,buffer,TCPNET_BUFFER_SIZE - 1);
if (n < 0) error("ERROR reading from socket");
while (true) {
bzero(buffer,TCPNET_BUFFER_SIZE);
n = read(newsockfd,buffer,TCPNET_BUFFER_SIZE - 1);
if (n == 0) break; // nothing to read from client anymore
if (n < 0) error("ERROR reading from socket");
std::string request{buffer};
std::pair<bool, std::string> response = process_request(request);
shutdown = response.first;
std::string response_str = response.second;
std::string request{buffer};
std::pair<bool, std::string> response = process_request(request);
shutdown = response.first;
std::string response_str = response.second;
n = write(newsockfd, response_str.c_str(), response_str.size());
if (n < 0)
error("ERROR writing to socket");
requests_processed++;
}
n = write(newsockfd, response_str.c_str(), response_str.size());
if (n < 0)
error("ERROR writing to socket");
close(newsockfd);
requests_processed++;
}
close(sockfd);
@@ -76,10 +89,12 @@ int TcpNet::server(int portno, std::function<std::pair<bool, std::string>(std::s
return requests_processed;
}
std::string TcpNet::client(const std::string address, int portno, const std::string &content) {
std::vector<std::string> TcpNet::client(const std::string &address, int portno, const std::vector<std::string> &requests) const {
int sockfd, n;
struct sockaddr_in serv_addr;
struct hostent *server;
std::vector<std::string> responses;
char buffer[TCPNET_BUFFER_SIZE];
@@ -102,17 +117,29 @@ std::string TcpNet::client(const std::string address, int portno, const std::str
error("ERROR connecting");
n = write(sockfd, content.c_str(), strlen(content.c_str()));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer, TCPNET_BUFFER_SIZE);
n = read(sockfd,buffer, TCPNET_BUFFER_SIZE - 1);
if (n < 0)
error("ERROR reading from socket");
responses.reserve(requests.size());
for(const auto &req : requests) {
n = write(sockfd, req.c_str(), strlen(req.c_str()));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer, TCPNET_BUFFER_SIZE);
n = read(sockfd,buffer, TCPNET_BUFFER_SIZE - 1);
if (n < 0)
error("ERROR reading from socket");
responses.push_back(std::string(buffer));
}
close(sockfd);
return std::string(buffer);
return responses;
}
std::string TcpNet::client(const std::string &address, int portno, const std::string &request) const {
std::vector<std::string> c{request};
auto response = client(address, portno, c);
return response[0];
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <vector>
class TcpNet {
@@ -9,12 +10,11 @@ public:
TcpNet();
// starts listening on port
int server(int port, std::function<std::pair<bool, std::string>(std::string)> process_request);
int server(int port, std::function<std::pair<bool, std::string>(std::string)> process_request) const;
// writes content to server on address:port and returns response
std::string client(const std::string address, int port, const std::string &content);
// writes request to server on address:port and returns response
std::string client(const std::string &address, int portno, const std::string &request) const;
// TODO add support for vector of strings to be sent to server
// std::vector<std::string> client(const std::string address, int port, const std::vector<std::string> &content);
std::vector<std::string> client(const std::string &address, int portno, const std::vector<std::string> &requests) const;
};