From 7a0ef3aca8bc6a75991dcd366586730766b65e84 Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Sat, 2 Mar 2024 17:31:55 +0100 Subject: [PATCH] Created a new exemple with a server client interface --- sandbox/client/client.cpp | 32 +++++++++++++++----------------- sandbox/commun.h | 8 ++++++++ sandbox/server/server.cpp | 29 ++++++++++++----------------- 3 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 sandbox/commun.h diff --git a/sandbox/client/client.cpp b/sandbox/client/client.cpp index 0581aa2..3a4601c 100644 --- a/sandbox/client/client.cpp +++ b/sandbox/client/client.cpp @@ -1,28 +1,26 @@ -#include -#include +#include "../commun.h" -#include "bakanet.h" using namespace Bk::Net; int main() { - IpAddress ip("127.0.0.1"); - Socket sock(ip, 8080, IpProtocol::TCP); + log(dns_lookup("edus2.rpn.ch", IpVersion::IPv4)[0]) - bool status = sock.conn(); - if(!status) perror("Couldn't connect."); + IpAddress ip("127.0.0.1"); + Socket sock(ip, 9000, IpProtocol::TCP); - std::string msg = "GET / HTTP/1.1\r\n"; - std::vector data(msg.begin(), msg.end()); + if(!sock.conn()) { + perror("Couldn't connect to the end point."); + exit(1); + } - sock.write(data); + Packet packet, meta; + std::string str("Hello world"); + + packet.push(str.c_str(), str.length()); + meta.push(packet.size()); - std::vector r_data; - while((r_data = sock.recv(1024)).size()) - { - log(r_data.size()) - std::string data_to_str(r_data.begin(), r_data.end()); - log(data_to_str) - } + sock.write(meta.payload); + sock.write(packet.payload); return 0; } \ No newline at end of file diff --git a/sandbox/commun.h b/sandbox/commun.h new file mode 100644 index 0000000..e837fb9 --- /dev/null +++ b/sandbox/commun.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include +#include +#include + +#define log(str) std::cout << str << "\n"; \ No newline at end of file diff --git a/sandbox/server/server.cpp b/sandbox/server/server.cpp index 8967083..82a39f8 100644 --- a/sandbox/server/server.cpp +++ b/sandbox/server/server.cpp @@ -1,32 +1,27 @@ -#include -#include +#include "../commun.h" -#include "bakanet.h" using namespace Bk::Net; int main() { - bool running = true; - IpAddress ip("127.0.0.1"); - Socket sock(ip, 8080, IpProtocol::TCP); - running = sock.init() && sock.start(50); - - std::string msg = "HTTP/1.1 200 OK\r\n" - "Content-Type: text/html\r\n\r\n" - "

Hello World!

"; - std::vector data(msg.begin(), msg.end()); + Socket sock(ip, 9000, IpProtocol::TCP); + + bool running = sock.init() && sock.start(50); while (running) { Connection conn; if ((conn = sock.ack()) > 0) { - auto r_data = sock.recv(conn, 2 * 2048); - std::string data_to_str(r_data.begin(), r_data.end()); - log(data_to_str) - //Sending data step - sock.write(conn, data); + log("New Connection") + Packet meta_data(sock.recv(conn, 4)); + int length = meta_data.pull(); + if (length > 0) + { + Packet data(sock.recv(conn, length + 2)); + log(data.pull(length).release()); + } close(conn); } }