By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

How to pool on socket ?

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <dcaOc.118392$Rf.22568@edtnps84>, User wrote:
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.
--
Tom Shelton [MVP]
Nov 20 '05 #2

P: n/a
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.

Tom Shelton wrote:
In article <dcaOc.118392$Rf.22568@edtnps84>, User wrote:
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.


Nov 20 '05 #3

P: n/a
On Fri, 30 Jul 2004 12:36:13 GMT, User wrote:
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/de...rverSocket.asp

There is a little more code involved in using the async methods, but they
are the most scalable and efficient.

--
Tom Shelton [MVP]
Nov 20 '05 #4

P: n/a
On Fri, 30 Jul 2004 12:36:13 GMT, User wrote:
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/de...rverSocket.asp

There is a little more code involved in using the async methods, but they
are the most scalable and efficient.

--
Tom Shelton [MVP]
Nov 20 '05 #5

P: n/a
That is very interesting Tom, I red the article on MSDN, I got to use it.

Thank you very much.

Tom Shelton wrote:
On Fri, 30 Jul 2004 12:36:13 GMT, User wrote:

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/de...rverSocket.asp

There is a little more code involved in using the async methods, but they
are the most scalable and efficient.


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.