Raspberry PI multiple ethernet server listener
<p>I have a simple TCP server listener program running on the third generation raspberry pi. The PI has two ethernet ports:</p>

<p>eth0:-</p>

<p>ip: 10.0.0.100</p>

<p>Bcast: 10.255.255.255</p>

<p>Subnet mask: 255.0.0.0</p>

<p>eth1:-</p>

<p>ip: 10.0.0.110</p>

<p>Bcast: 10.255.255.255</p>

<p>Subnet mask: 255.0.0.0</p>

<p>My program must listen on both IP addresses on specific ports (code below). However, when I attempt to connect to to either of the ip addresses i can't. But if i remove an ethernet cable from the PI and only have the one, then I can connect to that specific IP. Not sure why this is happening.</p>

<pre><code>void *Listener1();
void *Listener2();

int main()
{
    pthread_t thread1, thread2;
    pthread_create(&amp;thread1, NULL,&amp;Listener1, NULL);
    pthread_create(&amp;thread2, NULL,&amp;Listener2, NULL);
    sleep(50);
    return 0;
}
void *Listener1()
{
     int sockfd, newsockfd;
     socklen_t clilen;
     struct sockaddr_in serv_addr, cli_addr;

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd &lt; 0) 
        puts("ERROR opening socket");
     bzero((char *) &amp;serv_addr, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = inet_addr("10.0.0.100");
     serv_addr.sin_port = htons(12346);
     if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,
              sizeof(serv_addr)) &lt; 0) 
              puts("ERROR on binding");
     listen(sockfd,1);
     clilen = sizeof(cli_addr);

     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &amp;cli_addr, 
                 &amp;clilen);
     if (newsockfd &lt; 0) 
          puts("ERROR on accept");
     else
        puts("Client connected!");

     close(newsockfd);
     close(sockfd);
     return 0; 
}
void *Listener2()
{
    puts("hi)");
     int sockfd, newsockfd;
     socklen_t clilen;
     struct sockaddr_in serv_addr, cli_addr;

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd &lt; 0) 
        puts("ERROR opening socket");
     bzero((char *) &amp;serv_addr, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = inet_addr("10.0.0.110");
     serv_addr.sin_port = htons(12345);
     if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,
              sizeof(serv_addr)) &lt; 0) 
              puts("ERROR on binding");
     listen(sockfd,1);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &amp;cli_addr, 
                 &amp;clilen);
     if (newsockfd &lt; 0) 
          puts("ERROR on accept");
     else
        puts("Client connected!");

     close(newsockfd);
     close(sockfd);
     return 0; 
}
</code></pre>