6.8. Adding some refinements to our approach

Not too bad for our first multi-client server program, is it? There's some room for improvement however. This topic will discuss what we could've done better, and what to do about it.

First of all, we're sending any outgoing data with the WrLine procedure. WrLine() is designed to be used with blocking sockets. As I mentioned before, this doesn't really cause problems in our sample program because there's only a small amount of data to send. The chances of us somehow filling up the system's TCP buffer is very remote, so we probably won't ever notice the problem.

But in a real application? One where files are being sent, or data is being typed by another user? We'd be sure to have problems! As soon as the TCP buffer is full, data would simply be cut off.

Speaking of buffers, the read buffer has a problem too. Right now, we add to the read buffer. That's not a problem, however, we only take data out of the read buffer when a x'0A' (end-of-line) character is found. That means that if a client should fill up his read buffer without having an end of line character, it'll simply STOP receiving data from that client!

The 'wait' indicator is a neat idea, but it wouldn't really be necessary if we handled writes correctly. If we handled them correctly, data would go into a write buffer, and be written as soon as writing to the socket was feasible. If we did that, there'd be little point to having a 'wait' indicator, we'd just always do the full 60 second timeout.

Finally, why are we setting the 'readset' for a client every single time we do a select()? We should only set descriptor on if we have space in our read buffer. That way, we don't have to go through the motions of trying to do a recv() when we know there isn't anyplace to put the data.

I suggest that you try to solve all of these problems yourself. It'll be a good challenge for you -- trying to implement a write buffer, and handle a full read buffer, etc.

If you get stuck, take a look at my solution to the problem, which is online at this address: http://www.scottklement.com/rpg/socktut/qrpglesrc.serverex4