views:

234

answers:

2

A few words about an ongoing design and implementation

I send a lot of requests to the remote application (running on a different host, of course), and the application send back data.

About client

Client is a UI that spawn a separate thread to submit and process the requests. Once it submits all the requests, it calls Wait. And the Wait will parse all events coming the app and invoke client's callbacks.

Below is the implementation of Wait.

public void Wait (uint milliseconds)
{
       while(_socket.IsConnected)
       {
         if (_socket.Poll(milliseconds, SelectMode.SelectRead))
         {
              // read info of the buffer and calls registered callbacks for the client
              if(_socket.IsAvailable > 0)
                    ProcessSocket(socket);
         }
         else
            return; //returns after Poll has expired
      }
}

The Wait is called from a separate thread, responsible for managing network connection: both inbound and outbound traffic:

         _Receiver = new Thread(DoWork);
         _Receiver.IsBackground = true;
         _Receiver.Start(this);

This thread is created from UI component of the application.

The issue:

client sometimes sees delays in callbacks even though main application has sent the data on time. Notably, one the message in Poll was delayed until I client disconnected, and internally I called:

_socket.Shutdown(SocketShutdown.Both);

I think something funky is happening in the Poll

Any suggestions on how to fix the issue or an alternative workaround?

Thanks

please let me know if anything is unclear

A: 

What thread is calling the Wait() method? If you're just throwing it into the UI threadpool, that may be why you experience delays sometimes. If this is your problem, then either use the system threadpool, create a new one just for the networking parts of your application, or spawn a dedicated thread for it.

Beyond this, it's hard to help you much without seeing more code.

Ben Collins
well, I am creating a separate thread that does the processing, and in effect, responsible for calling Wait. I will illustrate how I create the thread above. thanks
A: 

A couple of things. First, in your example, is there a difference between "_socket" and "socket"? Second, you are using the System.Net.Sockets.Socket class, right? I don't see IsConnected or IsAvailable properties on that class in the MSDN documentation for any .NET version going back to 1.1. I assume these are both typing mistakes, right?

Have you tried putting an "else" clause on the "IsAvailable > 0" test and writing a message to the Console/Output window, e.g.,

  if (_socket.IsAvailable > 0) {
      ProcessSocket(socket);
  } else {
      Console.WriteLine("Poll() returned true but there is no data");
  }

This might give you an idea of what might be going on in the larger context of your program.

Aside from that, I'm not a big fan of polling sockets for data. As an alternative, is there a reason not to use the asynchronous Begin/EndReceive functions on the socket? I think it'd be straightforward to convert to the asynchronous model given the fact that you're already using a separate thread to send and receive your data. Here is an example from MSDN. Additionally, I've added the typical implementation that I use of this mechanism to this SO post.

Matt Davis
1. type error. 2. IsConnected is my wrapper and IsAvailable is just Availalble. Thanks