Re: Non-blocking method for reading writing objects to sockets



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
.



Relevant Pages

  • Re: Non-blocking method for reading writing objects to sockets
    ... also am writing a client/server using non blocking io. ... I'm sending buffers of bytes. ... When a client connects, register the channel with a reading selector ...
    (comp.lang.java.programmer)
  • Is there a way to do real time streaming with asf?
    ... the app and placing them into an .asf file set to 30fps and then serving that ... The problem I'm hitting is that the 'client' that connects (currently wmp, ... but eventually will be a wmp hosted in media center) buffers the video. ... causes a 3 to 5 second delay in what I'm doing to the apps responsiveness. ...
    (microsoft.public.windowsmedia.sdk)
  • Re: Socket switch delay
    ... buffers are queued and will be later sent. ... socket, not sure about a blocking socket. ... >>(and I noticed you don't do overlapped receive at the server!) ... > optional in the client too). ...
    (microsoft.public.win32.programmer.networks)
  • Re: Bazar Behavior Testing Select(2) Code
    ... >> The problem isn't the client input or the server output. ... I'm now seeing all 4 buffers flooded. ...
    (comp.unix.programmer)
  • Re: Trend CS Suite or CSM Suite for SMB?
    ... ScanMail / Exchange-aware A/V). ... before they even get to user's mailboxes, ... blocking & spam filtering. ... > I have a client who will be installing SMB 2k3 Prem but will not be ...
    (microsoft.public.windows.server.sbs)