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. how to make sure Socket has connected?

how to make sure Socket has connected?

Scheduled Pinned Locked Moved C#
tutorialquestion
7 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.
  • G Offline
    G Offline
    Gavin Roberts
    wrote on last edited by
    #1

    Hi all, I would like to be able to correctly determine whether I have connected to my Host before trying to login. Everything I can think of trying has failed. If I try connecting to 192.168.0.1 110 it says its connected, but has 0 bytes Available. Which is great, and works for valid and invalid addresses whilst debugging, but as soon as I remove all breakpoints it comes back with errors on the valid addresses. I done a quick search, and found using a mixture of Poll and Available which unfortunately doesn't work either. Basically I don't want any code to continue until I have a Connected and Communicating Socket. Any idea's? Regards Gav

    L 1 Reply Last reply
    0
    • G Gavin Roberts

      Hi all, I would like to be able to correctly determine whether I have connected to my Host before trying to login. Everything I can think of trying has failed. If I try connecting to 192.168.0.1 110 it says its connected, but has 0 bytes Available. Which is great, and works for valid and invalid addresses whilst debugging, but as soon as I remove all breakpoints it comes back with errors on the valid addresses. I done a quick search, and found using a mixture of Poll and Available which unfortunately doesn't work either. Basically I don't want any code to continue until I have a Connected and Communicating Socket. Any idea's? Regards Gav

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Gavin Roberts wrote:

      it comes back with errors

      What errors? What "comes back with errors"?

      Gavin Roberts wrote:

      Basically I don't want any code to continue until I have a Connected and Communicating Socket.

      Then check for the error and if present don't continue

      G 1 Reply Last reply
      0
      • L led mike

        Gavin Roberts wrote:

        it comes back with errors

        What errors? What "comes back with errors"?

        Gavin Roberts wrote:

        Basically I don't want any code to continue until I have a Connected and Communicating Socket.

        Then check for the error and if present don't continue

        G Offline
        G Offline
        Gavin Roberts
        wrote on last edited by
        #3

        Sorry. Basically I use Socket.Available to check if any data is available before continuing which whilst stepping through with a debugger, works fine as soon as I run the code with no breakpoints Socket.Available returns 0. I am currently using Socket.Read to check but that is quite slow. Cheers for the reply. Gav

        L 1 Reply Last reply
        0
        • G Gavin Roberts

          Sorry. Basically I use Socket.Available to check if any data is available before continuing which whilst stepping through with a debugger, works fine as soon as I run the code with no breakpoints Socket.Available returns 0. I am currently using Socket.Read to check but that is quite slow. Cheers for the reply. Gav

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Gavin Roberts wrote:

          I am currently using Socket.Read to check but that is quite slow.

          Slow?? Read will read quit fast when the TCP/IP stack has received the data from the remote socket. If it is "slow" then the network connection or remote server must be slow. Under normal network and server conditions for a POP3 Server, you can do a blocking "Connect" and when that returns you can "Read" the "OK" message sent by the Server immediately. There should be no significant delay. If there is, then you have a network issue or the POP3 Server is slow.

          G 1 Reply Last reply
          0
          • L led mike

            Gavin Roberts wrote:

            I am currently using Socket.Read to check but that is quite slow.

            Slow?? Read will read quit fast when the TCP/IP stack has received the data from the remote socket. If it is "slow" then the network connection or remote server must be slow. Under normal network and server conditions for a POP3 Server, you can do a blocking "Connect" and when that returns you can "Read" the "OK" message sent by the Server immediately. There should be no significant delay. If there is, then you have a network issue or the POP3 Server is slow.

            G Offline
            G Offline
            Gavin Roberts
            wrote on last edited by
            #5

            I am using Socket.Connect to connect. At the moment, I am having to use Socket.Receive to determine whether the connection is valid or not. I was using Socket.Available, but this only seemed to work when I was stepping through each line using Visual Studio. As soon as I removed all breakpoints and allowed it to continue as normal, Socket.Available returned incorrect data, as if it hadn't enough time to populate. Socket.Receive is slow for a invalid Connection, and i'd prefere to speed this up. Thanks Gav

            P L 2 Replies Last reply
            0
            • G Gavin Roberts

              I am using Socket.Connect to connect. At the moment, I am having to use Socket.Receive to determine whether the connection is valid or not. I was using Socket.Available, but this only seemed to work when I was stepping through each line using Visual Studio. As soon as I removed all breakpoints and allowed it to continue as normal, Socket.Available returned incorrect data, as if it hadn't enough time to populate. Socket.Receive is slow for a invalid Connection, and i'd prefere to speed this up. Thanks Gav

              P Offline
              P Offline
              pbraun
              wrote on last edited by
              #6

              Gavin Roberts wrote:

              Socket.Receive is slow for a invalid Connection, and i'd prefere to speed this up.

              Socket.Receive is blocking which means that it will only return after it has received data or the connection has been closed. To check if the connection is valid you will have to look at the socket options available to see if any of those help. Also you should be checking the return status of Socket.Connect to see if it connected properly and if you are not catching any exceptions around the Socket.Connect, then you will be missing information that may lead to the answer you are seeking. An inference from the above is that the socket should be valid by the time the receive method is called. Phil

              1 Reply Last reply
              0
              • G Gavin Roberts

                I am using Socket.Connect to connect. At the moment, I am having to use Socket.Receive to determine whether the connection is valid or not. I was using Socket.Available, but this only seemed to work when I was stepping through each line using Visual Studio. As soon as I removed all breakpoints and allowed it to continue as normal, Socket.Available returned incorrect data, as if it hadn't enough time to populate. Socket.Receive is slow for a invalid Connection, and i'd prefere to speed this up. Thanks Gav

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                Gavin Roberts wrote:

                I am using Socket.Connect to connect.

                Ok but have you "read" the documentation[^] You are expecting certain behavior based on "what" if you haven't read the documentation? Just guessing?

                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