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. TcpClient - waiting for connection

TcpClient - waiting for connection

Scheduled Pinned Locked Moved C#
question
6 Posts 3 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.
  • Q Offline
    Q Offline
    quilkin
    wrote on last edited by
    #1

    I am using TcpClient.Connect(ipaddress, portnumber) but the app must 'scan' a few possible IPs before finding the 'active' one. However if 'ipaddress' does not respond, the Connect() function hangs for nearly 60 seconds before raising an exception. Is there any way I can test for an IP quickly (e.g. a 'ping() command) before calling 'Connect()? I don't really want to get into starting separate threads for each possible IP address to search.

    J S 2 Replies Last reply
    0
    • Q quilkin

      I am using TcpClient.Connect(ipaddress, portnumber) but the app must 'scan' a few possible IPs before finding the 'active' one. However if 'ipaddress' does not respond, the Connect() function hangs for nearly 60 seconds before raising an exception. Is there any way I can test for an IP quickly (e.g. a 'ping() command) before calling 'Connect()? I don't really want to get into starting separate threads for each possible IP address to search.

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

      I don't have access to the documentation right now but I thought you could set a time out on the underlying socket via TcpClient? -Jeff

      Q 1 Reply Last reply
      0
      • J jahawkins

        I don't have access to the documentation right now but I thought you could set a time out on the underlying socket via TcpClient? -Jeff

        Q Offline
        Q Offline
        quilkin
        wrote on last edited by
        #3

        Jeff, I have set both SendTimeout and ReceiveTimeout (to 6000 ms) but they have no effect on starting the connection, only presumably on send/receive data.

        1 Reply Last reply
        0
        • Q quilkin

          I am using TcpClient.Connect(ipaddress, portnumber) but the app must 'scan' a few possible IPs before finding the 'active' one. However if 'ipaddress' does not respond, the Connect() function hangs for nearly 60 seconds before raising an exception. Is there any way I can test for an IP quickly (e.g. a 'ping() command) before calling 'Connect()? I don't really want to get into starting separate threads for each possible IP address to search.

          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          Try this code snippet.

          try
          {
          System.Net.Dns.GetHostByAddress(this.serverAddress);
          }
          catch (System.Net.Sockets.SocketException)
          {// IP-address isn't available.
          }


          www.troschuetz.de

          Q 1 Reply Last reply
          0
          • S Stefan Troschuetz

            Try this code snippet.

            try
            {
            System.Net.Dns.GetHostByAddress(this.serverAddress);
            }
            catch (System.Net.Sockets.SocketException)
            {// IP-address isn't available.
            }


            www.troschuetz.de

            Q Offline
            Q Offline
            quilkin
            wrote on last edited by
            #5

            Stefan, Thanks but I think this will only be OK if there is a domain name server to provide a name. This system needs to work on LANs where there may not be one. I guess I'll need to use the asynch StartConnect() method but I need a decent sample to see how to use it.

            S 1 Reply Last reply
            0
            • Q quilkin

              Stefan, Thanks but I think this will only be OK if there is a domain name server to provide a name. This system needs to work on LANs where there may not be one. I guess I'll need to use the asynch StartConnect() method but I need a decent sample to see how to use it.

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              I don't think so. The MSDDN doesn't state that you need the presence of a real DNS in your Network and i successfully used this method in my local LAN to determine whether a PC can be reached or not.


              www.troschuetz.de

              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