Thursday, September 11, 2008

HTTP/Socket 3

I had been working on a project, the chain browser mentioned before, and i went back to polish up the socket portion of it. The problem i faced was i would get the right data back from a server but there would be a delay on the last recv() that slowed everything down to a crawl. Initially i discovered that id i put a Shutdown() after i sent the data i would get back the data quickly without that delay. But this was unpredictable because sometimes i wouldn't get anything. So i had to remove it to retain predictability. So after some more studying i learned about using select() to delay the program till there was data ready to recv(). However using select didn't really seem to do much and it wasn't till I added in ioctlsocket(FIONREAD) (or just ioctl() if your using Berkly sockets). the trick was putting the ioctlsocket() in the right spot. At first it would return 0 bytes which meant that there was 0 bytes ready to recv(), which wasn't accurate. the key is to wait with select till there is data available, then figure out how much with ioctlsocket(FIONREAD), and lastly recv() but only the amount of bytes you get from the ioctlsocket(). After playing around with it for a little it worked great about 99% of the time. The socket library is very well documented but how to use it all together isn't. Not to mention there are hundreds of different types of applications using sockets and they all using them differently. whats even worse is that protocols like http use sockets differently throughout the protocol itself.
Lastly i posted a a file, something like Socket.cpp and Socket.h for an example. if you have any questions just email me or leave a comment. also consider that those files are part of a larger project. While i haven't posted an up to date versoin of that project you should be able to plug the "socket files" into one of the other projects i have posted.

No comments: