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]problem regarding when the host is not listening and the client attempts to read data in tcpsocket tcpserver program.............
Forum Updated to NodeBB v4.3 + New Features

[solved]problem regarding when the host is not listening and the client attempts to read data in tcpsocket tcpserver program.............

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.5k 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.
  • P Offline
    P Offline
    preeth
    wrote on last edited by
    #1

    Hello everyone,
    Actually i am trying connect server and client tcpsocket and tcpserver class. In my program i have a refresh button, whenever the client clicks the button if the host is connected data will be recieved if not the connection should be closed. Receiving of data every time is works fine, but if the server is not ready and client clicks the refresh button everything will go wrong, there after no data will be received at the client end even if the client is sending the data, I tried closing tcpsocket connection if the server is not ready even after that i am getting the same problem, here is my code snippet please help
    @
    void MainWindow::refresh()
    {

    blockSize = 0;

    tcpSocket->connectToHost("ipaddress",52222);
    if(tcpSocket->state() == QTcpSocket::UnconnectedState)
    {
    tcpSocket->close();
    }
    }
    @

    when the tcpsocket is ready for reading
    i am calling a method where after reading the data i am closing the connection

    [edit: code tags added, koahnig]

    1 Reply Last reply
    0
    • P Offline
      P Offline
      preeth
      wrote on last edited by
      #2

      I have a doubt what is the difference between tcpSocket->disconnectFromHost(); and tcpSocket->close() and if i am closing the tcpsocket connection how to open it again?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        To my understanding you will try to close something what is not open. This makes no sense and is the source of your problem.
        AFAIK the "SocketState":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#SocketState-enum is UnConnected at the beginning. When you are connecting to a host the state will change following the sequence of the different states.
        "disconnectFromHost":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#disconnectFromHost states:
        [quote]
        Attempts to close the socket. If there is pending data waiting to be written, QAbstractSocket will enter ClosingState and wait until all data has been written. Eventually, it will enter UnconnectedState and emit the disconnected() signal.
        [/quote]
        connectToHost will open the connection again.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • P Offline
          P Offline
          preeth
          wrote on last edited by
          #4

          '@' koahnig,
          sir whether
          @
          connect(&socket,SIGNAL(readyRead()),this,SLOT(readData(QTcpSocket *)));
          @

          and

          @
          if(socket.isReadable())
          {
          readData(&socket);
          }
          @

          are same?

          [edit, code added, koahnig]

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            Please use "code wrapping":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 in your posts. Otherwise they are not readable.

            There is a fundamental difference between the two cases. A signal is typically connected to a slot and will be triggered when a signal has been emitted. "Checkout this description.":http://qt-project.org/doc/qt-4.8/signalsandslots.html The slot routine will be called only when the signal is triggered. This consumes only computer power when required.
            With the second you are checking if data is available, so read it. This will require some sort of "endless" loop consuming eventually all computation power.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • P Offline
              P Offline
              preeth
              wrote on last edited by
              #6

              Sir in my program i want to run my data transfer part in thread so i am using thread. my run function consist
              @void thread::run()
              {
              QTcpSocket socket;
              socket.----connectToHost( "127.0.0.1", 52222 );
              if(socket.isReadable())
              {
              readData(&socket)
              }
              }@
              actually this is not working, but i can't use @ connect(&socket,SIGNAL(readyRead()),this,SLOT(readData()));
              @ because socket is local variable, if i use socket as global variable and initialize it in constructor or any other function of thread class i will get this error
              @Cannot create children for a parent that is in a different thread.@
              Can we change or override the signature of readyread() so that i can pass the local socket object?

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                The question is if you really need to use a thread. If the only purpose is to make sure that the socket is receiving the data when it should this is not required. The event loop will ensure that you will receive a notification through the readyRead signal.

                If you really have to use QThread please have a look to "this wiki article.":http://qt-project.org/wiki/Threads_Events_QObjects

                Probably you should also have a look to the fortune examples, "fortune server":http://qt-project.org/doc/qt-4.8/network-fortuneserver.html and "Fortune client.":http://qt-project.org/doc/qt-4.8/network-fortuneclient.html

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  preeth
                  wrote on last edited by
                  #8

                  sir actually i want dynamically data should appear on the screen when client receives the data, i mean i don't want use any buttons , so thought of using thread, if this can be done in gui thread please suggest how to do it..

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    preeth
                    wrote on last edited by
                    #9

                    Thank you sir,
                    problem solved :) :)

                    1 Reply Last reply
                    0

                    • Login

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