title: Socket in C to remote host
body: <p>i am making some experiments with sockets in C but i have a problem that i think there is a solution but i can't figure it out!</p>
  
  <p>i have a server and a client written in C and they works so good in local network, i tried <em>'localhost'</em> and <em>127.0.0.1</em> or just <em>192.168.1.2</em> and everything works like a charm...
  <strong>but</strong> when i try to connect to a remote host (for my experiments i used my own computer with IP found on <a href="http://www.whatismyip.com/" rel="nofollow">http://www.whatismyip.com/</a> 62.98.XXX.XX) connection and data (string) exchange never happens... </p>
  
  <p>how can i solve this issue?</p>
  
  <p>here is my server.c code</p>
  
  <pre><code>    //server
  
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;time.h&gt; 
  
  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(&amp;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*)&amp;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(&amp;ticks));
          write(connfd, sendBuff, strlen(sendBuff)); 
  
          close(connfd);
          sleep(1);
       }
  }
  </code></pre>
  
  <p>and this is my client.c code</p>
  
  <pre><code>//client
  
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;arpa/inet.h&gt; 
  
  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 &lt;ip of server&gt; \n",argv[0]);
          return 1;
      } 
  
      memset(recvBuff, '0',sizeof(recvBuff));
      if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      {
          printf("\n Error : Could not create socket \n");
          return 1;
      } 
  
      memset(&amp;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], &amp;serv_addr.sin_addr)&lt;=0)
      {
          printf("\n inet_pton error occured\n");
          return 1;
      } 
  
      if( connect(sockfd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0)
      {
         printf("\n Error : Connect Failed \n");
         return 1;
      } 
  
      while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) &gt; 0)
      {
          recvBuff[n] = 0;
          if(fputs(recvBuff, stdout) == EOF)
          {
              printf("\n Error : Fputs error\n");
          }
      } 
  
      if(n &lt; 0)
      {
          printf("\n Read error \n");
      } 
  
      return 0;
  }
  </code></pre>