-1

I created two linux containers, and want to use one as server and the other one as client.

So I added two cpp files below into these two containers.

hello_server.cpp

#include <iostream>
#include <netinet/in.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

int main (int argc, char* argv[]) {
    if (argc != 2) {
        std::cout << "wrong input" << std::endl;
        exit(1);
    }

    struct sockaddr_in serv_addr;
    struct sockaddr_in client_addr;

    char message[] = "Hello World!";

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(atoi(argv[1]));
    
    int serv_sock = socket(PF_INET, SOCK_STREAM, 0);
    if (serv_sock == -1) {
        std::cout << "socket() error" << std::endl;
        exit(1);
    }
        
    if (bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1) {
        std::cout << "bind() error" << std::endl;
        exit(1);
    }
    
    if (listen(serv_sock, 5) == 1) {
        std::cout << "listen() error" << std::endl;
        exit(1);
    }

    socklen_t client_addr_size = sizeof(client_addr);
    int client_sock = accept(serv_sock, (struct sockaddr*)&client_addr, &client_addr_size);
    if (client_sock == -1) {
        std::cout << "accept() error" << std::endl;
        exit(1);
    }
    //send(int sockfd, const void *buf, size_t len, int flags);
    send(client_sock, message, sizeof(message), 0);
    
    close(client_sock);
    close(serv_sock);

    return 0;
}

hello_client.cpp

#include <iostream>
#include <netinet/in.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
    if (argc != 3) {
        std::cout << "wrong input" << std::endl;
        exit(1);
    }
    struct sockaddr_in serv_addr;

    char message[30];

    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
    serv_addr.sin_port = htons(atoi(argv[2]));


    int client_sock = socket(PF_INET, SOCK_STREAM, 0);
    if (client_sock == -1) {
        std::cout << "socket() error" << std::endl;
        exit(1);
    }

    if (connect(client_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1) {
        std::cout << "connect() error" << std::endl;
        exit(1);
    }

    int strlen = recv(client_sock, message, sizeof(message) - 1, 0);
    if (strlen == -1) {
        std::cout << "read() error" << std::endl;
        exit(1);
    }

    std::cout << message << std::endl;
    close(client_sock);
    return 0;
}

I start my server with:

g++ hello_server.cpp -o hserver
./hserver 8888

What should I do to let my client (in another container) connect to it?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Andrew
  • 1
  • 2
  • The client and server look like a fairly routine C TCP service. Have you read background material like [Networking in Compose](https://docs.docker.com/compose/networking/), or related questions like [How to communicate between Docker containers via "hostname"](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname)? – David Maze Apr 11 '22 at 13:48

1 Answers1

1

Assuming the code itself "works", the question is really a docker question and there are ( at least ) three possibilities that come to mind.

Your first option is to run your containers and expose the underlying host network directly. This is the easiest approach in that your containers will simply use the underlying host network and therefore will behave (in terms of networking) just like regular applications that aren't containerized.

See here: https://docs.docker.com/engine/reference/run/#network-settings specifically for --net=host

The second option is to expose the listening port through the docker firewall for your application.

See here: https://docs.docker.com/config/containers/container-networking/

The third option is to use some management application, like kubernetes to deploy your services in a POD. kubernetes allows you associate applications and allow them to communicate (over TCP for example) as well as orchestrate the behavior (start/stop/run) etc.

Chad
  • 18,706
  • 4
  • 46
  • 63