c - Reading packets from a client stops after 1 or 2 received -


i'm writing server program in c read commands client. commands in form of 5 byte packets, , client sending bunch of them in succession. code have read each command is:

while(1) {     char buffer[1024];     int alreadyread = 0;     int socket = dequeue();      while(alreadyread != 5)     {                 {             nowread = read(socket,buffer+alreadyread,5-alreadyread);             alreadyread += nowread;         }         while((nowread > 0) && (5-alreadyread > 0));          if(nowread == -1 || nowread == 0)         {             printf("error reading client socket\n");             exit(1);         }               //do command 

but doesn't seem work: if client sends 10 packets, read 1 or 2 , segfault. have idea why?

the while (alreadyread != 5) seems extraneous since alreadyread 5 or nowread less 0 after do-while, , if nowread less 0 exit.

you've poorly defined command, think need more information on that.

it looks you're getting new socket after each command. if it's 1 client sending commands, should keep socket until commands want. should close socket after done don't run out of file descriptors.

the segfault caused area of program haven't shown.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -