views:

30

answers:

0

I have another tricky situation with my socket experience. I'm writing a non-blocking server/client for some key-value data storage.

Here's the situation:

  1. connect to server
  2. post data to it (wait for no confirmation that data's being submited)
  3. right after then (staying on the same connected socket) I'm trying to read that data

Here's the problem: I successfuly send my data request to server (as it looks on client) but it never reads it. It sits in SelectMode.Write and won't bother trying to read. If I look at the Socket object (I'm using C#) I see that AvailableData is 0 also. And it happens like in 99% of cases, so if I run my write operation on client and with no delay try to run the read operation it hangs.

But if I add a small delay after the write operation, like Thread.Sleep(10), no such problem occurs. I ran quite a lot of testing and it never hangs. Also if I do any number of read operations which do a write and then read to the socket it never hangs also. So the problem is somehow related to server implementation?

Here's metacode for my server:

get list of active sockets
create read list  // containing all sockets actually
create write list // same
create error list // same
Socket.Select
foreach( socket in readSockets ) // doing reading with small buffer in a cycle
foreach( socket in writeSockets ) // doing writing
foreach( socket in errorSockets ) // process errors (never seen any error here for somereason)

And here's metacode for client (it's not really non-blocking; it uses non-blocking sockets but use 1 thread per socket):

connect to remote host // i guess no need to post for that
do
    if socket.Poll( SelectMode.SelectWrite ) 
        socket.send( mybuffer )
if needToGetResult // it's a marker for command wether it needs to wait for result
    do 
        if socket.Poll( SelectMode.SelectRead )
            socket.recieve( socket.AvaliableData )
        else
            socket.recieve( socket.AvaliableData ) // in some cases poll returns false but I still can read (socket buffer is full?)
else
    Thread.Sleep(10) // if i add this, everything works smoothly, otherwise it hangs

Something like that. If I'm doing pure read commands it produces like ~300 operations per second, so commands run in a row really.

Test code is something like this:

NewClient client = new NewClient( "127.0.0.1", 9991, log );

for( int i = 0; i < 10000; i++ )
{
    client.Set( "myohmy", "tttt" );
    string data = client.Get( "myohmy" ).ToString();
}

What could be the problem?