c - Unable to receive the last socket data sent using the MSG_MORE flag -


server side code:

 dirp=opendir(path);     if(dirp==null)     {         strcpy(err,"error:");         strcat(err,strerror(errno));         send(fd,err,sizeof(err),0);      }            else     {         printf("\nstream opened\n");         while((dp=readdir(dirp))!= null)         {              r=send(fd,dp->d_name,100,msg_more);             if(r<0)                 perror("\nsend:");             printf("\n%s",dp->d_name);          }     } 

client:

while(recv(mainsocket,lsbuf,100,0)>0) {     printf("\n %s",lsbuf);     bzero(lsbuf,sizeof(lsbuf));  } 

the server side printing filenames on standard output,but on client side client not receiving last filename , program getting blocked @ point

the problem send syscall. call msg_more flag means more data follow , send waits more data without sending. last chunk of data should send without flag. server side should like:

dp = readdir(dirp); if (dp != null) {     /* each time check whether there more data */     while((dp_next = readdir(dirp))!= null)     {         r = send(fd, dp->d_name, 100, msg_more);         if (r < 0) {             perror("\nsend");         }         printf("\n%s",dp->d_name);         dp = dp_next;     }      /* send last or record */     r = send(fd, dp->d_name, 100, 0);     if (r < 0) {         perror("\nsend");     } } 

another posibility fix problem close conenction close(fd) syscall. send data in buffer before closing connection. it's less clean, more simple solution.


Comments