How to pool on socket ?

  • Thread starter Thread starter User
  • Start date Start date
U

User

Hi,

How do I pool my socket to see if there is any message pending?

I don't want to use timers because they are multithreaded and it break
the linearity of my messages. (incoming message are numbered and it is
important to keep the order)

Can I add a handler to the socket? So it could react by itself when
there is pending data? Something like that the winsock (in VB6) was doing.

Thank you.
 
Hi,

How do I pool my socket to see if there is any message pending?

I don't want to use timers because they are multithreaded and it break
the linearity of my messages. (incoming message are numbered and it is
important to keep the order)

Can I add a handler to the socket? So it could react by itself when
there is pending data? Something like that the winsock (in VB6) was doing.

Thank you.

The absolute best way to do this would most likely be to use the asnyc
methods of the socket class (BeginXXXX). The socket will then fire a
callback when data is received.

You could also use the Poll method on the socket to determine if there
is data. You can also use Socket.Select along with Receive with the
Peek flag to determine the socket state...

There are a number of ways that you can accomplish what you're asking,
but the Asnyc methods are usually the most efficient.
 
Thank you Tom,

I have another question. I haven't use this async socket yet, but I'll
read on it. Right now, I am using a timer control to pool over the
socket.pending to see if there is new data to process. The timer is
running all day long.

Does the async socket, once started, will pool the socket as long as it
is not stopped? I mean, I start it once and will run as the same way I
am using the timer control right now?

Thank you.
 
Thank you Tom,

I have another question. I haven't use this async socket yet, but I'll
read on it. Right now, I am using a timer control to pool over the
socket.pending to see if there is new data to process. The timer is
running all day long.

Does the async socket, once started, will pool the socket as long as it
is not stopped? I mean, I start it once and will run as the same way I
am using the timer control right now?

Thank you.

Basically, once you begin the async operation, it will continue until the
socket is closed - or the call back is fired. Once it is fired, you need
to call the async method again... So just for a quick psuedo code example:

begin serverfunc
socket.bind
socket.listen
while (running)
manualresetevent.reset
socket.beginaccept
manualresetevent.waitone
wend
end serverfunc


begin acceptfunc
' get socket object
client = get_from_async_object

' signal server thread to continue
manualresetevent.set

client.beginread
end acceptfunc

begin readfunc
' do read stuff

' send a reply
client.beginsend
end readfunc

begin sendfunc
if sent client.beginread
end sendfunc

etc, etc...

There are some good articles on msdn (this should get you in the correct
location):

http://msdn.microsoft.com/library/d...e/html/cpconUsingNon-BlockingServerSocket.asp

There is a little more code involved in using the async methods, but they
are the most scalable and efficient.
 
That is very interesting Tom, I red the article on MSDN, I got to use it.

Thank you very much.
 
Back
Top