title: Client socket won't open using socket() function in a chat client
body: <p>So this is a work in progress right now, but when I run the commands:
  ./server 4444
  and from a different terminal window, ./client localhost 4444</p>
  
  <p>The client stops at the "Error opening socket."
  I have no idea why, and any advice will be greatly appreciated. I am fully aware this code is buggy and incomplete, but I cannot move forward without first getting my client and server to connect properly.</p>
  
  <p>Here is my code:</p>
  
  <p>client.c</p>
  
  <pre><code>#include "../lib/sockettalk.h"
  #include "../lib/my.h"
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  //#define MAX_SIZE=255
  
  int main(int argc, char **argv)
  {
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);//only need one socket since you are the only one connecting
  int n;
  char buffer[MAX_SIZE];
  struct sockaddr_in serv_addr;
  struct hostent *server;
  int portnum = atoi(argv[2]);
  if(argc &lt; 3)
  {
      printf("Check your arguments.");
      exit(0);
  }
  
  //printf("%d", portnum);
  if(portnum &lt; 2000 || portnum &gt; 65535)//must be greater than 5000 to avoid conflictin ports
  {
      printf("Error, port number is out of bounds. Must be &gt;2000 AND &lt;65535.");
      printf("Please enter a valid port number next time.");
      exit(0);
  }
  
  if(sockfd &lt; 0);
      printf("Error opening socket. \n");
  else
      printf("Opened socket directly. \n");
  
  if((server = gethostbyname(argv[1])) == 0)
  {
      printf("Error, no host. \n");
      exit(0);
  }
  
  //memset(&amp;serv_addr, '0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htonl(portnum); 
  
  if((connect(sockfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr))) &lt; 0)
  {
      printf("Error connecting. \n");
  
  }
  
  while(1)
  {
      //clear and write a message
      memset(buffer, '0', sizeof(buffer));
      if((n = write(sockfd, buffer, my_strlen(buffer))) &lt; 0)
      {
          printf("Error writing to server. \n");
          exit(1);
      }
      printf("Client: ");
      printf("%s", buffer);
      printf(" \n");
  
      //clear and read
      memset(buffer, '0', sizeof(buffer));
      if((n = read(sockfd, buffer, MAX_SIZE)) &lt; 0)
      {
          printf("Error reading from server. \n");
          exit(1);
      }
      printf("Server: ");
      printf("%s", buffer);
      printf(" \n");
  }
  return 0;
  }
  </code></pre>
  
  <p>server.c</p>
  
  <pre><code>#include "../lib/my.h"
  #include "../lib/sockettalk.h"
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  
  
  int main(int argc, char **argv)
  {
  int port = atoi(argv[1]);
  int pid;
  int n; //to read and write 
  char buffer[MAX_BUFFER_SIZE];
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  int newsockfd;
  int clilen; //to be used in accept step as a dummy, need size of bytes
  struct sockaddr_in cli_addr, serv_addr;
  memset(&amp;serv_addr, 0, sizeof(serv_addr));
  
  if (sockfd&lt;0)
      printf("Problem openning socket.\n");
  else
  {
      printf("Openned socket successfully.\n");
  }
  
  serv_addr.sin_family = AF_INET;  //address family
  serv_addr.sin_port = port; //IP port
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //IP address, INADDR_ANY allows program to work without knowing the IP address of machine it is running on.
  
  if(bind(sockfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)) &lt; 0)//assigns a name to a socket. we must also cast serv_addr to a pointer to a struct socketDDR
  {   
      printf("Error binding socket. \n");
      exit(1);
  }
  else
  {
      printf("Bound socket successfully. \n");
  }
  
  if(listen(sockfd, 5) &lt; 0) //should be an already bound socket. 5 is the number of clients that can connect at once
  {
      printf("Error listning.\n");
      exit(1);
  }
  
  while(1) //place into loop so that process can repeat for every new connection
  {
  //now a newsockfd needs to be created so that it gives every new client a unique identifir
      newsockfd = accept(sockfd,(struct sockaddr*) &amp;cli_addr, &amp;clilen);
      if(newsockfd &lt; 0)
      {
          printf("Error accepting\n");
          exit(1);
      }
      else
      {
          printf("Client is connected.\n");
      }
  
      if ((pid = fork()) &lt; 0) //problem
      {
          printf("Error forking. \n");
          exit(1);
      }
      else if(pid == 0) //forking is sucessful, is a child
      {
          close(sockfd); //closes from the child side
          while(1)
          {
              memset(buffer, 0, sizeof(buffer));
              n = read(newsockfd, buffer, MAX_BUFFER_SIZE);
              if (n &lt; 0)
              {
                  printf("Error reading from client.\n");
                  exit(1);
              }
              printf("%s", buffer);
              printf("\n");
  
              memset(buffer, 0, sizeof(buffer));
              fgets(buffer, 254, stdin);
              n = write(newsockfd, buffer, strlen(buffer));
              if(n &lt; 0)
              {
                  printf("Error writing to client.\n");
                  exit(1);
              }
              printf("Server: ");
              printf("%s", buffer);
              printf("\n");
          }
          close(sockfd);
      }       
  }
  return 0;
  }
  </code></pre>