updates
This commit is contained in:
4
.env
Normal file
4
.env
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
SERVER_IP=172.30.0.39
|
||||||
|
SERVER_PORT=3030
|
||||||
|
CLIENT_IP=172.30.0.40
|
||||||
|
NET_SUBNET=
|
||||||
55
client.cpp
Normal file
55
client.cpp
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
48
docker-compose.yml
Normal file
48
docker-compose.yml
Normal file
@@ -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}
|
||||||
36
server.cpp
Normal file
36
server.cpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user