title: How to connect two computers over internet using socket programming in C?
body: <p>This is simple Client-Server chat program.This code is working fine on computers connected on Network, how to modify it such that it can connect between computers over the Internet. Using Public IP of the server in <code>gethostbyname()</code> isn't working.</p>
  
  <pre><code>//Client.c
  
  #include&lt;stdio.h&gt;
  #include&lt;sys/types.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;netinet/in.h&gt;
  #include&lt;netdb.h&gt;  
  #include&lt;unistd.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  
  int main(void)
  {
      int clientSocket; /* Socket Decriptor for Client */
      struct sockaddr_in server_addr;
      struct hostent *ptrh;
  
      char message[100];
      char received[100];
      int n = 0;
  
      clientSocket=socket(AF_INET, SOCK_STREAM, 0);
  
      memset((char*)&amp;server_addr, 0, sizeof(server_addr));
  
      server_addr.sin_family = AF_INET;
      server_addr.sin_port = htons(10000);
  
      /*  bind(clientSocket, (struct sockaddr*)&amp;server_addr, sizeof(struct sockaddr)); */
  
      ptrh=gethostbyname("110.172.156.2");
      memcpy(&amp;server_addr.sin_addr,ptrh-&gt;h_addr,ptrh-&gt;h_length);
  
      if( -1 == (connect(clientSocket, (struct sockaddr*)&amp;server_addr, sizeof(server_addr)))) 
      { printf("\nServer Not Ready !!\n"); exit(1); }
  
  while(1)
  {
      printf("\nUser:-");
     // memset(message, '\0', 10);
  
      gets(message);
  
      n = write(clientSocket, message, strlen(message)+1);
  if( (strcmp(message,"q") == 0 ) || (strcmp(message,"Q") == 0 ))
      {
         printf("Wrong place...Socket Closed\n");
         close(clientSocket);
         break;
      }
  
      //printf("Write:&lt;%u&gt;\n", n);
  
      read(clientSocket, received, sizeof(received));
      if( (strcmp(received,"q") == 0 ) || (strcmp(received,"Q") == 0 ))
      {
         printf("Wrong place...Socket Closed\n");
         close(clientSocket);
         break;
      }
      else
      printf("Server:- %s\n", received);
  
  
  
  }
  
  return 0;
  }
  
  //Server.c
  
  #include&lt;sys/types.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;netinet/in.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;string.h&gt;
  #include&lt;netdb.h&gt;
  
  int main(void) 
  {
  int serverSocket,client_connected,len;
  struct sockaddr_in client_addr,server_addr;
  struct hostent *ptrh;
  int n=0; 
  char message[100],received[100];
  
  serverSocket=socket(AF_INET, SOCK_STREAM, 0);
  
  memset((char*)&amp;server_addr,0,sizeof(server_addr));
  
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(10000);
  
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  
  if(bind(serverSocket,
  (struct sockaddr*)&amp;server_addr,sizeof(server_addr)) == -1)
  printf("Bind Failure\n");
  else
  printf("Bind Success:&lt;%u&gt;\n", serverSocket);
  
  
  
  
  while(1)
  {   
       listen(serverSocket,5);
       len=sizeof(struct sockaddr_in);
  
      client_connected=accept(serverSocket,
      (struct sockaddr*)&amp;client_addr,&amp;len);
  if (-1 != client_connected)
    printf("Connection accepted:&lt;%u&gt;\n", client_connected);
  
      while(1)
      {
      n = read(client_connected, received, sizeof(received));
      if( (strcmp(received,"q") == 0 ) || (strcmp(received,"Q") == 0 ))
      {
         printf("Wrong place...Socket Closed of Client\n");
         close(client_connected);
         break;
      }
      else{
      printf("\nUser:-%s", received);}
      printf("\nServer:-");
    //  memset(message, '\0', 10);
      gets(message);             
      write(client_connected, message, sizeof(message));
      if( (strcmp(message,"q") == 0 ) || (strcmp(message,"Q") == 0 ))
      {
         printf("Wrong place...Socket Closed of Client\n");
         close(client_connected);
         break;
      }  
      }
  }
  
  close(serverSocket); printf("\nServer Socket Closed !!\n");
  
  return 0;
  }
  </code></pre>