Socket and multi-threading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mahesh Devjibhai Dhola [MVP]

    Socket and multi-threading

    Hi,

    Socket class documentation says that it is not thread safe. We understand
    that if do simultaneous sends on ONE socket then it will be a problem (or
    simultaneous receive). Can we create TWO threads on OUR OWN such that one
    will do SEND and one will do RECEIVE using the SAME socket reference? This
    means that two threads WILL BE in the same SOCKET object - one doing receive
    and one doing send. We know we can use asynchronous calls on the socket to
    achieve this but whether we can do this using our own threads. Any pointers
    to official documentation that shows that this is permissible?

    Thanks in advance,
    Regards,
    Mahesh






  • Cor Ligthert

    #2
    Re: Socket and multi-threading

    Mahesh,

    I know that this subject (and like this) is often been in the newsgroups

    Maybe you find something in these threads before somebody else answers you.


    I hope this helps,

    Cor


    Comment

    • Vijaye Raji

      #3
      Re: Socket and multi-threading

      [Removing cross-posts]

      TCP/IP sockets are full duplex - which means you can send and receive at the
      same time.

      -vj

      "Mahesh Devjibhai Dhola [MVP]" <dholamahesh@ho tmail.com> wrote in message
      news:ee9sq5FVFH A.3544@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi,
      >
      > Socket class documentation says that it is not thread safe. We understand
      > that if do simultaneous sends on ONE socket then it will be a problem (or
      > simultaneous receive). Can we create TWO threads on OUR OWN such that one
      > will do SEND and one will do RECEIVE using the SAME socket reference? This
      > means that two threads WILL BE in the same SOCKET object - one doing
      > receive
      > and one doing send. We know we can use asynchronous calls on the socket to
      > achieve this but whether we can do this using our own threads. Any
      > pointers
      > to official documentation that shows that this is permissible?
      >
      > Thanks in advance,
      > Regards,
      > Mahesh
      >
      >
      >
      >
      >
      >[/color]


      Comment

      • Supra

        #4
        Re: Socket and multi-threading



        Cor Ligthert wrote:
        [color=blue]
        >Mahesh,
        >
        >I know that this subject (and like this) is often been in the newsgroups
        >
        >Maybe you find something in these threads before somebody else answers you.
        >http://groups-beta.google.com/group/...rch+this+group
        >
        >I hope this helps,
        >
        >Cor
        >
        >
        >
        >[/color]

        Comment

        • Ioannis Vranos

          #5
          Re: Socket and multi-threading

          Mahesh Devjibhai Dhola [MVP] wrote:
          [color=blue]
          > Hi,
          >
          > Socket class documentation says that it is not thread safe. We understand
          > that if do simultaneous sends on ONE socket then it will be a problem (or
          > simultaneous receive). Can we create TWO threads on OUR OWN such that one
          > will do SEND and one will do RECEIVE using the SAME socket reference? This
          > means that two threads WILL BE in the same SOCKET object - one doing receive
          > and one doing send. We know we can use asynchronous calls on the socket to
          > achieve this but whether we can do this using our own threads. Any pointers
          > to official documentation that shows that this is permissible?[/color]


          If you perform synchronized use of the socket by using thread locks (that is perform a
          sending operation with one thread, after the other thread has finished reading), then I
          suppose it is OK. However why are you using the Socket class itself for this?


          In a book I am currently reading about .NET networking, stream socket communication is
          performed in the style:

          (get the connection Socket)
          get a NetworkStream/create a NetworkStream with the Socket object
          create a BinaryWriter and a BinaryReader with the NetworkStream

          use the BinaryReader and BinaryWriter for network I/O

          Call methods Close() of BinaryReader, BinaryWriter, NetworkStream, (Socket) in turn.

          Comment

          • Arthur M.

            #6
            RE: Socket and multi-threading

            In your example the two threads will be executing different functions on the
            same socket instance; they wlll use two different buffers (send and receive),
            they will never block each other, hence you can use it.

            As a side note, creating 2 threads to manage a socket is a sure way to kill
            your application performance when n context of reasonably high number of
            users. I'd recommend looking into async IO. IOCP is far more efficient way of
            dealing with a problem.


            "Mahesh Devjibhai Dhola [MVP]" wrote:
            [color=blue]
            > Hi,
            >
            > Socket class documentation says that it is not thread safe. We understand
            > that if do simultaneous sends on ONE socket then it will be a problem (or
            > simultaneous receive). Can we create TWO threads on OUR OWN such that one
            > will do SEND and one will do RECEIVE using the SAME socket reference? This
            > means that two threads WILL BE in the same SOCKET object - one doing receive
            > and one doing send. We know we can use asynchronous calls on the socket to
            > achieve this but whether we can do this using our own threads. Any pointers
            > to official documentation that shows that this is permissible?
            >
            > Thanks in advance,
            > Regards,
            > Mahesh
            >
            >
            >
            >
            >
            >
            >[/color]

            Comment

            • Dennis

              #7
              RE: Socket and multi-threading

              You should be able to use a Monitor to lock one thread (Monitor.Enter( Me) and
              Monitor.Wait(Me )) to ensure the second thread is finished (use
              Monitor.Enter(M e) and Monitor.Pulse(M e) and Monitor.Exit(Me ) in the second
              class.

              "Mahesh Devjibhai Dhola [MVP]" wrote:
              [color=blue]
              > Hi,
              >
              > Socket class documentation says that it is not thread safe. We understand
              > that if do simultaneous sends on ONE socket then it will be a problem (or
              > simultaneous receive). Can we create TWO threads on OUR OWN such that one
              > will do SEND and one will do RECEIVE using the SAME socket reference? This
              > means that two threads WILL BE in the same SOCKET object - one doing receive
              > and one doing send. We know we can use asynchronous calls on the socket to
              > achieve this but whether we can do this using our own threads. Any pointers
              > to official documentation that shows that this is permissible?
              >
              > Thanks in advance,
              > Regards,
              > Mahesh
              >
              >
              >
              >
              >
              >
              >[/color]

              Comment

              Working...