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. TcpListener.AcceptTcpClient OR TimeOut

TcpListener.AcceptTcpClient OR TimeOut

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 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.
  • T Offline
    T Offline
    Trapper Hell
    wrote on last edited by
    #1

    It seems that I'm stuck on something small but I've been working on it for quite some time and I'm exhausted... I have a method that returns a bool whether a TcpConnection was successful or not... The method attempts TcpListener.AcceptTcpClient however I want it to return false if no client has been connected within a specified time (e.g. 2000 milliseconds)... How can I achieve this? I attempted the .BeginAcceptTcpClient and .EndAcceptTcpClient but these still block OR if I use the Thread.Sleep() I block the method from accepting any connecting client.. I need the result to be returned if a TcpClient is accepted or the operation 'timed-out' - whichever comes first. Please help!

    J 1 Reply Last reply
    0
    • T Trapper Hell

      It seems that I'm stuck on something small but I've been working on it for quite some time and I'm exhausted... I have a method that returns a bool whether a TcpConnection was successful or not... The method attempts TcpListener.AcceptTcpClient however I want it to return false if no client has been connected within a specified time (e.g. 2000 milliseconds)... How can I achieve this? I attempted the .BeginAcceptTcpClient and .EndAcceptTcpClient but these still block OR if I use the Thread.Sleep() I block the method from accepting any connecting client.. I need the result to be returned if a TcpClient is accepted or the operation 'timed-out' - whichever comes first. Please help!

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      something like this?

      [pseudo C#]
      private bool GetNext(out TcpClient client)
      {
      client = null;

      while ((less than 2000 ms has elapsed) and (!TcpListener.Pending())
      {
          Sleep a bit
      }
      
      if (TcpListener.Pending())
      {
          client = TcpListener.AcceptTcpClient();
          return true;
      } 
      
      return false;
      

      }

      :badger:

      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