title: Unix server client Connect: Connection refused
body: <p>I am having some problems creating my first server-client application with sockets. I have hardcoded the server's socket address to localhost and the port as 55555. When I try to connect to it from client in a separate terminal typing the command ./player localhost 55555, nothing happens to the server. What I believe should happen is that the server receives data which is "Rock" then prints "Client: ready.\nGo\n    Client: Rock". Instead, the server keeps on waiting with no reaction to player. I suspected that I haven't actually connected to the server so I tried to hardcode the socket address "localhost" into the player connect, as well as the port to 55555 (also hardcoded). I tried running the command ./player and it spews "Connect: Connection Refused". I was wondering how I could fix my hard coded player.c</p>
  
  <p><strong>referee.c</strong> (this is the server)</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;strings.h&gt;
  
  int main(int argc, const char* argv[])
  {
      char buf[1024];
      struct sockaddr_in localhost;
      int listenSock, serviceSock, bytesRead;
      size_t len = sizeof(localhost);
      int port = 55555;
      listenSock = socket(AF_INET, SOCK_STREAM, 0);
      if (listenSock&lt;0) {
          perror("Socket");
          exit(1);
      }
      bzero((char*)&amp;localhost, len);
      localhost.sin_family = AF_INET;
      localhost.sin_addr.s_addr = htonl(INADDR_ANY);
      localhost.sin_port = htons(port);
      bind(listenSock, (struct sockaddr*) &amp;localhost, len);
      printf("Paper, Scissors, Rock game start.\nWaiting for player...\n");
      listen(listenSock, 5);
      do {
          serviceSock = accept(listenSock, 0, 0);
          if(serviceSock == -1){
              perror("Accept");
          }
          else do {
              bzero(buf, sizeof(buf));
              if((bytesRead = read(serviceSock, buf, 1024))&lt;0) {
                  perror("Read");
              }
              if(bytesRead &gt; 0){
                  printf("Client: ready.\nGo\n    Client: %s\n", buf);
              }
          } while (bytesRead != 0);
          close(serviceSock);
      } while(1);
  }
  </code></pre>
  
  <p><strong>player.c</strong> (client)</p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char* argv[]) {
      int sock;
      struct sockaddr_in server;
      struct hostent *hp;
      char buf[1024], *data = "Rock";
      sock = socket(AF_INET, SOCK_STREAM, 0);
      if (sock &lt; 0) {
          perror("Socket");
          exit(1);
      }
      server.sin_family = AF_INET;
      hp = gethostbyname(argv[1]);
      if (hp == 0) {
          fprintf( stderr, "%s: unknown host", argv[1] );
          exit(1);
      }
      bcopy( hp-&gt;h_addr, &amp;server.sin_addr, hp-&gt;h_length );
  
      server.sin_port = htons(atoi(argv[2]));
      if(connect(sock, (struct sockaddr *) &amp;server, sizeof(server)) &lt; 0) {
          perror("Connect");
          exit(1);
      }
      if(write(sock, data, strlen(data)) &lt; 0) {
          perror("Write");
      }
      close(sock);
  }
  </code></pre>
  
  <p><strong>player.c with hardcoded address/port</strong></p>
  
  <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;strings.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char* argv[]) {
      int sock;
      struct sockaddr_in server;
      struct hostent *hp;
      char buf[1024], *data = "Rock";
      sock = socket(AF_INET, SOCK_STREAM, 0);
      if (sock &lt; 0) {
          perror("Socket");
          exit(1);
      }
      server.sin_family = AF_INET;
      hp = gethostbyname("localhost");
      if (hp == 0) {
          fprintf( stderr, "%s: unknown host", argv[1] );
          exit(1);
      }
      bcopy( hp-&gt;h_addr, &amp;server.sin_addr, hp-&gt;h_length );
  
      server.sin_port = 55555;
      if(connect(sock, (struct sockaddr *) &amp;server, sizeof(server)) &lt; 0) {
          perror("Connect");
          exit(1);
      }
      if(write(sock, data, strlen(data)) &lt; 0) {
          perror("Write");
      }
      close(sock);
  }
  </code></pre>