title: Beginner's Socket Programming in C
body: <p>I have just started learning socket programming and am finding it pretty interesting.
  Currently I am making the <strong>server and the client on the same computer</strong> and hence I can have the <em>ip address</em> as the loopback address, <strong>127.0.0.1</strong> and everything seems to work fine!!</p>
  <p>But now I was thinking of having two computers and doing the thing..
  I have the following questions</p>
  <ol>
  <li>Say one computer is Server and another is Client. Now, should the <strong>server code</strong> reside on the <strong>server computer</strong> and the <strong>Client code</strong> on the <strong>client one</strong>?</li>
  <li>In the <strong>server code</strong> when we are providing the <em>ip address</em> for <code>bind()</code>, it should be the <em>ip address</em> of the system that we can find through <code>ipconfig</code> or it should still remain the loopback address?</li>
  <li>In the client code, I guess the ip address of the destination should be that of the server computer right??</li>
  <li>And the last and the most important thing, HOW DO I CONNECT THE TWO COMPUTERS??</li>
  </ol>
  <p>I am attaching the simple server and client message passing code that I started out with. Kindly guide me through the changes that I need to make..</p>
  <p>SERVER CODE</p>
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define MYPORT 3500
  
  int main()
  {
      int sockfd;
      int clientfd;
      int bytes_read;
      char buf[100];
      int struct_size;
      struct sockaddr_in my_addr;
      struct sockaddr_in con_addr;
      
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      
      my_addr.sin_family = AF_INET;
      my_addr.sin_port = htons(MYPORT);
      my_addr.sin_addr.s_addr = inet_addr(&quot;127.0.0.1&quot;);
      my_addr.sin_zero[8]='\0';
  
      bind(sockfd, (struct sockaddr*)&amp;my_addr, sizeof(struct sockaddr));
  
      listen(sockfd,5);
      
      struct_size = sizeof(con_addr);
      clientfd = accept(sockfd, (struct sockaddr*)&amp;con_addr, &amp;struct_size);
  
      bytes_read = read(clientfd, buf, 100);
      buf[bytes_read] = '\0';
      printf(&quot;Message from client:%d is %s \n&quot;,clientfd, buf);
  
      close(sockfd);
      close(clientfd);
  }
  </code></pre>
  <p>CLIENT CODE</p>
  <pre><code>#include&lt;sys/types.h&gt;
  #include&lt;sys/socket.h&gt;
  #include&lt;netinet/in.h&gt;
  #include&lt;fcntl.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdio.h&gt;
  
  #define DESTPORT 3500
  
  int main()
  {
  
      struct sockaddr_in dest_addr;
      
      int sockfd = socket(AF_INET,SOCK_STREAM,0);
  
      dest_addr.sin_family = AF_INET;
      dest_addr.sin_port = htons(DESTPORT);
      dest_addr.sin_addr.s_addr = inet_addr(&quot;127.0.0.1&quot;);
      dest_addr.sin_zero[8]='\0';
  
      connect(sockfd,(struct sockaddr*)&amp;dest_addr, sizeof(struct sockaddr));
  
      char msg[100];
      printf(&quot;Enter you message: &quot;);
      gets(&amp;msg); 
      
      int w = write(sockfd, msg, strlen(msg));
      
      close(sockfd);
      printf(&quot;Client Dying.....\n&quot;); 
  
      return 0;
  }
  </code></pre>