Compare commits

...

16 Commits

Author SHA1 Message Date
marcelb
9217ce91c4 Merge win-support to testing 2023-08-05 19:18:29 +02:00
4388730e9f Some clean and test, befor merge 2023-08-05 19:09:22 +02:00
2118270646 Add support Windows x64 OS 2023-08-04 23:10:18 +02:00
marcelb
043d0b26c6 Sync and async server 2023-07-20 20:09:11 +02:00
marcelb
e0feb8b2da Working on sync and async 2023-07-19 22:41:37 +02:00
marcelb
65c2cdbbaf Working on... 2023-07-19 20:55:17 +02:00
marcelb
9e05ee86f7 Time testing 2023-07-19 19:23:20 +02:00
marcelb
7be0716f61 Support for multiclient (async) 2023-07-18 21:12:26 +02:00
marcelb
915975fba8 Support Windows for client 2023-07-16 20:30:59 +02:00
marcelb
1f82e94296 Integrate client as variable in server class 2023-07-16 16:46:07 +02:00
Marcel Bandić
b6468bd148 Integration incoming and outgoing clients into one class - fix saving 2023-07-13 19:51:08 +02:00
Marcel Bandić
9e14e361e3 Integration incoming and outgoing clients into one class 2023-07-13 19:50:17 +02:00
marcelb
93d72e16aa Add timeout callculate and return string in obey methods 2023-06-27 21:29:05 +02:00
marcelb
24e2a4e4a9 Fix SSL handshake error, set timeout and SSL prefun calls 2023-06-27 16:31:14 +02:00
marcelb
3944512585 Timeout fix and string throw on exception v2 2023-06-22 18:53:42 +02:00
marcelb
2efacec104 Timeout fix and string throw on exception 2023-06-22 18:28:20 +02:00
13 changed files with 388 additions and 216 deletions

View File

@ -44,6 +44,7 @@
"stdexcept": "cpp", "stdexcept": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeinfo": "cpp" "typeinfo": "cpp",
"thread": "cpp"
} }
} }

31
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,31 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${fileDirname}/client.cpp",
"${fileDirname}/../src/*.cpp",
// "${fileDirname}../include/*/src/*.cpp",
"-o",
"${fileDirname}/client.o",
"-lssl",
"-lcrypto"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

View File

@ -4,9 +4,16 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <string.h> #include <string.h>
#include <arpa/inet.h>
#include <netdb.h> #if __linux__
#include <unistd.h> #include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif _WIN32
// #include <sstream>
#include <WinSock.h>
#include <ws2tcpip.h>
#endif
using namespace std; using namespace std;

View File

@ -3,17 +3,34 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <string.h> #include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/err.h> #include <openssl/err.h>
#if __linux__
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif _WIN32
#include <WinSock.h>
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#define ushort u_short
#define uint u_int
#endif
#include "ip.hpp" #include "ip.hpp"
using namespace std; using namespace std;
class client;
// class secure;
// class server;
/** /**
* Server klasa za TCP/IP soket * Server klasa za TCP/IP soket
* Instanca se incijalizira kada pokrećemo server * Instanca se incijalizira kada pokrećemo server
@ -21,12 +38,22 @@ using namespace std;
class server { class server {
public: public:
int sock; #if __linux__
int sock;
#elif _WIN32
WSADATA wsa;
SOCKET sock;
#endif
struct sockaddr_in addr; struct sockaddr_in addr;
SSL_CTX* securefds = NULL;
vector<thread> thr;
server (const ushort port, const uint limit = 1000); server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL);
~server (); ~server ();
void sync(void (*handlecli)(client&), const uint timeout = 100);
void async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100);
}; };
/** /**
@ -51,40 +78,28 @@ class secure {
class client { class client {
public: public:
int sock; // zajedničke
#if __linux__
int conn; // mijenja sock
#elif _WIN32
WSADATA wsa;
SOCKET conn; // mijenja sock
#endif
struct sockaddr_in addr; struct sockaddr_in addr;
SSL* ssl = NULL; SSL* ssl = NULL;
// server s klijentima
client (const string address, const ushort port, const uint timeout = 500, SSL_CTX* securefds = NULL); const server* srv;
~client (); // klijent sa serverom
bool tell (const string msg);
string obey (size_t byte_limit = 1024);
};
/**
* Klasa za inicijalizaciju dolaznih veza
* Definira se na serverskom tipu aplikacija i predstavlja identifikator klijenta
*/
class comming {
public:
const server *srv;
struct sockaddr_in addr;
int conn;
string ipv4; string ipv4;
string ipv6; string ipv6;
SSL* ssl = NULL;
// konstruktor za klijente bez servera
comming(const server *_srv, const uint timeout = 100, SSL_CTX* securefds = NULL); client (const string address, const ushort port, const uint timeout = 100, SSL_CTX* securefds = NULL);
~comming(); // konstruktor za klijente sa serverom
bool tell (const string msg); client (const server *_srv, const uint timeout = 100, SSL_CTX* securefds = NULL);
string obey (size_t byte_limit = 1024); ~client ();
bool push (const string msg);
string pull (size_t byte_limit = 1024);
}; };
#endif #endif

View File

@ -4,33 +4,85 @@
* Kontrustruktor varijable tipa server, prima port i limit za ograničenje liste klijenata na čekanju * Kontrustruktor varijable tipa server, prima port i limit za ograničenje liste klijenata na čekanju
*/ */
server::server (const ushort port, const uint limit) { server::server (const ushort port, const uint queue, SSL_CTX* _securefds) {
securefds = _securefds;
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY; addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port); addr.sin_port = htons(port);
#if _WIN32
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
throw string("[ERROR] WSA Startup. Detail: " + to_string(WSAGetLastError()));
}
#endif
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock <= 0) { if (sock <= 0) {
throw "[ERROR] Unable to open TCP socket "; throw string("[ERROR] Unable to open TCP socket ");
} }
int opt=1; int opt=1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { #if __linux__
throw "[ERROR] Unable to set REUSEADDR or REUSEPORT on socket "; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
} throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
}
#elif _WIN32
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt))) {
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
}
#endif
if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
throw "[ERROR] Unable to bind socket "; throw string("[ERROR] Unable to bind socket ");
} }
if (listen(sock, limit) < 0) { if (listen(sock, queue) < 0) {
throw "[ERROR] It is not possible to set the allowed number of waiting clients "; throw string("[ERROR] It is not possible to set the allowed number of waiting clients ");
} }
} }
/**
* Metoda za sinkroni rad s klijentima, prima pokazivač na funkciju i timeout;
* Funkcija handlecli prima referencu tipa client - važno za definiranje funkcija koje se šalju;
* Nije moguće proslijediti druge parametre;
*/
void server::sync(void (*handlecli)(client&), const uint timeout) {
do {
client cli(this, timeout, securefds);
handlecli(cli);
} while (true);
}
/**
* Metoda za asinkdorni rad s klijentima, prima limit, pokazivač na funkciju i timeout;
* Funkcija handlecli prima referencu tipa client - važno za definiranje funkcija koje se šalju;
* Nije moguće proslijediti druge parametre;
*/
void server::async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout) {
mutex io;
do {
for (uint i=0; i<limit; i++) {
thr.push_back(thread([&](){
client cli(this, timeout, securefds);
handlecli(cli, io);
}));
}
for (uint i=0; i<limit; i++) {
thr[i].join();
}
thr.clear();
} while (true);
}
/** /**
* Destruktor varijable tipa server * Destruktor varijable tipa server
*/ */
@ -39,13 +91,24 @@ server::server (const ushort port, const uint limit) {
server::~server () { server::~server () {
if (sock<=0) { if (sock<=0) {
throw "[ERROR] The socket is already closed "; throw string("[ERROR] The socket is already closed ");
} }
else if (close(sock) != 0) { else {
throw "[ERROR] Unable to close socket ";
}
#if __linux__
if (close(sock) != 0) {
throw string("[ERROR] Unable to close socket ");
}
#elif _WIN32
if (closesocket(sock) != 0) {
throw string("[ERROR] Unable to close socket ");
}
WSACleanup();
#endif
}
} }
/** /**
@ -53,9 +116,14 @@ server::~server () {
*/ */
secure::secure() { secure::secure() {
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
fds = SSL_CTX_new(SSLv23_client_method()); fds = SSL_CTX_new(SSLv23_client_method());
if (!fds) { if (!fds) {
throw "[ERROR] Creating SSL context "; throw string("[ERROR] Creating SSL context ");
} }
} }
@ -71,19 +139,19 @@ secure::secure(const string cert, const string priv) {
SSL_load_error_strings(); SSL_load_error_strings();
OpenSSL_add_all_algorithms(); OpenSSL_add_all_algorithms();
// Create an SSL context // Create an SSL context
fds = SSL_CTX_new(SSLv23_server_method()); fds = SSL_CTX_new(SSLv23_server_method());
if (!fds) { if (!fds) {
throw "[ERROR] Creating SSL context "; throw string("[ERROR] Creating SSL context ");
} }
// Load the server's certificate and private key files // Load the server's certificate and private key files
if (SSL_CTX_use_certificate_file(fds, cert.c_str(), SSL_FILETYPE_PEM) <= 0) { if (SSL_CTX_use_certificate_file(fds, cert.c_str(), SSL_FILETYPE_PEM) <= 0) {
throw "[ERROR] Loading certificate file "; throw string("[ERROR] Loading certificate file ");
} }
if (SSL_CTX_use_PrivateKey_file(fds, priv.c_str(), SSL_FILETYPE_PEM) <= 0) { if (SSL_CTX_use_PrivateKey_file(fds, priv.c_str(), SSL_FILETYPE_PEM) <= 0) {
throw "[ERROR] Loading private key file "; throw string("[ERROR] Loading private key file ");
} }
} }
@ -105,9 +173,15 @@ secure::~secure () {
client::client(const string address, const ushort port, const uint timeout, SSL_CTX* securefds) { client::client(const string address, const ushort port, const uint timeout, SSL_CTX* securefds) {
sock = socket(AF_INET, SOCK_STREAM, 0); #if _WIN32
if (sock < 0) { if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
throw "[ERROR] Unable to open TCP socket "; throw string("[ERROR] Unable to set WinSock " + to_string(WSAGetLastError()));
}
#endif
conn = socket(AF_INET, SOCK_STREAM, 0);
if (conn < 0) {
throw string("[ERROR] Unable to open TCP socket ");
} }
const string _address = isIPAddress(address) ? address : ipFromDomain(address); const string _address = isIPAddress(address) ? address : ipFromDomain(address);
@ -116,31 +190,99 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
addr.sin_addr.s_addr = inet_addr(_address.c_str()); addr.sin_addr.s_addr = inet_addr(_address.c_str());
addr.sin_port = htons(port); addr.sin_port = htons(port);
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) { if (connect(conn, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) {
throw "Unable to connect to server "; throw string("Unable to connect to server ");
} }
struct timeval tv; #if __linux__
tv.tv_sec = 0; struct timeval tv;
tv.tv_usec = timeout*1000; tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) { if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw "[ERROR] Unable to set timeout "; throw string("[ERROR] Unable to set timeout ");
} }
#elif _WIN32
DWORD tv = timeout;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) {
throw string("[ERROR] Unable to set timeout ");
}
#endif
if (securefds) { if (securefds) {
ssl = SSL_new(securefds); ssl = SSL_new(securefds);
if (!ssl) { if (!ssl) {
throw "[ERROR] Creating SSL object "; throw string("[ERROR] Creating SSL object ");
} }
SSL_set_fd(ssl, sock); SSL_set_fd(ssl, conn);
}
// Perform the SSL handshake // Perform the SSL handshake
if (SSL_connect(ssl) <= 0) { if (SSL_connect(ssl) <= 0) {
SSL_free(ssl); SSL_free(ssl);
throw "[ERROR] Performing SSL handshake "; throw string("[ERROR] Performing SSL handshake ");
}
} }
}
/**
* Konstruktor varijable tipa commint
* Prima pokazivač na inicijaliziranu varijablu tipa, port,
* i opcijonalno dozvoljeno vrijeme čekanja servera i deskriptor datoteke
* SSL certifikat za sigurne komunikacije
*/
client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
srv = _srv;
socklen_t len = sizeof(struct sockaddr_in);
if ((conn = accept(srv->sock, (struct sockaddr *)&(srv->addr), (socklen_t*)&len)) < 0) {
throw string("[ERROR] Unable to accept client connection ");
}
#if __linux__
struct timeval tv;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout ");
}
#elif _WIN32
DWORD tv = timeout;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) {
throw string("[ERROR] Unable to set timeout ");
}
#endif
if (securefds) {
ssl = SSL_new(securefds);
if (!ssl) {
throw string("[ERROR] Creating SSL object ");
}
SSL_set_fd(ssl, conn);
// Perform SSL handshake
if (SSL_accept(ssl) <= 0) {
SSL_free(ssl);
throw string("[ERROR] Performing SSL handshake ");
}
}
char ipv4_buff[INET_ADDRSTRLEN];
char ipv6_buff[INET6_ADDRSTRLEN];
inet_ntop(AF_INET, &(srv->addr.sin_addr), ipv4_buff, INET_ADDRSTRLEN);
ipv4 = ipv4_buff;
inet_ntop(AF_INET6, &(srv->addr.sin_addr), ipv6_buff, INET6_ADDRSTRLEN);
ipv6 = ipv6_buff;
} }
@ -156,12 +298,21 @@ client::~client () {
SSL_free(ssl); SSL_free(ssl);
} }
if (sock <= 0) { if (conn <= 0) {
throw "[ERROR] The socket is already closed "; throw string("[ERROR] The socket is already closed ");
} }
else if (close(sock) != 0) { else {
throw "[ERROR] Unable to close socket "; #if __linux__
if (close(conn) != 0) {
throw string("[ERROR] Unable to close socket ");
}
#elif _WIN32
if (closesocket(conn) != 0) {
throw string("[ERROR] Unable to close socket ");
}
//WSACleanup();
#endif
} }
} }
@ -173,13 +324,13 @@ client::~client () {
*/ */
bool client::tell (const string msg) { bool client::push (const string msg) {
size_t sended = 0; size_t sended = 0;
if (ssl) { if (ssl) {
sended = SSL_write(ssl, msg.c_str(), msg.length()); sended = SSL_write(ssl, msg.c_str(), msg.length());
} }
else { else {
sended = write(sock, msg.c_str(), msg.length()); sended = send(conn, msg.c_str(), msg.length(), 0);
} }
return sended == msg.length(); return sended == msg.length();
} }
@ -190,121 +341,18 @@ bool client::tell (const string msg) {
* Vraća string primljene poruke * Vraća string primljene poruke
*/ */
string client::obey (size_t byte_limit) { string client::pull (size_t byte_limit) {
char res[byte_limit] = {0}; char res[byte_limit] = {0};
if (ssl) { if (ssl) {
SSL_read(ssl, res, byte_limit); SSL_read(ssl, res, byte_limit);
} }
else { else {
read(sock , res, byte_limit); recv(conn , res, byte_limit, 0);
} }
return (string) res; return string(res);
}
/**
* Konstruktor varijable tipa commint
* Prima pokazivač na inicijaliziranu varijablu tipa, port,
* i opcijonalno dozvoljeno vrijeme čekanja servera i deskriptor datoteke
* SSL certifikat za sigurne komunikacije
*/
comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) {
srv = _srv;
socklen_t len = sizeof(struct sockaddr_in);
if ((conn = accept(srv->sock, (struct sockaddr *)&(srv->addr), (socklen_t*)&len)) < 0) {
throw "[ERROR] Unable to accept client connection ";
}
struct timeval tv;
tv.tv_sec = 1; // za sad 2 sekunde timeout, harkodirano
tv.tv_usec = 0;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw "[ERROR] Unable to set timeout ";
}
if (securefds) {
ssl = SSL_new(securefds);
if (!ssl) {
throw "[ERROR] Creating SSL object ";
}
SSL_set_fd(ssl, conn);
// Perform SSL handshake
if (SSL_accept(ssl) <= 0) {
SSL_free(ssl);
throw "[ERROR] Performing SSL handshake ";
}
}
char ipv4_buff[INET_ADDRSTRLEN];
char ipv6_buff[INET6_ADDRSTRLEN];
inet_ntop(AF_INET, &(srv->addr.sin_addr), ipv4_buff, INET_ADDRSTRLEN);
ipv4 = ipv4_buff;
inet_ntop(AF_INET6, &(srv->addr.sin_addr), ipv6_buff, INET6_ADDRSTRLEN);
ipv6 = ipv6_buff;
}
/**
* Destruktor varijable tipa comming
*/
comming::~comming() {
if (ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
}
if (conn <= 0) {
throw "[ERROR] The socket is already closed ";
}
else if (close(conn) != 0) {
throw "[ERROR] Unable to close socket ";
}
}
/**
* Metoda klase comming za slanje podataka preko soketa
* Prima string koji će biti poslan
* Vraća logički statu poređenja psolanih karaktera i karaktera u stringu
*/
bool comming::tell (const string msg) {
ssize_t sended = 0;
if (ssl) {
sended = SSL_write(ssl, msg.c_str(), msg.length());
}
else {
sended = write(conn, msg.c_str(), msg.length());
}
return sended == msg.length();
} }
/**
* Metoda klase comming za primanje poruke preko soketa
* Prima dozvoljeni broj karaktera koji će primiti
* Vraća string primljene poruke
*/
string comming::obey (size_t byte_limit) {
char res[byte_limit] = {0};
if (ssl) {
SSL_read(ssl, res, byte_limit);
}
else {
read(conn , res, byte_limit);
}
return (string) res;
}

View File

@ -1,4 +1,5 @@
#include <iostream> #include <iostream>
#include <string>
#include "../lib/tcp_socket.hpp" #include "../lib/tcp_socket.hpp"
@ -8,11 +9,36 @@ int main() {
try { try {
secure crypto; uint n = 10000;
client myserver("localhost", 5000, 500, crypto.fds);
string sends = "Hello world!"; vector<thread> thr;
cout << myserver.tell(sends) << " " << sends.length() << endl; for (uint i=0; i<n; i++) {
cout << myserver.obey(); thr.push_back(thread([](uint a){
client myserver("127.0.0.1", 5000, 500);
string sends = "Hello world " + to_string(a);
myserver.push(sends);
cout << myserver.pull() << endl;
}, i));
}
for (uint i=0; i<n; i++) {
thr[i].join();
}
// secure crypto;
// cout << "init cert " << endl;
// client myserver("127.0.0.1", 5000, 5000, crypto.fds);
client myserver("localhost", 8000, 5000, crypto.fds);
// client myserver("localhost", 5000);
// cout << "init client " << endl;
// string sends = "Hello world!";
// cout << myserver.push(sends) << " " << sends.length() << endl;
// cout << "wait client " << endl;
// cout << myserver.pull();
} }
catch (const string err) { catch (const string err) {

BIN
test/client.exe Normal file

Binary file not shown.

Binary file not shown.

1
test/compile-client.ps1 Normal file
View File

@ -0,0 +1 @@
g++ client.cpp ../src/* -o client.exe -lssl -lcrypto -lws2_32

1
test/compile-server.ps1 Normal file
View File

@ -0,0 +1 @@
g++ server.cpp ../src/* -o server.exe -lssl -lcrypto -lws2_32

View File

@ -5,35 +5,77 @@
using namespace std; using namespace std;
int main() { int main() {
try{ try{
cout << "init server " << endl; // cout << "init cert " << endl;
server myserver(5000, 10); // secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init cert " << endl; // cout << "init server " << endl;
secure crypto ("../example/cert.pem", "../example/privkey.pem"); // server myserver(5000, 100, crypto.fds);
cout << "init client " << endl; cout << "init cert " << endl;
secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init server " << endl;
server myserver(8000, 100, crypto.fds);
// cout << "init client " << endl;
comming myclient(&myserver, 1000, crypto.fds); // // client myclient(&myserver, 100, crypto.fds);
cout << "wait client " << myclient.ipv4 << endl; // // comming myclient(&myserver, 100);
// myserver.accept();
// // cout << "wait client " << myclient.ipv4 << endl;
// cout << "wait client " << myserver.cli->ipv4 << endl;
string fromclient = myclient.obey(); // string fromclient = myserver.cli->pull();
cout << "tell client " << fromclient << endl; // // string fromclient = myclient.pull();
sleep(5); // cout << "tell client " << fromclient << endl;
myclient.tell(fromclient); // // usleep(600*1000);
// myclient.~comming(); // sleep(5);
// myserver.cli->push(fromclient);
string fromclient = myserver.cli->pull();
// string fromclient = myclient.pull();
cout << "tell client " << fromclient << endl;
// usleep(600*1000);
//sleep(5);
myserver.cli->push(fromclient);
// myclient.push(fromclient);
// myclient.~comming();
// while (true) { // while (true) {
// comming myclient(&myserver, 1000); // comming myclient(&myserver, 1000);
// string fromclient = myclient.obey(); // string fromclient = myclient.obey();
// cout << fromclient << " " << myclient.conn << endl; // cout << fromclient << " " << myclient.conn << endl;
// cout << "Poslano: " << myclient.tell(fromclient) << "Veličin: " << fromclient.length() << endl; // cout << "Poslano: " << myclient.tell(fromclient) << "Veličin: " << fromclient.length() << endl;
// // myclient.~comming(); // // myclient.~comming();
// cout << "IPv4 " << myclient.ipv4 << " ipv6 " << myclient.ipv6; // cout << "IPv4 " << myclient.ipv4 << " ipv6 " << myclient.ipv6;
// } // }
// sleep(80); // sleep(80);
}
catch(const string err) {
cout << err << endl; server myserver(5000, 100);
} myserver.async(8, [](client &cli, mutex &io) {
cout << "Klijent " << cli.ipv4 << endl;
string fromclient = cli.pull();
io.lock();
cout << "S klijenta " << fromclient << endl;
io.unlock();
// fromclient += teststr;
cli.push(fromclient);
}, 200);
// string teststr = " Idemooo";
// myserver.sync([](client &cli, mutex &io) {
// cout << "Klijent " << cli.ipv4 << endl;
// string fromclient = cli.pull();
// io.lock();
// cout << "S klijenta " << fromclient << endl;
// io.unlock();
// // fromclient += teststr;
// cli.push(fromclient);
// });
}
catch(const string err) {
cout << err << endl;
}
return 0; return 0;
} }

BIN
test/server.exe Normal file

Binary file not shown.

Binary file not shown.