Asynchronous Sockets and Threading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darren

    #1

    Asynchronous Sockets and Threading

    Dear all,

    I have written a TCP server as a windows service that accepts connections
    using the System.Net.Sock ets.Socket class. I use the various asynchronous
    methods in the socket class. I'm having a problem at the moment when I shut
    the service down. When I stop the service I attempt to shutdown and close
    the socket. But when the service is started again and the Socket.Bind
    statement is encountered, an error occurs telling me that it is already in
    use? How can this be if I have shutdown and closed the socket?

    Further more can anyone explain what happens when an Asynchronous method is
    called. Is a new thread allocated for the callback? If the method has been
    called before in the socket do you get the same thread? I have stepped
    through the code using the debugger and I don't understand where the threads
    have been created.

    Any information on these things would be gratefully received!

    Darren

    Example Code:

    The windows service calls the StartListener and StopListener methods

    public void StartListener()
    {
    Socket ConnectionListe ner = new Socket( AddressFamily.I nterNetwork,
    SocketType.Stre am, ProtocolType.Tc p );
    ConnectionListe ner.Bind( new IPEndPoint( HostAddress, PortNumber ) );
    ConnectionListe ner.Listen( 10 );
    while (!ShuttingDown)
    {
    Listen.Reset();
    ConnectionListe ner.BeginAccept ( new AsyncCallback(
    ConnectionReque st ), ConnectionListe ner );
    Listen.WaitOne( );
    }
    }

    /// <summary>
    /// Stop listening for clients
    /// </summary>
    public void StopListener()
    {
    ShuttingDown = true;
    Socket ConnectionListe ner = new Socket( AddressFamily.I nterNetwork,
    SocketType.Stre am, ProtocolType.Tc p );
    ConnectionListe ner.Connect( new IPEndPoint( HostAddress, PortNumber ) );
    ConnectionListe ner.Shutdown( SocketShutdown. Both );
    ConnectionListe ner.Close();
    ConnectionListe ner = null;
    }

    private void ConnectionReque st( IAsyncResult ar )
    {
    Socket Listener = (Socket) ar.AsyncState;

    Socket WorkingSocket = Listener.EndAcc ept( ar );
    if ( ConnectedClient s.Count < MaximumClients )
    AcceptClient( WorkingSocket );
    else
    RefuseClient( WorkingSocket );

    Listen.Set();
    }


    private void AcceptClient( Socket WorkingSocket )
    {
    Client Client = new Client();
    Client.Connecte d += new Client.ConnectH andler( AddClient );
    Client.Disconne cted += new Client.Disconne ctHandler( RemoveClient );
    Client.WorkingS ocket = WorkingSocket;
    }

    private void RefuseClient( Socket WorkingSocket )
    {
    WorkingSocket.S end( Encoding.ASCII. GetBytes("To many Clients") );
    WorkingSocket.S hutdown( SocketShutdown. Both );
    WorkingSocket.C lose();
    }


  • Arthur Nesterovsky

    #2
    Re: Asynchronous Sockets and Threading

    Hi,
    [color=blue]
    > Further more can anyone explain what happens when an Asynchronous method[/color]
    is[color=blue]
    > called. Is a new thread allocated for the callback? If the method has[/color]
    been[color=blue]
    > called before in the socket do you get the same thread? I have stepped
    > through the code using the debugger and I don't understand where the[/color]
    threads[color=blue]
    > have been created.[/color]

    All asynchronous calls get the threads from ThreadPool. So, there is
    probability
    that two different calls use the same thread.

    Take a look at the following article and try to search another articles
    about
    threading and asynchronous call in MSDN:

    http://msdn.microsoft.com/msdnmag/is...t/default.aspx

    --
    _______________ _______________
    With best wishes, Arthur Nesterovsky
    Please visit my home page:




    Comment

    Working...