Re: How to do Async TCP Listener?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Michael D. Ober (obermd._at_.alum.mit.edu.nospam)
Date: 11/21/04


Date: Sat, 20 Nov 2004 21:32:22 -0700

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" <tolsen64@hotmail.com> wrote in message
news:O3agqD4zEHA.3336@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
>
>



Relevant Pages

  • Having a problem with some socket code
    ... Anyway I have it working with threads, as each client connects the socket ... data method that calls recieve directly to get the size of the data in the ... This all works great if I don't use the Poll method to prevent the blocking ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: My server sometimes goes deaf to certain hosts
    ... why does the client get connection refused, ... half-open connections exceeds the allowed backlog (which may not have ... closed the listening port. ... Maybe your server accidentally closed the socket it was listening on? ...
    (comp.os.linux.development.apps)
  • Re: Sockets / erste Schritte
    ... > Client analog zu einem CodeGuru Beispiel geschrieben. ... Die Listener Applikation erstellt einen listening CSocket. ... Anschließend wird die ReceiveMethode des listening Sockts ... > jeweils zwei Sockets eine ...
    (microsoft.public.de.vc)
  • Re: Interrupting a socket operation from another thread
    ... Using blocking connect, send, recv calls. ... Are you using a dedicated thread per client? ... close the socket handle but this gives a race condition (on unix ... If you want to shutdown the connection the socket refers to, ...
    (comp.programming.threads)
  • Re: Bad ServerSocket ! Listen! Listen! Listen!
    ... The code is blocking because a) you don't have the thread properly ... > the returned Socket... ... > ServiceManager is a class that extends Thread. ... > the serv_sock doesn't go back to listening... ...
    (comp.lang.java.programmer)