title: Linux TCP connection refused
body: <p>I am doing a TCP connection program and I want to set the address and connection step as a function. The program can be compiled but when I running it the screen displays: ERROR CONNECTION: Connection refused. Could you please help me? Here is the server code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt; 
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  
  void error(char *msg)
  {
      perror(msg);
      exit (1);
  }
  struct sockaddr_in createSockAddr(int portnum)
  {
      struct sockaddr_in addr;
      addr.sin_family = AF_INET;
      addr.sin_addr.s_addr = INADDR_ANY;
      addr.sin_port = htons(portnum);
      return addr;
  }
  
  int createServerSocket(struct sockaddr_in serv_addr)
  {   
      int newsockfd;
      int sockfd;
      int clilen;
      sockfd =  socket(AF_INET, SOCK_STREAM, 0);//create socket
  
      if (sockfd &lt; 0) 
          error("ERROR opening socket");
  
  
      //bind socket
      if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0) 
          error("ERROR on binding");  
      listen(sockfd,5);//listen
      clilen = sizeof(serv_addr);
      newsockfd = accept(sockfd, (struct sockaddr *) &amp;serv_addr, &amp;clilen);//accept
      if (newsockfd &lt; 0) 
          error("ERROR on accept");
  
      return newsockfd;
  }
  int main(int argc, char *argv[])
  {
      int sockfd, newsockfd, portnum, clilen;
      char buffer[256];
      struct sockaddr_in serv_addr, cli_addr;
      int n;
      if (argc &lt; 2) {
          fprintf(stderr,"ERROR, no port provided\n");
      exit (2);
      }
      portnum = atoi(argv[1]);
      serv_addr = createSockAddr(portnum);
      newsockfd = createServerSocket(serv_addr);
  
      bzero(buffer,256);
      n = read(newsockfd,buffer,255);
      if (n &lt; 0) error("ERROR reading from socket");
      printf("Here is the message: %s\n",buffer);
      n = write(newsockfd,"I got your message",18);
      if (n &lt; 0) error("ERROR writing to socket");
      return 0; 
  }
  </code></pre>
  
  <p>Here is the client code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt; 
  
  
  void error(char *msg)
  {
      perror(msg);
      exit(1);
  }
  struct sockaddr_in createSockAddr(int portnum)
  {
  
      struct sockaddr_in addr;
      addr.sin_family = AF_INET;
      addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      addr.sin_port = htons(portnum);
      return addr;
  }
  
  int createClientSocket(struct sockaddr_in sockAddr)
  {
  
      int sockfd;
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd &lt; 0) 
          error("ERROR opening socket");//create socket
  
      if (connect(sockfd,(struct sockaddr *)&amp;sockAddr,sizeof(sockAddr)) &lt; 0) 
          error("ERROR connecting");
      return sockfd;
  }
  
  
  int main(int argc, char *argv[])
  {
      int sockfd, portnum, n;
  
      struct sockaddr_in serv_addr;
      struct hostent *server;
  
      char buffer[256];
      if (argc &lt; 3) {
          fprintf(stderr,"usage %s hostname port\n", argv[0]);
          exit(2);
      }
      portnum = atoi(argv[2]);
      serv_addr = createSockAddr(portnum);
      sockfd = createClientSocket(serv_addr);
      printf("Please enter the message: ");
      bzero(buffer,256);
      fgets(buffer,255,stdin);
      n = write(sockfd,buffer,strlen(buffer));
      if (n &lt; 0) 
          error("ERROR writing to socket");
      bzero(buffer,256);
      n = read(sockfd,buffer,255);
  
      if (n &lt; 0) 
          error("ERROR reading from socket");
      printf("%s\n",buffer);
      return 0;
  }
  </code></pre>