title: Socket error: connection refused - what am I doing wrong?
body: <p>I've just started learning the basics of sockets (Linux). I tried my hand at a small example, but it doesn't work and I have no idea what's wrong.</p>
  
  <p>I get a "Connection Refused" error message. </p>
  
  <hr>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netinet/ip.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  
  int main() {
      int c;
      c = socket(AF_INET, SOCK_STREAM, 0);
      if (c &lt; 0) {
          printf("Error in creating socket! %s\n", strerror(errno));
          return 1;
      }
  
      struct sockaddr_in server;
      memset(&amp;server, 0, sizeof(server));
      server.sin_port = htons(1234);
      server.sin_family = AF_INET;
      server.sin_addr.s_addr = inet_addr("127.0.0.1"); //local host
      if (connect(c, (struct sockaddr *)&amp;server, sizeof(server)) &lt; 0) {
          // Here is my error
          printf("Error when connecting! %s\n",strerror(errno)); 
          return 1;
      }
  
      while(1) {
          char msg[100];
          printf("Give message: ");
          fgets(msg, sizeof(msg), stdin); 
          send(c, &amp;msg, sizeof(msg), 0);
          char resp[100];
          recv(c, &amp;resp, sizeof(resp), 0);
          printf("Received: %s\n", resp);
      }
  close(c);
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Of course ! the error was actually in the server. I simply found it weired that the client sent the message, so I narrowed my view, didn't even bother looking back at the server.</p>
  
  <p><strong>Since the error seems to be also in my server, I might end up asking another question and linking it here</strong></p>
  
  <p>Server was listening to (12345) ...</p>