473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to do Async TCP Listener?

I've tried the following code straight out of MSDN. But my app still blocks
while listening. Isn't this code supposed to keep the UI responsive while
listening? Or maybe I'm not doing it right.... any help is begged for ;-)

Public Class Listener

Public Event Connected(ByVal client As TcpClient)
Public ClientConnected As New ManualResetEvent(False)

Public Sub DoBeginAcceptTcpClient()
Dim myListener As New TcpListener(IPAddress.Parse("127.0.0.1"), 23)
myListener.Start()
ClientConnected.Reset()
myListener.BeginAcceptTcpClient(New AsyncCallback(AddressOf
DoAcceptTcpClientCallback), myListener)
ClientConnected.WaitOne()
End Sub

Public Sub DoAcceptTcpClientCallback(ByVal ar As IAsyncResult)
Dim myListener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim client As TcpClient = myListener.EndAcceptTcpClient(ar)
RaiseEvent Connected(client)
ClientConnected.Set()
End Sub

End Class
Nov 21 '05 #1
8 12795
I suspect your listening socket is a "blocking" socket (not always obvious)
that the OS is running in an internal thread for you. If so, your client
socket that is created during the accept function will also be blocking. To
fix this, issue a socket ioctl call to the new client socket immediately
after the accept to switch it from blocking to non-blocking.

Mike Ober.

"Terry Olsen" <to******@hotmail.com> wrote in message
news:O3**************@TK2MSFTNGP11.phx.gbl...
I've tried the following code straight out of MSDN. But my app still blocks while listening. Isn't this code supposed to keep the UI responsive while
listening? Or maybe I'm not doing it right.... any help is begged for ;-)
Public Class Listener

Public Event Connected(ByVal client As TcpClient)
Public ClientConnected As New ManualResetEvent(False)

Public Sub DoBeginAcceptTcpClient()
Dim myListener As New TcpListener(IPAddress.Parse("127.0.0.1"), 23) myListener.Start()
ClientConnected.Reset()
myListener.BeginAcceptTcpClient(New AsyncCallback(AddressOf
DoAcceptTcpClientCallback), myListener)
ClientConnected.WaitOne()
End Sub

Public Sub DoAcceptTcpClientCallback(ByVal ar As IAsyncResult)
Dim myListener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim client As TcpClient = myListener.EndAcceptTcpClient(ar)
RaiseEvent Connected(client)
ClientConnected.Set()
End Sub

End Class

Nov 21 '05 #2
How do I create a listener that doesn't block? I want to be able to stop
the listner from my app, but the app is locked up while it's listening.
"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
I suspect your listening socket is a "blocking" socket (not always obvious)
that the OS is running in an internal thread for you. If so, your client
socket that is created during the accept function will also be blocking.
To
fix this, issue a socket ioctl call to the new client socket immediately
after the accept to switch it from blocking to non-blocking.

Mike Ober.

"Terry Olsen" <to******@hotmail.com> wrote in message
news:O3**************@TK2MSFTNGP11.phx.gbl...
I've tried the following code straight out of MSDN. But my app still

blocks
while listening. Isn't this code supposed to keep the UI responsive
while
listening? Or maybe I'm not doing it right.... any help is begged for

;-)

Public Class Listener

Public Event Connected(ByVal client As TcpClient)
Public ClientConnected As New ManualResetEvent(False)

Public Sub DoBeginAcceptTcpClient()
Dim myListener As New TcpListener(IPAddress.Parse("127.0.0.1"),

23)
myListener.Start()
ClientConnected.Reset()
myListener.BeginAcceptTcpClient(New AsyncCallback(AddressOf
DoAcceptTcpClientCallback), myListener)
ClientConnected.WaitOne()
End Sub

Public Sub DoAcceptTcpClientCallback(ByVal ar As IAsyncResult)
Dim myListener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim client As TcpClient = myListener.EndAcceptTcpClient(ar)
RaiseEvent Connected(client)
ClientConnected.Set()
End Sub

End Class


Nov 21 '05 #3
You need to derive a new object from your listening socket object. There is
an overridable "OnAccept" event in VB6's WINSOCK.ocs and in the MFC
CAsyncSocket class so there should be one in .Net as well.

Mike.

"Terry Olsen" <to******@hotmail.com> wrote in message
news:Od****************@TK2MSFTNGP10.phx.gbl...
How do I create a listener that doesn't block? I want to be able to stop
the listner from my app, but the app is locked up while it's listening.
"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:uu**************@TK2MSFTNGP09.phx.gbl...
I suspect your listening socket is a "blocking" socket (not always obvious) that the OS is running in an internal thread for you. If so, your client socket that is created during the accept function will also be blocking.
To
fix this, issue a socket ioctl call to the new client socket immediately
after the accept to switch it from blocking to non-blocking.

Mike Ober.

"Terry Olsen" <to******@hotmail.com> wrote in message
news:O3**************@TK2MSFTNGP11.phx.gbl...
I've tried the following code straight out of MSDN. But my app still

blocks
while listening. Isn't this code supposed to keep the UI responsive
while
listening? Or maybe I'm not doing it right.... any help is begged for

;-)

Public Class Listener

Public Event Connected(ByVal client As TcpClient)
Public ClientConnected As New ManualResetEvent(False)

Public Sub DoBeginAcceptTcpClient()
Dim myListener As New TcpListener(IPAddress.Parse("127.0.0.1"),

23)
myListener.Start()
ClientConnected.Reset()
myListener.BeginAcceptTcpClient(New AsyncCallback(AddressOf
DoAcceptTcpClientCallback), myListener)
ClientConnected.WaitOne()
End Sub

Public Sub DoAcceptTcpClientCallback(ByVal ar As IAsyncResult)
Dim myListener As TcpListener = CType(ar.AsyncState, TcpListener) Dim client As TcpClient = myListener.EndAcceptTcpClient(ar)
RaiseEvent Connected(client)
ClientConnected.Set()
End Sub

End Class



Nov 21 '05 #4
On 2004-11-21, Terry Olsen <to******@hotmail.com> wrote:
I've tried the following code straight out of MSDN. But my app still blocks
while listening. Isn't this code supposed to keep the UI responsive while
listening? Or maybe I'm not doing it right.... any help is begged for ;-)

Public Class Listener

Public Event Connected(ByVal client As TcpClient)
Public ClientConnected As New ManualResetEvent(False)

Public Sub DoBeginAcceptTcpClient()
Dim myListener As New TcpListener(IPAddress.Parse("127.0.0.1"), 23)
myListener.Start()
ClientConnected.Reset()
myListener.BeginAcceptTcpClient(New AsyncCallback(AddressOf
DoAcceptTcpClientCallback), myListener)
ClientConnected.WaitOne()
End Sub

Public Sub DoAcceptTcpClientCallback(ByVal ar As IAsyncResult)
Dim myListener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim client As TcpClient = myListener.EndAcceptTcpClient(ar)
RaiseEvent Connected(client)
ClientConnected.Set()
End Sub

End Class


Unless your DoBeginAcceptTcpClient () method is on a separate thread,
then it will block - but not at the BeginAcceptTcpClient, but at the
ClientConnected.WaitOne ().

Normally, I would start listening on a separate thread, and do something
like:

Do While Until Terminate
ClientConnected.Reset()
MyListener.BeginAccept......
ClientConnected.WaiteOne ()
Loop

This way, you can handle multiple clients, and you won't block the main
thead. When you need to kill the thread, just close the listener...

Don't ask me any specifics about TcpListener/Client - I never use them.
I always use the Socket class in System.Net.Sockets :)

--
Tom Shelton [MVP]
Nov 21 '05 #5
Speaking of the ManualResetEvent...what's it used for? I commented out all
the "ClientConnected.x" code and the listener still worked fine...without
blocking at all...

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:up**************@TK2MSFTNGP10.phx.gbl...
On 2004-11-21, Terry Olsen <to******@hotmail.com> wrote:
I've tried the following code straight out of MSDN. But my app still
blocks
while listening. Isn't this code supposed to keep the UI responsive
while
listening? Or maybe I'm not doing it right.... any help is begged for
;-)

Public Class Listener

Public Event Connected(ByVal client As TcpClient)
Public ClientConnected As New ManualResetEvent(False)

Public Sub DoBeginAcceptTcpClient()
Dim myListener As New TcpListener(IPAddress.Parse("127.0.0.1"),
23)
myListener.Start()
ClientConnected.Reset()
myListener.BeginAcceptTcpClient(New AsyncCallback(AddressOf
DoAcceptTcpClientCallback), myListener)
ClientConnected.WaitOne()
End Sub

Public Sub DoAcceptTcpClientCallback(ByVal ar As IAsyncResult)
Dim myListener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim client As TcpClient = myListener.EndAcceptTcpClient(ar)
RaiseEvent Connected(client)
ClientConnected.Set()
End Sub

End Class


Unless your DoBeginAcceptTcpClient () method is on a separate thread,
then it will block - but not at the BeginAcceptTcpClient, but at the
ClientConnected.WaitOne ().

Normally, I would start listening on a separate thread, and do something
like:

Do While Until Terminate
ClientConnected.Reset()
MyListener.BeginAccept......
ClientConnected.WaiteOne ()
Loop

This way, you can handle multiple clients, and you won't block the main
thead. When you need to kill the thread, just close the listener...

Don't ask me any specifics about TcpListener/Client - I never use them.
I always use the Socket class in System.Net.Sockets :)

--
Tom Shelton [MVP]

Nov 21 '05 #6
It was the "ClientConnected.WaitOne()" that caused you to block.

The ManualResetEvent is an object that lets a thread sleep until the "Set"
is called. The problem you hit is that you called WaitOne() from your UI
thread. You should have called that from a worker (listener) thread, like in
Tom's example.

Regards,
Tom C

"Terry Olsen" <to******@hotmail.com> wrote in message news:<On**************@TK2MSFTNGP15.phx.gbl>...
Speaking of the ManualResetEvent...what's it used for? I commented out all
the "ClientConnected.x" code and the listener still worked fine...without
blocking at all...

Nov 21 '05 #7
Can you tell me why I would need a ManualResetEvent at all (in this
particular situation)? I removed it and now everything seems to work fine.
"tcarvin" <NO***********@lycos.com> wrote in message
news:9f*************************@posting.google.co m...
It was the "ClientConnected.WaitOne()" that caused you to block.

The ManualResetEvent is an object that lets a thread sleep until the "Set"
is called. The problem you hit is that you called WaitOne() from your UI
thread. You should have called that from a worker (listener) thread, like
in
Tom's example.

Regards,
Tom C

"Terry Olsen" <to******@hotmail.com> wrote in message
news:<On**************@TK2MSFTNGP15.phx.gbl>...
Speaking of the ManualResetEvent...what's it used for? I commented out
all
the "ClientConnected.x" code and the listener still worked fine...without
blocking at all...

Nov 21 '05 #8
Terry,

In this particular situation (because you are using a callback and don't
need/want to "wait" for a connection, you do not need to use one.

(Be careful in your callback though, because it is called from a non-GUI
thread and you must use Invoke() to update GUI components),

Regards,
Tom C.

"Terry Olsen" <to******@hotmail.com> wrote in message news:<eh**************@TK2MSFTNGP15.phx.gbl>...
Can you tell me why I would need a ManualResetEvent at all (in this
particular situation)? I removed it and now everything seems to work fine.
"tcarvin" <NO***********@lycos.com> wrote in message
news:9f*************************@posting.google.co m...
It was the "ClientConnected.WaitOne()" that caused you to block.

The ManualResetEvent is an object that lets a thread sleep until the "Set"
is called. The problem you hit is that you called WaitOne() from your UI
thread. You should have called that from a worker (listener) thread, like
in
Tom's example.

Regards,
Tom C

"Terry Olsen" <to******@hotmail.com> wrote in message
news:<On**************@TK2MSFTNGP15.phx.gbl>...
Speaking of the ManualResetEvent...what's it used for? I commented out
all
the "ClientConnected.x" code and the listener still worked fine...without
blocking at all...

Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Morgan Leppink | last post by:
Hi all - We have been developing a complex TCP socket communication app that is responsible for keeping numerous connections open to clients on routable IPs. The app receives request on a...
3
by: Sagaert Johan | last post by:
Can someone point me to a good implementation of Async Sockets in c# ? So far i did not find a lot usefull info on this item. For the TcpListener, an example that supports multiple incoming...
8
by: MuZZy | last post by:
Hi, Could someone pls help me here: If i use async sockets in the separate thread like this: void ThreadFunction() { ..... 1. MySocket.BeginAccept(AsyncCallBack(OnConnectRequest),...
1
by: Chris Morse | last post by:
WARNING: Verbosity: skip to the very bottom paragraph for succinct version of my question.) Hi- I can't seem to find an answer to this. I am playing around with a variation of the ".NET...
6
by: gabriel.landais | last post by:
Hi, I'm currently building a parser class in JS and I have a question about variables. I retrieve XML data and then process it. After that, I process a result array (mydataarray). It looks like...
7
by: Roemer | last post by:
Hi all I stumbled over a new problem: I have a programm with just a class that is asynchronous listening for network connections. As soon as someone connected, a new form needs to be created....
6
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to...
5
by: Arno | last post by:
reposted with the right microsoft managed newsgroup ID: Sorry for the inconvinience Hi, I've written a class for client-socket connection, but I get a lot of times the error message "Unable...
4
by: Veeraraghavan | last post by:
Hi All, I am developing a client server communication using system.net.socket and I am finding it very difficult to get a solution for this. I started with single port communication with single...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.