Re: Question about non-blocking NIO and Selection Keys



On Jul 2, 12:03 am, Neil Coffey <neil.cof...@xxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
Zachary Turner wrote:
Well, most of this is due to the fact that it was originally designed
around blocking sockets, and without a complete re-write it's hard to
do it correctly.  The idea is that I want to wait *until the socket is
ready for writing*, then I want to write N bytes of data and return
immediately.

The sole reason this was changed from blocking to non-blocking is
because we need to support the case where the client can
asynchronously send a message to the server telling it to stop sending
more data, so the server needs to be able to do a non-blocking read,
because otherwise there's no way to say "is there data waiting?  if so
read it, if not let me do other stuff".

Isn't your communication protocol just broken, then? What you
seem to be saying is that sometimes you need write/reads to be
sequenced, but sometimes you don't... Possibly things could be improved
by doing one of the following:

- use a separate connection for the asynchronous control message
   (obviously wasteful, but OK for a small number of connections)
This could be an option, I considered it initially but wrote it off
without much thought because non-blockign sockets seemed like the way
to go.

- make everything synchronous: after every portion of data sent, you
   expect a response from the client "OK got that" or "OK got that but
   don't send more"; if there's no data for you to send in X seconds,
   send some "dummy" message (and expect a response) just to check if
   the client is wanting to stop communication
Actually tried this, but considering the volume of data being sent,
the constant round trips back to the server were having a noticeable
performance impact. I got about 10% higher throughput just by
removing a 4 byte acknowledgement from client -> server after every
chunk of data.

- Of course, the client could also signal to the server that it
   didn't want more data by closing the connection... :-)
Well, it's really more just like "I don't want any more of THAT
data." But the server still has lots of other data to send.


The problem is that whatever I do has to fit nicely and transparently
into the implementation of a class that used to represent a blocking
socket, and is now scattered across 10s of thousands of lines of code
of our server. So what you said at the very beginning is pretty much
spot on. It needs to be blockign most of the time, but sometimes it
doesn't. I know it sounds stupid, but that's what happens when stuff
is designed poorly in the beginning and you just have to make
something work.

What I'm after is whatever is the closest I can possibly get to a
blocking socket with the added ability to "peek" at the socket's recv
buffer.
.



Relevant Pages

  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Locking on async calls
    ... you must synchronize the entire SendMessage routine as an atomic ... operation to prevent mixed messages from being transmitted to the server. ... You are correct that read and write on the socket do not interfere with each ... If you want to handle multiple client connections from one server object ...
    (microsoft.public.dotnet.general)
  • Re: socket communication: socket doesnt connect
    ... Microsoft MVP, MCSD ... As far as your server code goes, ... accept the listening socket. ... Client client = new Client; ...
    (microsoft.public.vc.language)
  • Re: TCP server stop receiving new connections
    ... reset the event mask of your listening socket each time you ... I have a strange problem in my class library used by all our client ... server applications. ... incomming connections, but keeps current connections. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Design issue with WinSock/GetQueuedCompletionStatus
    ... delegate that to a shutdown routine called after all worker threads ... The application I've created is a server accepting connections on a few ... different TCP/IP ports and then lets the client run different commands. ... a TCP/IP socket can be closed for 2 different reasons: ...
    (microsoft.public.win32.programmer.networks)

Loading