Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]How to check socket connect to host
Forum Updated to NodeBB v4.3 + New Features

[Solved]How to check socket connect to host

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 11.8k 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.
  • A Offline
    A Offline
    anmol2701
    wrote on 19 Jun 2014, 11:54 last edited by
    #1

    Hi!

    After
    socket->connectToHost(server->text(), port->value());

    How can i find socket connect to host or not??

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 19 Jun 2014, 12:11 last edited by
      #2

      Hi,

      If the connection succeeds "connected":https://qt-project.org/doc/qt-5/qabstractsocket.html#connected signal is emitted.
      You can connect this signal to your slot.

      157

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DBoosalis
        wrote on 19 Jun 2014, 12:32 last edited by
        #3

        also can check by

        @if (socket->state() == QAbstractSocket::Connected)@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          anmol2701
          wrote on 19 Jun 2014, 12:33 last edited by
          #4

          Thanks for the reply..

          I am using
          if(socket->state() == QAbstractSocket::ConnectedState)
          to check but it is not working..

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DBoosalis
            wrote on 19 Jun 2014, 12:36 last edited by
            #5

            What is the state value returned ?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              anmol2701
              wrote on 19 Jun 2014, 13:03 last edited by
              #6

              if i use

              if(socket->state() == QAbstractSocket::ConnectedState || QAbstractSocket::ConnectingState)

              it always returns true

              1 Reply Last reply
              0
              • D Offline
                D Offline
                DBoosalis
                wrote on 19 Jun 2014, 13:40 last edited by
                #7

                Of course the statement below is always true:

                @if(socket->state() == QAbstractSocket::ConnectedState || QAbstractSocket::ConnectingState)
                @

                as QAbstractSocket::ConnectingState is certainly greater then zero.

                You need to change the statement to

                @if((socket->state() == QAbstractSocket::ConnectedState) ||
                (socket->state() == QAbstractSocket::ConnectingState))

                @

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  anmol2701
                  wrote on 20 Jun 2014, 05:27 last edited by
                  #8

                  Thanks all for the reply..

                  First of all i am checking socket is connected using

                    if (socket->state() == QAbstractSocket::UnconnectedState)
                  

                  it is working fine, then i add

                    socket->connectToHost(server->text(), port->value());
                  

                  after this i want to know is it conncet or not??

                  Moreover, I try to use this

                     if (socket->state() == QAbstractSocket::connectedState)
                  

                  But it is not working?

                  Could you please guide me?

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    p3c0
                    Moderators
                    wrote on 20 Jun 2014, 05:33 last edited by
                    #9

                    Did you try connecting to the "connected":https://qt-project.org/doc/qt-5/qabstractsocket.html#connected signal ?

                    157

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DBoosalis
                      wrote on 20 Jun 2014, 13:31 last edited by
                      #10

                      Look at the example in the documentation (fortune client)
                      "http://qt-project.org/doc/qt-5/qtnetwork-fortuneclient-example.html":http://qt-project.org/doc/qt-5/qtnetwork-fortuneclient-example.html.

                      If you still have trouble post your code

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        anmol2701
                        wrote on 25 Jun 2014, 05:41 last edited by
                        #11

                        Thanks!
                        It works..
                        I simply check it using condition
                        if(!socket->waitForConnected(1000))
                        {
                        //code
                        }

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          ankursaxena
                          wrote on 25 Jun 2014, 07:30 last edited by
                          #12

                          If your problem is solved then please prepend your title as <solved>.

                          1 Reply Last reply
                          0

                          1/12

                          19 Jun 2014, 11:54

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved