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. Managed C++/CLI
  4. TcpListener.AcceptTcpClient query

TcpListener.AcceptTcpClient query

Scheduled Pinned Locked Moved Managed C++/CLI
questionhtmldatabasecomhosting
3 Posts 3 Posters 9 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.
  • N Offline
    N Offline
    Nish Nishant
    wrote on last edited by
    #1

    TcpListener.AcceptTcpClient is a blocking call. How do I elegantly stop the call? From another thread say I want to stop listening. If I call TcpListener.Stop on the TcpListener object I get an exception. Some funny exception about how I halted a blocking operation. Right now I am putting an empty try{}...catch{} block and evading this exception. But there must be a more elegant solution! Any help is hugely appreciated! Warm regards Nish


    Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

    J 1 Reply Last reply
    0
    • N Nish Nishant

      TcpListener.AcceptTcpClient is a blocking call. How do I elegantly stop the call? From another thread say I want to stop listening. If I call TcpListener.Stop on the TcpListener object I get an exception. Some funny exception about how I halted a blocking operation. Right now I am putting an empty try{}...catch{} block and evading this exception. But there must be a more elegant solution! Any help is hugely appreciated! Warm regards Nish


      Author of the romantic comedy Summer Love and Some more Cricket [New Win] Review by Shog9 Click here for review[NW]

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      Untested; but give this a shot. Instead of calling AcceptTcpClient() from the start; make calls to Pending() instead, and when it returns true THEN make the call to AcceptTcpClient. Since that probably sounds convoluted, here's some code to illustrate it :)

      TcpListener tcpL = ....;

      tcpL.Start();

      while(bShouldAcceptConnections)
      {
      if( tcpL.Pending() )
      {
      TcpClient tcpC = tcpL.AcceptTcpClient();
      // do something with it
      }

      // Other processing that will eventually set
      // bShouldAcceptConnections to false
      }

      tcpL.Stop();

      HTH, James "Java is free - and worth every penny." - Christian Graus

      A 1 Reply Last reply
      0
      • J James T Johnson

        Untested; but give this a shot. Instead of calling AcceptTcpClient() from the start; make calls to Pending() instead, and when it returns true THEN make the call to AcceptTcpClient. Since that probably sounds convoluted, here's some code to illustrate it :)

        TcpListener tcpL = ....;

        tcpL.Start();

        while(bShouldAcceptConnections)
        {
        if( tcpL.Pending() )
        {
        TcpClient tcpC = tcpL.AcceptTcpClient();
        // do something with it
        }

        // Other processing that will eventually set
        // bShouldAcceptConnections to false
        }

        tcpL.Stop();

        HTH, James "Java is free - and worth every penny." - Christian Graus

        A Offline
        A Offline
        Andres Manggini
        wrote on last edited by
        #3

        Yes, I have an application using pretty much the same logic and it works ok. Andres Manggini. Buenos Aires - Argentina.

        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