title: Socket Programming Error in Connect
body: <p>I am trying to do Socket programming. I've google and looked at problems like mine. However, I can't see to figure out my error.</p>
  
  <p>Whenever I run my code, I get an error in <code>connect()</code> in <code>main</code> in <code>client.c</code>. The error is <code>invalid argument</code>. </p>
  
  <p><strong>Server.c</strong></p>
  
  <pre class="lang-c prettyprint-override"><code>/* chatroom server */
  
  #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;arpa/inet.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;signal.h&gt;
  
  
  #define MAX_ROOMS 36
  #define MAX_NAME_SIZE 56
  #define PORT_NUM 12333
  #define MSG_SIZE 8
  #define MAX_LOAD 246
  #define MAX_CRC 64 //max clients
  #define MAX_BUF 256
  #define SERVER_IP 32
  #define DEBUG 0
  
  struct msg {
      int type; //create, join, delete
      int length; // length or port num
  };
  
  struct chat_room {
      int socket_d; //socket descriptor
      int port;
      pthread_t id; //id of chatroom
      char name[MAX_NAME_SIZE];
      int clients[MAX_ROOMS];  // used to hold the client's master sockets
  };
  
  struct chat_room* findRoom(char* name, struct chat_room* chat){
  
      int iterator;
  
      for(iterator = 0; iterator &lt; MAX_ROOMS; iterator++){
          if(strcmp(name, chat[iterator].name) &amp;&amp; (chat[iterator].port != 0)) {
              return &amp;chat[iterator];
          }
      }
  
      //if room does not exist
      return NULL;    
  }
  
  struct chat_room* joinServer(int chat_socket, char* name, struct chat_room* chat){
  
      struct chat_room* local_chat = findRoom(name, chat);
      int i;
      if(local_chat != NULL){ //if the chat exists
          for(i= 0; i&lt; MAX_CRC; i++){
              if(local_chat-&gt;clients[i] == 0)
              {
                  local_chat-&gt;clients[i] = chat_socket;
              }
              return local_chat;
          }
      }
  
      //if server if full or else error
      return NULL; 
  }
  
  int createResponse(int chat_socket, int new_port_num, int type){
      struct msg temp;
      char temp_buff[MAX_LOAD];
  
      memset(temp_buff, 0, MAX_LOAD);
      temp.type = type; 
      temp.length = new_port_num;
  
      memcpy(temp_buff, &amp;temp, sizeof(temp));
  
      write(chat_socket, temp_buff, MSG_SIZE);
  
      return 0;
  }
  
  int joinResponse(int chat_socket, int new_port_num){
      struct msg temp;
      char temp_buff[MAX_LOAD];
  
      memset(temp_buff, 0, MAX_LOAD);
      temp.type = 11; 
      temp.length = new_port_num;
  
      memcpy(temp_buff, &amp;temp, sizeof(temp));
  
      write(chat_socket, temp_buff, MSG_SIZE);
  
      return 0;
  }
  
  int deleteResponse(int chat_socket, struct chat_room* chat){
      struct msg temp;
      char temp_buff[MAX_LOAD];
      int i; 
      memset(temp_buff, 0, MAX_LOAD);
      temp.type = 12; 
  
      memcpy(temp_buff, &amp;temp, sizeof(temp));
  
      for(i=0; i&lt;MAX_CRC; i++){
          if((chat-&gt;clients[i] != chat_socket) &amp;&amp; (chat-&gt;clients[i] != 0))
              write(chat-&gt;clients[i],temp_buff, MSG_SIZE);
      }
  
      return 0;
  }
  
  struct chat_room* addRoom(int chat_socket, char* name, struct chat_room* chat){
  
      int socket_d;
      int i;
      struct sockaddr_in sock;
  
      static int port = PORT_NUM;
  
      memset(&amp;sock, 0, sizeof(sock));
  
      int temp = -1;
  
      for(i = 0; i&lt;MAX_ROOMS; i++){
          if((strcmp(chat[i].name, name) == 0) &amp;&amp; (chat[i].port != 0)){
              createResponse(chat_socket, chat[i].port, 15);
              return NULL;
          }
          else if((chat[i].port == 0) &amp;&amp; (temp== -1)){
              temp = i;
          }
      }
  
      if(temp == -1){
          return NULL;
      }
  
      socket_d = socket(AF_INET, SOCK_STREAM, 0);
  
      if(socket_d == -1 &amp;&amp; DEBUG){
          perror("Error creating chatroom socket");
          return NULL;
      }
  
      sock.sin_family = AF_INET;
      port++;
      sock.sin_port = htons(port);
  
      if(bind(socket_d, (struct sockaddr*)&amp;sock, sizeof(struct sockaddr_in)) == -1){
          perror("error in binding ");
          return NULL;
      }
  
      chat[temp].socket_d = socket_d;
      chat[temp].port = port;
      strcpy(chat[temp].name, name);
  
      return &amp;chat[temp];
  
  }
  
  void* chat_room_main(void* chat_room_cluster){
  
      char buf[MAX_LOAD];
  
      int socket_d, chat_socket;
      int temp; //temp_fd
      int read_val;
      int num_clients = 0;
      int i;
      int clients[MAX_CRC];
  
      fd_set allfd, modfd;
  
      struct chat_room chat_room_para;
  
      memcpy(&amp;chat_room_para, (struct chat_room*)chat_room_cluster, sizeof(struct chat_room));
      free(chat_room_cluster);
  
      memset(clients, 0, sizeof(int)*MAX_CRC);
      socket_d = chat_room_para.socket_d;
  
      listen(socket_d, 1);
  
      FD_ZERO(&amp;allfd);
      FD_SET(socket_d, &amp;allfd);
      printf("New Chatroom Started\n");
      while(1){
          modfd = allfd;
          select(FD_SETSIZE, &amp;modfd, NULL, NULL, NULL);
  
          for(temp = 0; temp &lt; FD_SETSIZE; temp++){
              if(FD_ISSET(temp, &amp;modfd)){
  
                  memset(buf, 0, sizeof(buf));
  
                  if(temp == socket_d) {
  
                      chat_socket = accept(socket_d, NULL, NULL);
                      FD_SET(chat_socket, &amp;allfd);
  
  
                      // find an empty spot to add the chat room
                      for(i = 0; i&lt;MAX_CRC; i++) {
                          if(clients[i] == 0){
                              clients[i] = chat_socket;
                              break;
                          }
                      }
  
                      sprintf(buf, "Number of people in chatroom: %d", num_clients);
                      write(chat_socket, buf, strlen(buf));
                      num_clients++;
                  }
                  else{
                      if(read_val = read(temp, buf, MAX_LOAD) &gt; 0){
                          for(i = 0; i&lt; MAX_CRC; i++){
                              if((clients[i] != temp) &amp;&amp; (clients[i] != 0)){
                                  write(clients[i], buf, read_val);
                              }
                          }
                      }
                      else if(read_val &lt;= 0) {
                          FD_CLR(temp, &amp;allfd);
  
                          for(i = 0; i&lt;MAX_CRC; i++){
                              if(clients[i] == temp)
                                  clients[i] = 0;
                          }
                          num_clients--;
                          close(chat_socket);
                      }
                  }
              }
          }
      }   
  
  }
  
  int main(int argc, char* argv[]){
  
      int server_socket, chat_socket; //file descriptors for server and chat
      int temp; //tempfd 
      int i, j;
  
      char server_ip[SERVER_IP];
      char buf[MAX_BUF];
      char msg_buf[MSG_SIZE];
  
      fd_set allfd, modfd;
  
      struct sockaddr_in sock;
      struct hostent* host_name;
  
      struct chat_room chatrooms[MAX_ROOMS];
  
      memset(chatrooms, '\0', sizeof(struct chat_room)* MAX_ROOMS);
      memset(&amp;sock, '\0', sizeof(sock));
  
      server_socket = socket(AF_INET, SOCK_STREAM, 0);
      if(server_socket == -1){
          perror("Error creating socket");
          return -1;
      }
  
      sock.sin_family = AF_INET;
      sock.sin_port = htons(PORT_NUM);
      sock.sin_addr.s_addr = INADDR_ANY;
  
      if((bind(server_socket, (struct sockaddr*)&amp;sock, sizeof(struct sockaddr_in))) == -1){
          perror("Error in bind()");
          return -1;
      }
  
      listen(server_socket, 1);
  
      FD_ZERO(&amp;allfd);    
      FD_SET(server_socket, &amp;allfd); 
      FD_SET(0, &amp;allfd);
      printf("\n*******Chatroom Server*******");
      while(1){
          modfd = allfd;
          select(FD_SETSIZE, &amp;modfd, NULL, NULL, NULL);
  
          for(temp = 0; temp &lt; FD_SETSIZE; temp++) {
              if(FD_ISSET(temp, &amp;modfd)) {
                  switch(temp){
                      case 0:
                          break;
                      default:
                          if(temp == server_socket){
                              chat_socket = accept(server_socket, NULL, NULL);
                          }
                          else{
                              char msg_buf[MSG_SIZE];
                              char buf[MAX_LOAD];
                              char name[MAX_LOAD];
  
                              struct msg temp_message;
                              struct chat_room* local_chat = NULL;
  
                              void* (*init_chatroom)() = &amp;chat_room_main;
                              void* thread_args;
  
                              pthread_attr_t attr;
                              pthread_t tid;
  
                              pthread_attr_init(&amp;attr);
                              pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_DETACHED);
  
                              memset(buf, 0, sizeof(buf));
                              memset(msg_buf, 0, sizeof(msg_buf));
                              msg_buf[19]= 0;
  
                              int read_val = read(temp, msg_buf, MSG_SIZE);
                              if(read_val &gt; 0){
                                  memcpy(&amp;temp_message, msg_buf, sizeof(temp_message));
                                  read(temp, buf, temp_message.length - sizeof(temp_message));
                                  memcpy(name, buf, temp_message.length - sizeof(temp_message));
  
                                  if(temp_message.type == 0) {//if create
                                      local_chat = addRoom(temp, name, chatrooms);
  
                                      if(local_chat != NULL){
                                          thread_args = (void*)malloc(sizeof(struct chat_room));
                                          memcpy((struct chat_room*) thread_args, local_chat, sizeof(struct chat_room));
                                          pthread_create(&amp;tid, &amp;attr, init_chatroom, thread_args);
                                          local_chat-&gt;id = tid;
                                          createResponse(temp, local_chat-&gt;port, 10);
                                      }
  
                                  }
                                  else if(temp_message.type == 1){ //join
                                      local_chat = joinServer(temp, name, chatrooms);
  
                                      if(local_chat != NULL){
                                          joinResponse(temp, local_chat-&gt;port);
                                      }
  
                                  }
                                  else if(temp_message.type == 2){ //delete
                                      local_chat = findRoom(name, chatrooms);
  
                                      printf("Deleting Room\n");
                                      if(local_chat != NULL) {
                                          local_chat-&gt;port = 0;
                                          close(local_chat-&gt;socket_d);
                                          deleteResponse(temp, local_chat);
  
                                          for(j = 0; j&lt;MAX_CRC; j++){
                                              if(local_chat-&gt;clients[j]!=0) {
                                                  FD_CLR(local_chat-&gt;clients[j], &amp;modfd);
                                                  local_chat-&gt;clients[j] = 0;
                                              }
                                          }
                                          pthread_cancel(local_chat-&gt;id);
                                      }
                                  }
  
                              }
                              else if(read_val &lt;= 0){
                                  FD_CLR(temp, &amp;allfd);
  
                                  close(temp);
                              }
                          }
                  }
              }
          }
      }
  
      return 0;
  
  }
  </code></pre>
  
  <p><strong>Client.c</strong></p>
  
  <pre class="lang-c prettyprint-override"><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;arpa/inet.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;pthread.h&gt;
  
  #define PORT_NUM 12333
  #define MSG_SIZE 8
  #define MAX_LOAD 246
  #define MAX_BUF 256
  #define SERVER_IP 32
  
  struct msg {
      int type;
      int length;
  };
  
  struct thread_para {
      int port;
      struct hostent* host_name;
  
  };
  
  int sendCmd(int chat_socket, char* buf, int type){ //function for sending the command
      struct msg temp;
      char temp_buf[MAX_LOAD];
      char name[MAX_LOAD];
  
      int iterator;
  
      if(type == 0){
          iterator = 6;
      }
      else if(type == 1){
          iterator = 4;
      }
      else if(type == 2){
          iterator = 6;
      }
  
      for(; iterator &lt; strlen(buf); iterator++){
          if(buf[iterator] == ' ') {
              continue;
          }   
          else{
              break;
          }
      }
  
      strcpy(name, buf+iterator);
      memset(temp_buf, 0, MAX_LOAD);
      temp.type = type;
      temp.length = sizeof(temp)+strlen(name)+1;  //for \0
  
  
      memcpy(temp_buf, &amp;temp, sizeof(temp));
      memcpy(temp_buf+sizeof(temp), name, strlen(name)+1);
  
      write(chat_socket, temp_buf, temp.length);
  
      return 0;
  
  }
  
  void* connectChat(int port_num, struct hostent* host_name, int master){
      char buf[MAX_BUF];
      char temp_buf[MAX_BUF];
  
      int chat_socket;
      int i;
      int input;
      int temp; //temp fd
  
      fd_set allfd, modfd;
  
      struct sockaddr_in sock;
  
      printf("Successfully Joined Room\n");
      memset(buf, 0, sizeof(buf));
      memset(&amp;sock, 0, sizeof(sock));
      sock.sin_family = AF_INET;
      sock.sin_port   = htons(port_num);
      memcpy((char*)&amp;sock.sin_addr.s_addr, host_name-&gt;h_addr, host_name-&gt;h_length);
  
      chat_socket = socket(AF_INET, SOCK_STREAM, 0);
      if(chat_socket == -1){
          perror("Error in creation");
          return NULL;
      }
  
      if(connect(chat_socket, (struct sockaddr*)&amp;sock, sizeof(struct sockaddr)) &lt; 0 ){
          perror("Error in connection");
          return NULL;
      }
  
      FD_ZERO(&amp;allfd);        
      FD_SET(chat_socket, &amp;allfd);    
      FD_SET(0, &amp;allfd);     
      FD_SET(master, &amp;allfd);
  
      while(1) {
          modfd = allfd;
          select(FD_SETSIZE, &amp;modfd, NULL, NULL, NULL);
  
          for(temp = 0; temp&lt; FD_SETSIZE; temp++){
  
              memset(buf, 0, sizeof(buf));
              memset(temp, 0, sizeof(buf));
  
              if(temp == 0) { //reading from standard in
                  input = read(0, buf, MAX_BUF);
                  buf[input-1] = '\0'; //add termination to end
                  write(chat_socket, buf, strlen(buf));
  
              }
              else if(temp == chat_socket){
                  input = read(0, buf, MAX_BUF);
                  buf[input] = '\0';
  
                  memcpy(temp, buf, input);
                  //display message
                  printf("%s \n", temp_buf);
              }
              else if(temp == master){
                  struct msg temp_message;
                  input = read(temp, buf, MSG_SIZE);
                  memcpy(&amp;temp_message, buf, MSG_SIZE);
                  if(temp_message.type == 12){
                      printf("Chatroom has been deleted, Shutting down chatroom\n");
                      return NULL;
                  }
              }
          }
      }
  
      return 0;
  }
  
  int main(int argc, char* argv[]){
  
      char buf[MAX_BUF];
  
      int chat_socket;
      int i;
      int input;
      int temp; //temp fd
      int accept_input = 1;  // take input for stdin to create a chat thread
  
      fd_set allfd, modfd;
  
      char server_ip[SERVER_IP];
      struct hostent* host_name;
      struct sockaddr_in sock;
  
  
      struct msg temp_message;
  
      pthread_attr_t tattr;
      pthread_t tid;
  
      if(argc &lt; 2) {
          printf("Please try ./crc &lt;server IP&gt; \n");
          return -1;
      }
  
      pthread_attr_init(&amp;tattr);
      pthread_attr_setdetachstate(&amp;tattr, PTHREAD_CREATE_DETACHED);
  
      memset(&amp;sock, '\0', sizeof(sock));
      sock.sin_family = AF_INET;
      sock.sin_port   = htons(PORT_NUM);
      strcpy(server_ip ,argv[1]);
  
  
      if((host_name = (struct hostent*)gethostbyname(server_ip)) ==  NULL){
          perror("failed to get host name");
          return -1;
      }
  
      memcpy((char*)&amp;sock.sin_addr.s_addr, host_name-&gt;h_addr, host_name-&gt;h_length);
  
      chat_socket = socket(AF_INET, SOCK_STREAM, 0);
      if(chat_socket == -1){
          perror("Error creating socket");
          return -1;
      }
  
      if((connect(chat_socket, (struct sockaddr*)&amp;sock, sizeof(sock))) &lt; 0) {
          perror("Error connecting");
          return -1;
      }
  
      FD_ZERO(&amp;allfd);        /* first, clear the allfd set */
      FD_SET(chat_socket, &amp;allfd);    /* adding client to the set */
      FD_SET(0, &amp;allfd);  
      printf("*****Welcome to the Chatroom*****\n");
  
      while(1){
          modfd = allfd;
          select(FD_SETSIZE, &amp;modfd, NULL, NULL, NULL);
  
          for(temp = 0; temp &lt; FD_SETSIZE; temp++){
              if(FD_ISSET(temp, &amp;modfd)){
  
                  if(temp == 0){
  
  
                      input = read(0, buf, MAX_BUF);
                      buf[input-1] = '\0';  //remove \n inserts termination
  
                      if((strncasecmp(buf, "create ", 7) == 0)) {
                          sendCmd(chat_socket, buf, 0);
                      }
                      else if((strncasecmp(buf, "join ", 5) == 0)) {
                          sendCmd(chat_socket, buf, 1);
                      }
                      else if((strncasecmp(buf, "delete ", 7)==0)){
                          sendCmd(chat_socket, buf, 2);
                      }
                      else
                      {
                          printf("Enter a valid command: create &lt;Room_name&gt;, join &lt;Room_name&gt;, delete &lt;Room_name&gt;\n");
                      }
                  }
                  else if(temp == chat_socket){
                      input = read(temp, buf, MSG_SIZE);
                      memcpy(&amp;temp_message, buf, sizeof(temp_message));
  
                      if(temp_message.type == 10) {
                          printf("Created Chatroom\n");
                      }
                      else if(temp_message.type == 11){
                          connectChat(temp_message.length, host_name, chat_socket);
                              fflush(stdin);
                      }
                      else if(temp_message.type == 15) {
                          printf("Chatroom exits. Type join &lt;Room_name&gt; to join\n");
                      }
                  }
              }
          }
      }
  
      close(chat_socket);
      return 0;
  }
  </code></pre>
  
  <p>I gave both sets of code just in case it was need. The program is designed to create multiple chat rooms. </p>