Problem in read function of sockets

Hi all,

I am using Juce sockets to interact with the webservers. I am using the read() function – Socket::read() . Wait till you get a response as -1.

The code for reading the response is as follows:

ilen = 1; while(ilen != -1) { //reading the response and appending to the m_strResponse ilen =, MAX_DATA_SIZE); if(ilen > 0) { chBuff[ilen] = '\0'; Response=Response+chBuff; } }
Now the “read()” fucntion says that it reads the data from the socket and returns the length of data read. And returns -1 in case it isn’t able to read or it encounters any error.

  1. But some servers don’t read the comeplete data and just goes out of the while after the first read.

  2. Some read the data properly but when they didn’t get any data or there is any error in reading the reutrning of -1 takes a very long time, there by making the whole application slow.

So i modified the code for the servers which take a long time to read -1 and thought it would be generic for all.

while(ilen > 0) { //reading the response and appending to the m_strResponse ilen =, MAX_DATA_SIZE); if(ilen == -1 || ilen < MAX_DATA_SIZE) { chBuff[ilen] = '\0'; Response=Response+chBuff; break; } if(ilen != -1) { chBuff[ilen] = '\0'; Response=Response+chBuff; } }

Of course the standards i followed is not correct , anyway it solves the 2nd problem, but the first problem still persists i.e. incomplete data read.

Can somebody suggest me a way to solve this one ? Are there some other steps i need to follow during reading data from a socket, or some other precautions i need to take so that data is not lost ?

Thanks in Advance

There are probably people who are more experienced with sockets than I am who might be able to give a better solution here…