title: Send / receive data over network in C
body: <p>I have written this program:</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int socket_desc;
  struct sockaddr_in adress;
  int addrlen;
  int new_socket;
  int bufsize = 1024;
  
  char *you_sent = "You sent: ";
  
  int main() {
      char *buffer = malloc(bufsize);
      socket_desc = socket(AF_INET, SOCK_STREAM, 0);
      adress.sin_family = AF_INET;
      adress.sin_addr.s_addr = INADDR_ANY;
      adress.sin_port = htons(7000);
      bind(socket_desc, (struct sockaddr *)&amp;adress, sizeof(adress));
      listen(socket_desc, 3);
      addrlen = sizeof(struct sockaddr_in);
      new_socket = accept(socket_desc, (struct sockaddr *)&amp;adress, &amp;addrlen);
      while(recv(new_socket,buffer,bufsize,0))
      {
          printf("I recieved: %s", buffer);
          send(new_socket, you_sent, strlen(you_sent), 0);
          send(new_socket, buffer, strlen(buffer), 0);
          memset(buffer, '\0', sizeof(buffer));
      }
  }
  </code></pre>
  
  <p>I can connect to the server with a telnet. And write stuff to the application and recieve data from the application.
  But i cannot get my head around how i can connect to this with another c program and send and recieve data from that program.
  I have tried this:</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int socket_desc;
  struct sockaddr_in adress;
  int addrlen;
  int new_socket;
  
  char *message_to_send = "Hello world!";
  
  int main() {
      socket_desc = socket(AF_INET, SOCK_STREAM, 0);
      adress.sin_family = AF_INET;
      adress.sin_addr.s_addr = INADDR_ANY;
      adress.sin_port = htons(7000);
      bind(socket_desc, (struct sockaddr *)&amp;adress, sizeof(adress));
      listen(socket_desc, 3);
      addrlen = sizeof(struct sockaddr_in);
      new_socket = accept(socket_desc, (struct sockaddr *)&amp;adress, &amp;addrlen);
      send(new_socket, message_to_send, strlen(message_to_send), 0);
  }
  </code></pre>