c - Segmentation fault: 11 Server/Client -


enum recstate {     initial };  int num_clients = 0;  static void addclient(int fd, struct in_addr addr){     struct client *p = (struct client *) malloc (sizeof(struct client));     if (!p) {         fprintf(stderr, "out of memory!\n");         exit(1);     }     printf("adding client %s\n", inet_ntoa(addr));     p->fd = fd;                p->next = top;             p->state = initial;        top = p;                  num_clients++; }  struct client {     int fd;               // socket descriptor client     enum recstate state;  // current state of data transfer client     struct client *next;  // pointer next client in list     struct in_addr ipaddr; } *top = null;  int main(int argc, char* argv[]){     int listenfd, clientfd, maxfd, nready;     struct client *p;     struct sockaddr_in self, client;      listenfd = socket(af_inet, sock_stream, 0);     memset(&self, '\0', sizeof(self));     self.sin_family      = af_inet;      self.sin_addr.s_addr = inaddr_any;      self.sin_port        = htons(port);       if (bind(listenfd, (struct sockaddr *) &self, sizeof(self))){         perror("bind");         exit(1);     }      if (listen(listenfd, 5) < 0){         perror("listen");         exit(1);     }      while (1){         fd_set allset, rset;         fd_zero(&allset);                    fd_set(listenfd, &allset);           maxfd = listenfd;                     rset = allset;          (p = top; p; p = p->next){             fd_set(p->fd, &allset);              if (p->fd > maxfd){                 maxfd = p->fd;                   }         }          if(fd_isset(listenfd, &rset)){              printf("listenfd ready\n");             len = sizeof(client);              if ((clientfd = accept(listenfd, (struct sockaddr *) &client, &len)) < 0){                 perror("accept");                 return(1);             }              else {                 printf("connection %s\n", inet_ntoa(client.sin_addr));                 addclient(clientfd, client.sin_addr);                  fd_set(clientfd, &rset);                 printf("clientfd has been added fdset\n");                  if(p->state == initial){ //cannot if statement                     printf("now in initial state\n");                      nready = select(maxfd + 1, &rset, null, null, null);                      if(nready == 0){                         printf("timeout happened\n");                         continue;                     }                     else if(nready == -1){                         perror("select");                         continue;                     }                     else if (nready > 0){                         printf("data available.\n");                         continue;                     }                      if (fd_isset(listenfd, &rset)){ //returns value fd in rset                         //read data file                     }                 }             }         }     } } 

this part of server code. i'm trying send files client server, when send files client, server execute until above if(p->state == initial) statement, , hangs there. when terminate server, it'll give me segmentation fault: 11 error. also, i'm using select() inside initial state allow multiple clients connect simultaneously. don't know went wrong, appreciated. thanks.

for (p = top; p; p = p->next){

unless i'm mistaken, loop until p null

if(p->state == initial){

and dereference p. kaboom.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -