I'm a newbie with Docker. I write a server/client (very simple) application which both of them run in the same container.
I'm working on OSX and it work perfectly when I connect the client to the server.
However, I can't connect my client to my server. Here's my code:
client.c:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
    int sockfd = 0, n = 0;
    char recvBuff[1024];
    struct sockaddr_in serv_addr;
    if(argc != 2) {
      printf("\n Usage: %s <ip of server> \n",argv[0]);
      return 1;
    }
    memset(recvBuff, '0',sizeof(recvBuff));
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
      printf("\n Error : Could not create socket \n");
      return 1;
    }
    memset(&serv_addr, '0', sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5000);
    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0) {
      printf("\n inet_pton error occured\n");
      return 1;
    }
   int connect_value = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
   if (connect_value < 0) {
     printf("Value of connect_value %d\n", connect_value);
     fprintf(stderr, "Value of errno: %d\n", errno);
     return (1);
   }
    while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0){
      recvBuff[n] = 0;
      if(fputs(recvBuff, stdout) == EOF)
        printf("\n Error : Fputs error\n");
    }
    if(n < 0)
      printf("\n Read error \n");
    return 0;
}
server.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr;
    char sendBuff[1025];
    time_t ticks;
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(5000);
    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
    listen(listenfd, 10);
    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
        ticks = time(NULL);
        snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
        write(connfd, sendBuff, strlen(sendBuff));
        close(connfd);
        sleep(1);
     }
}
the command line I use to launch my container:
docker run -it --security-opt seccomp=unconfined --mount type=bind,src=/Users/steven/Documents/eip/firmware_coit,dst=/bidon 987steven/eip_linux bash
I compile and run with the following way:
gcc server.c -o server && ./server
gcc client.c -o client && ./client 127.0.0.1
Error : Connect Failed Value of
connect_value -1
Value of errno: 111
I tried to figured out by myself before post on SO but most of the topic and read about:
- how to connect unix-socket to docker-demon
- docker.sock
- docker container networking (offical documentation)
But didn't find anything that could solve my problem.
Just to be clear that I have a very few knowledge in network in general and newbie in docker. Please feel free to ask if you node additional information from me. Thanks
