c - UDP multi-client chat server -


i have multi-client chat server , reason first client being added. used tutorial me started. have included code below. when try , add client doesnt appear added. if add 1 client response server want first message enter after stops sending correctly.

server code:

 int main(void)    {    struct sockaddr_in my_addr, cli_addr[10],cli_temp;    int sockfd;    socklen_t slen[10],slen_temp;   slen_temp = sizeof(cli_temp);  char buf[buflen];    int clients = 0;  int client_port[10];   if ((sockfd = socket(af_inet, sock_dgram, ipproto_udp))==-1)  {      printf("test\n");      err("socket");    }else{        printf("server : socket() successful\n");    }     bzero(&my_addr, sizeof(my_addr));    my_addr.sin_family = af_inet;    my_addr.sin_port = htons(port);    my_addr.sin_addr.s_addr = htonl(inaddr_any);     if (bind(sockfd, (struct sockaddr* ) &my_addr, sizeof(my_addr))==-1)  {      err("bind");    }else{      printf("server : bind() successful\n");    }   int num_clients = 0;  while(1)    {      //receive      printf("receiving...\n");      if (recvfrom(sockfd, buf, buflen, 0, (struct sockaddr*)&cli_temp, &slen_temp)==-1)            err("recvfrom()");       if (clients <= 10) {         cli_addr[clients] = cli_temp;         client_port[clients] = ntohs(cli_addr[clients].sin_port);         clients++;         printf("client added\n");         //printf("%d",clients);         int i;         for(i=0;sizeof(clients);i++) {             sendto(sockfd, buf, buflen, 0, (struct sockaddr*)&cli_addr[i], sizeof(cli_addr[i]));          }      }  }     close(sockfd);    return 0;   

}

i have included client code in case helps.

void err(char *s)   {   perror(s);   exit(1);   }   sig_atomic_t child_exit_status;  void clean_up_child_process (int signal_number)  { /* clean child process. */  int status;  wait (&status);  /* store exit status in global variable. */  child_exit_status = status; }   int main(int argc, char** argv)    {    struct sockaddr_in serv_addr;    int sockfd, slen=sizeof(serv_addr);    char buf[buflen];    struct sigaction sigchld_action;   memset (&sigchld_action, 0, sizeof (sigchld_action));   sigchld_action.sa_handler = &clean_up_child_process;   sigaction (sigchld, &sigchld_action, null);  int pid,ppid;   if(argc != 2)    {      printf("usage : %s <server-ip>\n",argv[0]);      exit(0);    }     if ((sockfd = socket(af_inet, sock_dgram, ipproto_udp))==-1)        err("socket");     bzero(&serv_addr, sizeof(serv_addr));    serv_addr.sin_family = af_inet;    serv_addr.sin_port = htons(port);    if (inet_aton(argv[1], &serv_addr.sin_addr)==0)    {        fprintf(stderr, "inet_aton() failed\n");        exit(1);    }  pid = fork();  if (pid<0) {      err("fork error");  }else if (pid==0) {      //child process receive server      while (1) {          bzero(buf,buflen);          //printf("attempting read socket %d: ",sockfd);          fflush(stdout);          //recvfrom here          if (recvfrom(sockfd, buf, buflen, 0, (struct sockaddr*)&serv_addr, &slen)==-1)                err("recvfrom()");           printf("the message server is: %s \n",buf);          if (strcmp(buf,"bye\n") == 0) {              ppid = getppid();              kill(ppid, sigusr2);               break;          }      }  }else {      //parent send server      while(1){          printf("please enter message send: ");          bzero(buf,buflen);          fgets(buf,buflen,stdin);          printf("attempting write socket %d: ",sockfd);          fflush(stdout);          //send here          if (sendto(sockfd, buf, buflen, 0, (struct sockaddr*)&serv_addr, slen)==-1)          {              err("sendto()");          }      }  }  close(sockfd);    return 0;   

}

several problems jump out @ me. first, every time receive message consider new client. instead of incrementing clients variable message, you'll need scan through array see if source address present. second, sizeof(clients) return static value (probably 4) depending on how many bytes int occupies on machine. loop should for( int = 0; < clients; i++ ).

you have variable named num_clients not used. supposed there , maybe causing confusion?

finally, instead of using magic value 10 on place, use #define max_connections 10 , replace numbers max_connections. it's lot easier read , change later.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -