Hello,
I am trying to achive the IPC socket communication between containers on rcar h3 bboard. I have created crosscompile containers using buildx. When I try running it containers created but communication not happening.
here is the dockerfile :
- client:
FROM alpine
RUN apk add build-base
WORKDIR /app
RUN apk add --no-cache g++
COPY client.cpp .
#RUN g++ client.cpp -o client
RUN g++ "-DARCH=\"`uname -a`\"" client.cpp -o client
CMD ["./client"]
2.server
FROM alpine
RUN apk add build-base
WORKDIR /app
RUN apk add --no-cache g++
COPY server.cpp .
#RUN g++ server.cpp -o server
RUN g++ "-DARCH=\"`uname -a`\"" server.cpp -o server
CMD ["./server"]
I have used opensource available sample code for client and server :
client.cpp
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define PORT 8080
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
struct hostent* server;
// Get server address using hostname
server = gethostbyname("container2");
if (!server) {
std::cerr << "No such host found" << std::endl;
return 1;
}
// Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
std::cerr << "Socket creation error" << std::endl;
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
memcpy(&serv_addr.sin_addr, server->h_addr_list[0], server->h_length);
// Connect to server
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
std::cerr << "Connection failed" << std::endl;
return 1;
}
// Send message
send(sock, "Hi, I'm container1", 18, 0);
char buffer[1024] = {0};
read(sock, buffer, 1024);
std::cout << "Received from server: " << buffer << std::endl;
close(sock);
return 0;
}
server.cpp :
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8080
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[1024] = {0};
// Create socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
std::cerr << "Socket failed" << std::endl;
return 1;
}
// Bind to port 8080
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
std::cerr << "Bind failed" << std::endl;
return 1;
}
// Listen for incoming connections
if (listen(server_fd, 3) < 0) {
std::cerr << "Listen failed" << std::endl;
return 1;
}
std::cout << "Server listening on port 8080..." << std::endl;
// Accept connection
new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
if (new_socket < 0) {
std::cerr << "Accept failed" << std::endl;
return 1;
}
read(new_socket, buffer, 1024);
std::cout << "Received: " << buffer << std::endl;
// Send response
send(new_socket, "Hello", 5, 0);
std::cout << "Response sent" << std::endl;
close(new_socket);
close(server_fd);
return 0;
}
Using below command to build the containers :
sudo docker buildx build --platform linux/arm64 -t server:arm64 --load -f server/server.Dockerfile ./server
sudo docker buildx build --platform linux/arm64 -t client:arm64 --load -f client/client.Dockerfile ./client
After shifting these images to board, I am loading and using below commands to run them on board ":
docker run -d --name container1 --network mybridge client:arm64
docker run -d --name container2 --network mybridge server:arm64
also I used below commands :
docker run -d --name container2 --network mybridge -p 8080:80 server:arm64
docker run -d --name container1 --network mybridge client:arm64
but none of them is helping me to achive communication.
On runnig client both server and client will be closed/stopped.
As we test on host it is correct once comm is done they stop. Issue here is we are not able to see logs that we are passing i.e printf stats from .cpp files.
for example on linux host we see below message on server part :
and below on client :
we are missing this part here on board.
Can anybody help me on this as I am trying this ofr the first time.Any suggestion would help me a lot.
regards,
Siddhartha V