Initial commit

This commit is contained in:
leca 2024-08-14 00:37:30 +03:00
parent 2d775a08db
commit f6d338571d
5 changed files with 2803943 additions and 0 deletions

BIN
client Executable file

Binary file not shown.

83
client.cpp Normal file
View File

@ -0,0 +1,83 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <chrono>
#include <iostream>
#include <netinet/in.h>
#include <string>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
using std::cerr;
using std::cout;
using std::endl;
using std::ofstream;
using std::stoi;
using std::string;
using std::thread;
#define DEBUG
class myClient {
string name;
unsigned short port;
unsigned short time;
sockaddr_in serverAddress;
int clientSocket;
public:
myClient(string name, unsigned short port, unsigned short time) {
this->name = name;
this->port = port;
this->time = time;
this->clientSocket = socket(AF_INET, SOCK_STREAM, 0);
this->serverAddress.sin_family = AF_INET;
this->serverAddress.sin_port = htons(port);
this->serverAddress.sin_addr.s_addr = INADDR_ANY;
if (connect(this->clientSocket, (sockaddr *)&serverAddress,
sizeof(serverAddress)) < 0) {
cerr << "Cannot connect to server!" << endl;
exit(-2);
}
thread t(&myClient::sendData, this);
t.join();
}
void sendData() {
while (1) {
std::this_thread::sleep_for(std::chrono::seconds(this->time));
send(this->clientSocket, this->name.c_str(), strlen(this->name.c_str()), 0);
}
}
~myClient() {
close(this->clientSocket);
}
};
int main(int argc, char *argv[]) {
if (argc < 4) {
cerr << "Usage: " << argv[0] << " [name] [port] [time(s)]." << endl;
exit(-1);
}
const string name = argv[1];
const unsigned short port = stoi(argv[2]);
const unsigned short time = stoi(argv[3]);
#ifdef DEBUG
cout << "[DEBUG] Starting a client with parameters: " << endl
<< "Name: " << name << endl
<< "Port: " << port << endl
<< "Time: " << time << endl;
#endif
myClient c(name, port, time);
while(1);
}

2803734
log.txt Normal file

File diff suppressed because it is too large Load Diff

BIN
server Executable file

Binary file not shown.

126
server.cpp Normal file
View File

@ -0,0 +1,126 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <fstream>
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
#include <vector>
using std::cerr;
using std::cout;
using std::endl;
using std::ofstream;
using std::stoi;
using std::thread;
using std::vector;
#define DEBUG
class myServer {
unsigned short port;
int serverSocket;
sockaddr_in serverAddress;
vector<thread> clients;
ofstream logFile;
public:
myServer(unsigned short port) {
this->port = port;
this->logFile.open("log.txt", std::fstream::out | std::fstream::app);
if (!this->logFile.is_open()) {
cerr << "Error! Cannot open logfile." << endl;
}
this->serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (this->serverSocket < 0) {
cerr << "Error! Cannot open a socket." << endl;
exit(-2);
}
this->serverAddress.sin_family = AF_INET;
this->serverAddress.sin_port = htons(port);
this->serverAddress.sin_addr.s_addr = INADDR_ANY;
start();
}
void start() {
if (bind(this->serverSocket, (sockaddr *)&serverAddress,
sizeof(serverAddress)) < 0) {
cerr << "Cannot bind a socket!" << endl;
exit(-3);
}
if (listen(this->serverSocket, 10) < 0) {
cerr << "Cannot listen a socket!" << endl;
exit(-4);
}
#ifdef DEBUG
cout << "[DEBUG] server started on port " << this->port << "." << endl;
this->logFile << "[DEBUG] server started on port " << this->port << "." << endl;
#endif
acceptClients();
}
void handleClient(unsigned short clientSocket) {
#ifdef DEBUG
cout << "Starting handle of a client with socket descriptor "
<< clientSocket << endl
<< ".";
#endif
while (true) {
char buffer[128] = {0};
recv(clientSocket, buffer, 128, 0);
cout << "Received message from a client [" << clientSocket
<< "]: " << buffer << "." << endl;
this->logFile << "["
<< boost::posix_time::microsec_clock::universal_time()
<< "]" << buffer << endl;
}
}
void acceptClients() {
while (true) {
unsigned short clientSocket =
accept(this->serverSocket, nullptr, nullptr);
#ifdef DEBUG
this->logFile << "["
<< boost::posix_time::microsec_clock::universal_time()
<< "] {DEBUG} A client " << clientSocket
<< " has connected";
#endif
thread clientHandler(&myServer::handleClient, this, clientSocket);
this->clients.push_back(std::move(clientHandler));
}
for (int i = 0; i < this->clients.size(); i ++) {
this->clients[i].join();
}
}
~myServer() { close(this->serverSocket); }
};
int main(int argc, char *argv[]) {
if (argc < 2) {
cerr << "Usage:" << argv[0] << " [port]." << endl;
exit(-1);
}
const unsigned short port = stoi(argv[1]);
#ifdef DEBUG
cout << "[DEBUG] Port: " << port << "." << endl;
#endif
myServer s(port);
while(1);
}