Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Asynchronous TcpListener and TcpClient design patterns

Asynchronous TcpListener and TcpClient design patterns

Scheduled Pinned Locked Moved C#
designsysadminquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    Are there asynchronous code designs for a server and a client using TcpListener and TcpClient including asynchronous data read/write?

    Чесноков

    R L B 3 Replies Last reply
    0
    • C Chesnokov Yuriy

      Are there asynchronous code designs for a server and a client using TcpListener and TcpClient including asynchronous data read/write?

      Чесноков

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      If you don't mind using WCF, you can get the async for free (along with several other benefits). /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      C 1 Reply Last reply
      0
      • R Ravi Bhavnani

        If you don't mind using WCF, you can get the async for free (along with several other benefits). /ravi

        My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

        C Offline
        C Offline
        Chesnokov Yuriy
        wrote on last edited by
        #3

        No, I need to use TcpListener and TcpClient in .NET assembly for windows forms. I'm intrested in correct usage of begin accept, read, write for server and begin read, write for client. Also correct server shutdown, socket exception handling etc...

        Чесноков

        1 Reply Last reply
        0
        • C Chesnokov Yuriy

          Are there asynchronous code designs for a server and a client using TcpListener and TcpClient including asynchronous data read/write?

          Чесноков

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          There's always MSDN[^] of course :)

          Bastard Programmer from Hell :suss:

          C 1 Reply Last reply
          0
          • L Lost User

            There's always MSDN[^] of course :)

            Bastard Programmer from Hell :suss:

            C Offline
            C Offline
            Chesnokov Yuriy
            wrote on last edited by
            #5

            Documentation encourages to use TcpListener and TcpCllient classes. I've seen those samples with a server getting back data from a client, sending back response and closing it. I'm intrested in a server handing many clients connected and client connected to many servers architecture. There should be some ready described solutions to avoid common mistakes. e.g. in BeginRead callback there is a missing zero byte call back which occurs if client connection is lost

            Чесноков

            R L 2 Replies Last reply
            0
            • C Chesnokov Yuriy

              Documentation encourages to use TcpListener and TcpCllient classes. I've seen those samples with a server getting back data from a client, sending back response and closing it. I'm intrested in a server handing many clients connected and client connected to many servers architecture. There should be some ready described solutions to avoid common mistakes. e.g. in BeginRead callback there is a missing zero byte call back which occurs if client connection is lost

              Чесноков

              R Offline
              R Offline
              Ravi Bhavnani
              wrote on last edited by
              #6

              Wow, this seems to be a good candidate for WCF!  The WCF service doesn't need to be hosted in a server app - you can use it much like you'd use TcpListener and TcpClient. /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

              C 1 Reply Last reply
              0
              • C Chesnokov Yuriy

                Documentation encourages to use TcpListener and TcpCllient classes. I've seen those samples with a server getting back data from a client, sending back response and closing it. I'm intrested in a server handing many clients connected and client connected to many servers architecture. There should be some ready described solutions to avoid common mistakes. e.g. in BeginRead callback there is a missing zero byte call back which occurs if client connection is lost

                Чесноков

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Chesnokov Yuriy wrote:

                I've seen those samples with a server getting back data from a client, sending back response and closing it.
                I'm intrested in a server handing many clients connected and client connected to many servers architecture.

                There is no multiple/multiple architecture, it's always one server/many clients. The fact that your browser can open multiple pages is due to having multiple clients, that each request data from a different server.

                Chesnokov Yuriy wrote:

                There should be some ready described solutions to avoid common mistakes.

                The All-in-one samples refer to the same page[^].

                Bastard Programmer from Hell :suss:

                1 Reply Last reply
                0
                • R Ravi Bhavnani

                  Wow, this seems to be a good candidate for WCF!  The WCF service doesn't need to be hosted in a server app - you can use it much like you'd use TcpListener and TcpClient. /ravi

                  My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                  C Offline
                  C Offline
                  Chesnokov Yuriy
                  wrote on last edited by
                  #8

                  Well I'm developing according to domain-driven design DDD and was going to write a service for server and client. What is the advanatage of WCF over ordinary .NET sockets apprach? I need several services running on the same machine each with a different port.

                  Чесноков

                  R 1 Reply Last reply
                  0
                  • C Chesnokov Yuriy

                    Well I'm developing according to domain-driven design DDD and was going to write a service for server and client. What is the advanatage of WCF over ordinary .NET sockets apprach? I need several services running on the same machine each with a different port.

                    Чесноков

                    R Offline
                    R Offline
                    Ravi Bhavnani
                    wrote on last edited by
                    #9

                    WCF provides the plumbing for easily:

                    • building a configuration (port, transport, security) driven client/server system
                    • implementing both sync and async client APIs
                    • adding hooks (e.g. custom compression) before data is sent/received
                    • implementing logging and instrumenting traffic
                    • implementing handshaking (full-duplex) communication and transacted communication
                    • embedding the server in any type of host host (GUI app, console app, web app, Windows service)

                    If you haven't had the opportunity to use it, I recommend giving it a try.  I think you'll be pleasantly surprised by its power and ease of use!  Here are a couple of links that should get you up and running quickly:

                    • WCF Introduction - Part1[^] (CP) <-- recommended
                    • WCF Getting Started[^] (MSDN)

                    /ravi

                    My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                    C 1 Reply Last reply
                    0
                    • R Ravi Bhavnani

                      WCF provides the plumbing for easily:

                      • building a configuration (port, transport, security) driven client/server system
                      • implementing both sync and async client APIs
                      • adding hooks (e.g. custom compression) before data is sent/received
                      • implementing logging and instrumenting traffic
                      • implementing handshaking (full-duplex) communication and transacted communication
                      • embedding the server in any type of host host (GUI app, console app, web app, Windows service)

                      If you haven't had the opportunity to use it, I recommend giving it a try.  I think you'll be pleasantly surprised by its power and ease of use!  Here are a couple of links that should get you up and running quickly:

                      • WCF Introduction - Part1[^] (CP) <-- recommended
                      • WCF Getting Started[^] (MSDN)

                      /ravi

                      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                      C Offline
                      C Offline
                      Chesnokov Yuriy
                      wrote on last edited by
                      #10

                      Thanks, but I'm not sure that is suitable for my design. I'm coding video surveillance application, server runs video capture and sends captured frames to connected clients. There can be any number of clients connected to a server and a client can be connected to any number of servers. There are video capture service and video data transfer service (the server). As the frame is captured video capture service invokes send method from the video data transfer service which sends the frame to all clients connected. WCF as I understood provides service contract and public methods to call. In that case I need to add WCF logic to video capture service and declare public method as e.g. GetRecentFrame(). And a client needs to call that method. I do not want to combine video capture and video data transfer services. As video capture service is running on despite the fact if someone is connected to transfer it a frame. And network connection may be slow, video capture service may get 10 frames in a time 1 frame is finished with the transfer to the client.

                      Чесноков

                      R 1 Reply Last reply
                      0
                      • C Chesnokov Yuriy

                        Thanks, but I'm not sure that is suitable for my design. I'm coding video surveillance application, server runs video capture and sends captured frames to connected clients. There can be any number of clients connected to a server and a client can be connected to any number of servers. There are video capture service and video data transfer service (the server). As the frame is captured video capture service invokes send method from the video data transfer service which sends the frame to all clients connected. WCF as I understood provides service contract and public methods to call. In that case I need to add WCF logic to video capture service and declare public method as e.g. GetRecentFrame(). And a client needs to call that method. I do not want to combine video capture and video data transfer services. As video capture service is running on despite the fact if someone is connected to transfer it a frame. And network connection may be slow, video capture service may get 10 frames in a time 1 frame is finished with the transfer to the client.

                        Чесноков

                        R Offline
                        R Offline
                        Ravi Bhavnani
                        wrote on last edited by
                        #11

                        Ah.  I now understand why you prefer to be lower in the network stack. /ravi

                        My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                        1 Reply Last reply
                        0
                        • C Chesnokov Yuriy

                          Are there asynchronous code designs for a server and a client using TcpListener and TcpClient including asynchronous data read/write?

                          Чесноков

                          B Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #12

                          I don't know if it counts as a design pattern but my sockets library[^] may be of some use to you (either to use it or to look at the code to adapt for your own solution). I don't use asynchronous writing (writing is de facto asynchronous, as it writes to the local buffer immediately, so you only need to use the methods if you want feedback), but I do use asynchronous accept and read and handle the various ways in which it can go wrong unexpectedly.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups