title: Socket being set up without errors, but no connection
body: <p>I am trying to learn how to use TCP/IP via 2 Linux machines, both of which have functioning internet connections. I tried setting up a server and a client, but I'm not getting a connection, despite not getting any errors during setup.</p>
  
  <p>Server code:</p>
  
  <pre><code>int serverSocketFd,clientSocketFd,clientLength,numberOfBytes;
  static struct sockaddr_in server_address, clint address;
  char buf[256];
  
  serverSocketFd = socket(AF_INET,SOCK_STREAM,0);
  if(serverSocketFd&lt;0){error("socket() call Failed");}
  
  //Clears the allocated space of the sockaddr_in struct before configuration
  bzero((char *)&amp;server_address, sizeof(server_address));
  
  server_address.sin_family = AF_INET;
  //inet_aton should take an IP address in string format and convert it to the proper format for the struct. Returns nonzero for error
  if(!inet_aton("XXX.XXX.XXX.XXX",&amp;(server_address.sin_addr))){error("inet_aton() Failed");}
  
  //Picked a random port number for this test
  server_address.sin_port = htons(34567);
  
  //Quick check that I have the expected settings
  inet_ntop(AF_INET,&amp;server_address.sin_addr,buf,255);
  printf("Listening on %s:%d\n",buf,server_address.sin_port);
  
  if(bind(serverSocketFd,(struct sockaddr *)&amp;server_address,sizeof(server_address)) &lt;0){error("bind() call Failed");}
  
  //At this point, Socket should be prepped to start listening, and waits here until another program makes contact
  printf("Waiting on client...\n");
  listen(serverSocketFd,5);
  
  //If a client program makes a connection, the 'clinetSocketFd' represents it
  clientLength = sizeof(client_address);
  clientSocketFd = accept(serverSocektFd, (struct sockaddr *)&amp;clinet_address, (socklen_t *)&amp;clientLength);
  if(clientSocketFd &lt; 0){error("accept() call returned invalid");}
  
  printf("CONNECTION ACCEPTED\n");
  
  //Now that a connection is established, wait for a request from client
  bzero(buf,256);
  numberOfBytes = read(clientSocketFd,buf,255);
  if(numberOfBytes&lt;0){error("read() call Failed");}
  
  printf("MESSAGE RECEIVED: %s\n",buf);
  
  numberOfBytes = write(clientSocketFd,"ACKNOWLEDGED",strlen("ACKNOWLEDGED"));
  return numberOfBytes;
  </code></pre>
  
  <p>I expect the output to be a message printing my IP address and port, then a wait message, then a message announcing a connection, then a message announcing a request from the client, and the incoming text.</p>
  
  <p>Client code:</p>
  
  <pre><code>static struct sockaddr_in server_address;
  char buf[256];
  int numberOfBytes,e;
  
  clientSocketFd = socket(AF_INET,SOCK_STREAM,0);
  if(clientSocketFd&lt;0){error("socket() call Failed");}
  
  //Clears allocated space of the sockaddr_in struct before configuration
  bzero((char *)&amp;server_address, sizeof(server_address));
  server_address.sin_family = AF_INET;
  //The IP address being put in here is the same as the one put in the server code, the IP address of the host machine.
  if(!inet_aton("XXX.XXX.XXX.XXX",&amp;(server_address.sin_addr))){error("inet_aton() Failed");}
  
  server_address.sin_port = htons(34567);
  
  //Same check as server code, look at the target IP and port
  inet_ntop(AF_INET, &amp;server_address.sin_addr,buf,255);
  printf("Connecting to %s:%d\n",buf,server_address.sin_port);
  
  //As client, we connect the socketFd for the CLIENT to the address struct of the SERVER
  e = connect(clientSocketFd,(struct sockaddr *)&amp;server_address,sizeof(server_address));
  if(e&lt;0){error("connect() call Failed");printf("Error code %d\n",e);}
  
  //Connection has now been established and can be referenced through clientSocketFd
  printf("CONNECTION ESTABLISHED\n");
  
  //Now send a generic message 
  numberOfBytes = write(clientSocketFd,"TESTMESSAGE",strlen(message));
  if(numberOfBytes&lt;0){error("write() call Failed");}
  
  bzero(buf,256);
  numberOfBytes = read(clientSocketFd,buf,255);
  if(numberOfBytes&lt;0){error("read() call Failed");}
  
  printf("%s\n",buf);
  
  return 0;
  </code></pre>
  
  <p>I expect the output to be roughly the same, print the target IP and port, acknowledgement messages, and then the received acknowledge.</p>
  
  <p>What I end up seeing, on both machines, is up to the listen/connect stage.</p>
  
  <p>I see the "Listening on/Connecting to XXX.XXX.XXX.XXX:34567", which matches up with the IP address on the machine running the server code. Then it times out. errno is set to ECONNREFUSED.</p>
  
  <p>The connection is never made, but no error comes up before this point, so it doesn't seem like anything went wrong while configuring the sockets.</p>
  
  <p>Can someone explain what is problem is with this code?</p>