diff --git a/.env b/.env new file mode 100644 index 0000000..c7ab229 --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +SERVER_IP=172.30.0.39 +SERVER_PORT=3030 +CLIENT_IP=172.30.0.40 +NET_SUBNET= \ No newline at end of file diff --git a/client.cpp b/client.cpp new file mode 100644 index 0000000..e2855e2 --- /dev/null +++ b/client.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include + +#include +#include + +void load_env(const std::string& path) { + std::ifstream f(path); + std::string line; + while (std::getline(f, line)) { + if (line.empty() || line[0] == '#') continue; + auto pos = line.find('='); + if (pos == std::string::npos) continue; + + std::string key = line.substr(0, pos); + std::string val = line.substr(pos + 1); + +#ifdef _WIN32 + _putenv_s(key.c_str(), val.c_str()); +#else + setenv(key.c_str(), val.c_str(), 1); +#endif + } +} +namespace std {} + +int main(void){ + load_env(".env"); + // Declare variables + const char *server_ip = std::getenv("SERVER_IP"); + const int server_port = std::atoi(std::getenv("SERVER_PORT")); + char client_message[1024]; + char server_message[1024]; + // Create socket: + + // Send connection request to server, be sure to set por tand IP the same as server-side + + // Get input from the user: + printf("Enter message sent to the server: "); + // fgets(client_message); + + // Send the message to server: + + // Receive the server's response: + + + printf("Server's response: %s\n", server_message); + + // Close the socket + + return 0; +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2683ba5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,48 @@ +services: + server: + image: gcc:13 + working_dir: /app + volumes: + - ./:/app:rw + env_file: + - .env + networks: + net-hw1: + ipv4_address: ${SERVER_IP} + command: > + bash -lc " + set -euo pipefail; + g++ -std=c++17 -O2 -Wall -Wextra -pedantic /app/server.cpp -o /tmp/server; + echo '[server] IP='$(hostname -i)' SERVER_IP='${SERVER_IP}' SERVER_PORT='${APP_PORT}; + exec /tmp/server + " + + client: + image: gcc:13 + working_dir: /app + volumes: + - ./:/app:rw + env_file: + - .env + networks: + net-hw1: + ipv4_address: ${CLIENT_IP} + depends_on: + - server + stdin_open: true + tty: true + command: > + bash -lc " + set -euo pipefail; + g++ -std=c++17 -O2 -Wall -Wextra -pedantic /app/client.cpp -o /tmp/client; + echo '[client] IP='$(hostname -i)' CLIENT_IP='${CLIENT_IP}' SERVER_IP='${SERVER_IP}' APP_PORT='${APP_PORT}; + exec /tmp/client + " + +networks: + net-hw1: + name: net-hw1 + driver: bridge + ipam: + config: + - subnet: ${NET_SUBNET} diff --git a/server b/server new file mode 100755 index 0000000..98777c0 Binary files /dev/null and b/server differ diff --git a/server.cpp b/server.cpp new file mode 100644 index 0000000..12965ab --- /dev/null +++ b/server.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include + +int main(void){ + + // Declare variables + const char *server_ip = "127.0.0.1"; + const int server_port = 8080; + char client_message[1024]; + char server_message[1024]; + // Create socket + + + // Bind to the set port and IP + printf("Done with binding with IP: %s, Port: %d\n", server_ip, server_port); + + // Listen for clients: + const char *client_ip = "127.0.0.1"; + const int client_port = 12345; + + // Accept an incoming connection + printf("Client connected at IP: %s and port: %i\n", client_ip, client_port); + + // Receive client's message + printf("Msg from client: %s\n", client_message); + + // Respond to client + strcpy(server_message, "Hello from server"); + + // Close the socket + + return 0; +} \ No newline at end of file