Re: Non-blocking method for reading writing objects to sockets
- From: "John C. Bollinger" <jobollin@xxxxxxxxxxx>
- Date: Wed, 11 May 2005 15:53:28 -0500
jmricker wrote:
I would like to somehow interject my question into this discussion. I also am writing a client/server using non blocking io. However I have set up a selector to select those clients with buffers ready to be read. Instead of using objects, I'm sending buffers of bytes. My buffers are set up as:
[int id][int id2][int payload size][ payload of bytes of size length ]
Now lets say my client has sent off two of these buffers as two seperate (blocking?) writes in the time it takes for my server to get around to readying from it. Are you saying that a blocking read will just get the first buffer?
No, I'm not saying that. A single blocking read will get some number of bytes, which may be anything from a single byte to the full contents of both buffers (subject to various constraints, such as hardware and O/S buffer sizes).
So far what I've been doing is this:
When a client connects, register the channel with a reading selector with an object that wraps up a bytebuffer and does other administrative tasks like checking header completeness.
When I select channels that I can read from, I read up to 512 bytes, and push it into the container attached to the channel. I then check it for completeness. If not, then I go off checking other channels, then come back to this one, read off another 512 bytes, etc. Once I have a complete header and payload, I pass it off the appropriate controller, reset the container, and continue on.
That sounds like the right way to go about it, although you need to be prepared for the case where you get the end one block _plus_ part or all of another in the same read. If the client is sending data rapidly then this is not too unlikely to happen.
I'm hoping to avoid the loop spending too much time reading in from one client. My concern here is here, how can I be sure where the end of the buffer is at? Lets say the client sends an incorrect payload size or a byte of the payload gets lost in the transmission. If I have two buffers sitting to be read, I could easily start reading bytes from the second buffer into the first buffer. Any thoughts on my process?
TCP provides reliable transmission. It's not foolproof, but you don't generally have to worry about data being lost in transmission without the connection being broken altogether. (It would take a combination of errors that is so unlikely to occur that you might as well ignore the possibility.) Likewise accidental data corruption on the wire. On the other hand, you either have to trust the client or have some way to detect when it goes goofy. Your protocol does not seem to support the latter.
I don't think any of that has much to do with hanging while reading from any particular client. If a channel is ready to read then you can read some number of bytes from it without blocking -- that's what it means for the channel to be ready. You can use a blocking or nonblocking read in this case; it doesn't matter. The alternative is to assign a unique thread to each client, use blocking I/O, and get on with the rest of the application. There are a great many servers that take the latter approach.
-- John Bollinger jobollin@xxxxxxxxxxx .
- References:
- Non-blocking method for reading writing objects to sockets
- From: Sameer
- Re: Non-blocking method for reading writing objects to sockets
- From: John C. Bollinger
- Re: Non-blocking method for reading writing objects to sockets
- From: Sameer
- Re: Non-blocking method for reading writing objects to sockets
- From: John C. Bollinger
- Re: Non-blocking method for reading writing objects to sockets
- From: John C. Bollinger
- Re: Non-blocking method for reading writing objects to sockets
- From: jmricker
- Non-blocking method for reading writing objects to sockets
- Prev by Date: Re: sql*plus diagrams
- Next by Date: Re: JVM optimization
- Previous by thread: Re: Non-blocking method for reading writing objects to sockets
- Next by thread: Re: Non-blocking method for reading writing objects to sockets
- Index(es):
Relevant Pages
|