Server Socket Synchronization When Connecting?

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

    Server Socket Synchronization When Connecting?

    When sharing a Socket between threads, are the socket operations
    automatically synchronized to support multithreading?

    For example:

    Thread1 sets up a server socket to listen and invokes BeginAccept.

    A connection is made and Thread2 (within the BeginAccept delegate) begins.

    Does the server socket been to be "locked" before Thread2 calls
    EndAccept? If not, what prevents corruption when Thread1 from closing
    the server socket while the BeginAccept delegate (Thread2) is just about
    to call EndAccept?
  • Michael  Goldfarb

    #2
    Re: Server Socket Synchronization When Connecting?

    You need to call EndAccept before performing any other operations with
    your socket.

    Once you do that you can pass your socket through threads.

    On Oct 26, 12:47 pm, "O.B." <funkj...@bells outh.netwrote:
    When sharing a Socket between threads, are the socket operations
    automatically synchronized to support multithreading?
    >
    For example:
    >
    Thread1 sets up a server socket to listen and invokes BeginAccept.
    >
    A connection is made and Thread2 (within the BeginAccept delegate) begins.
    >
    Does the server socket been to be "locked" before Thread2 calls
    EndAccept? If not, what prevents corruption when Thread1 from closing
    the server socket while the BeginAccept delegate (Thread2) is just about
    to call EndAccept?

    Comment

    • O.B.

      #3
      Re: Server Socket Synchronization When Connecting?

      Hmm, maybe I'm not being clear enough. There are actually 3 threads.
      Here's some code. See my question in the AcceptConnectio n operation.

      The first thread starts a new thread using the AcceptConnectEv entLoop as
      a delegate.


      // Invoked as a new thread from the main thread.
      // This is the second thread.
      private void AcceptConnectEv entLoop() {
      IAsyncResult result = null;
      while (true) {
      if (socket != null && socket.IsBound) {
      result = socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),
      socket);
      } else {
      break;
      }
      // Wait for the EndAccept before issuing a new BeginAccept
      result.AsyncWai tHandle.WaitOne ();
      }
      }

      // This is the third thread; invoked when a client wants
      // to make a connection.
      private void AcceptConnectio n(IAsyncResult asyncResult) {
      Socket serverSocket = (Socket)asyncRe sult.AsyncState ;
      Socket clientSocket = null;
      try {
      // WHAT PREVENTS THE 1ST THREAD FROM CLOSING THE SOCKET
      // BEFORE THE FOLLOWING LINE EXECUTES???
      clientSocket = serverSocket.En dAccept(asyncRe sult);
      lock (connectedSocke ts) {
      connectedSocket s.Add(clientSoc ket);
      }
      } catch (SocketExceptio n) {
      return;
      } catch (Exception) {
      return;
      }
      }






      Michael Goldfarb wrote:
      You need to call EndAccept before performing any other operations with
      your socket.
      >
      Once you do that you can pass your socket through threads.
      >
      On Oct 26, 12:47 pm, "O.B." <funkj...@bells outh.netwrote:
      >When sharing a Socket between threads, are the socket operations
      >automaticall y synchronized to support multithreading?
      >>
      >For example:
      >>
      >Thread1 sets up a server socket to listen and invokes BeginAccept.
      >>
      >A connection is made and Thread2 (within the BeginAccept delegate) begins.
      >>
      >Does the server socket been to be "locked" before Thread2 calls
      >EndAccept? If not, what prevents corruption when Thread1 from closing
      >the server socket while the BeginAccept delegate (Thread2) is just about
      >to call EndAccept?
      >

      Comment

      • William Stacey [C# MVP]

        #4
        Re: Server Socket Synchronization When Connecting?

        Nothing stops thread 1 from calling Stop() on the listener at any time -
        other then good design.

        --
        William Stacey [C# MVP]

        "O.B." <funkjunk@bells outh.netwrote in message
        news:45410CBD.9 080402@bellsout h.net...
        | Hmm, maybe I'm not being clear enough. There are actually 3 threads.
        | Here's some code. See my question in the AcceptConnectio n operation.
        |
        | The first thread starts a new thread using the AcceptConnectEv entLoop as
        | a delegate.
        |
        |
        | // Invoked as a new thread from the main thread.
        | // This is the second thread.
        | private void AcceptConnectEv entLoop() {
        | IAsyncResult result = null;
        | while (true) {
        | if (socket != null && socket.IsBound) {
        | result = socket.BeginAcc ept(new AsyncCallback(A cceptConnection ),
        | socket);
        | } else {
        | break;
        | }
        | // Wait for the EndAccept before issuing a new BeginAccept
        | result.AsyncWai tHandle.WaitOne ();
        | }
        | }
        |
        | // This is the third thread; invoked when a client wants
        | // to make a connection.
        | private void AcceptConnectio n(IAsyncResult asyncResult) {
        | Socket serverSocket = (Socket)asyncRe sult.AsyncState ;
        | Socket clientSocket = null;
        | try {
        | // WHAT PREVENTS THE 1ST THREAD FROM CLOSING THE SOCKET
        | // BEFORE THE FOLLOWING LINE EXECUTES???
        | clientSocket = serverSocket.En dAccept(asyncRe sult);
        | lock (connectedSocke ts) {
        | connectedSocket s.Add(clientSoc ket);
        | }
        | } catch (SocketExceptio n) {
        | return;
        | } catch (Exception) {
        | return;
        | }
        | }
        |
        |
        |
        |
        |
        |
        | Michael Goldfarb wrote:
        | You need to call EndAccept before performing any other operations with
        | your socket.
        | >
        | Once you do that you can pass your socket through threads.
        | >
        | On Oct 26, 12:47 pm, "O.B." <funkj...@bells outh.netwrote:
        | >When sharing a Socket between threads, are the socket operations
        | >automaticall y synchronized to support multithreading?
        | >>
        | >For example:
        | >>
        | >Thread1 sets up a server socket to listen and invokes BeginAccept.
        | >>
        | >A connection is made and Thread2 (within the BeginAccept delegate)
        begins.
        | >>
        | >Does the server socket been to be "locked" before Thread2 calls
        | >EndAccept? If not, what prevents corruption when Thread1 from closing
        | >the server socket while the BeginAccept delegate (Thread2) is just
        about
        | >to call EndAccept?
        | >


        Comment

        Working...