I'm programming in C an IRC chat client. everything it's working well except I can't read the whole answer sent by the server. here's the code:
char buffer[2048];
write_on_screen(current_page(), "LOG COMMAND", command);
write(sockfd, command, strlen(command)); //write to socket
bzero(buffer, sizeof(buffer));
read(sockfd, buffer, sizeof(buffer));
write_on_screen(current_page(), "RESPONSE", buffer);
return buffer;
most of the time buffer will contain just a piece of the response (which is shorter than 2048 bytes) and other times it contains nothing. in both cases if I do another read()
after the first one, it returns me the rest of the answer or another small piece (and then I've to do another read()
again). if I put a sleep(1)
between write()
and read()
I get the whole answer, but I'm sure this not a good pratice.
Is there some way I can avoid this?
thank you in advance