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. Two-way communication with QTcpSocket
QtWS25 Last Chance

Two-way communication with QTcpSocket

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 5.3k Views
  • 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.
  • Z Offline
    Z Offline
    Zingam
    wrote on last edited by
    #1

    First of all I have 0 experience with network programming.

    Task: To create a client program that communicates to a device.

    1. Sends 4 bytes over a local network to set parameters on the device or to request data from the device. byte 0 - command, byte 1 - sensorId, byte 2 and byte 3 - data.
    2. The client queries the device every two seconds with a "GET" command (byte 0).
    3. The user can set/(send to) data on the device with a "SET" command (byte 0).

    For me to test this scenario, I followed the documentation and the threaded fortune server example and client. I made a server and a client. They are not quite finished yet.
    Unfortunately most of the "packets" are not received by the client (I haven't tested setting data on the server yet).

    My actual question is: what would be a proper architecture and implementation of my scenario?

    I am not posting any code because I don't think it would help.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Which is your device ? Does it have network support ? Is the TCP Socket EndPoint created on Device ?

      In general you can use the socket programming for your task. You need to define the message exchanges between your client application and device application.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        Zingam
        wrote on last edited by
        #3

        The device is not yet available. That's why I am trying to make an emulation. I have created a server with QTcpServer (that would be the device) and and the client code. Also this is a study project before I apply my acquired knowledge to the real thing.
        I used as a base the Threaded Fortune Server example and the Fortune Client. Since the client queries the server for data every two seconds, it doesn't receive an answer to all the queries.
        I cannot find anything similar on this topic made with Qt.
        I need to use Tcp Sockets.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          First and foremost

          1. First you write Client and Server
          2. Just pass "Hello" between client and server.
          3. Ensure that data is received by both perspective.

          Once you ready with basic communication ready, start implementing the client and server protocol what you wanted.

          If you don't find simple program, do let me know. I can send you the very simple program.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #5

            As i understand your primary goal is client side app? In network applications hard part is server side(usually).
            From your description, server side never initiate data sending, right? For your case fortune server should be ok. Try test fortune server and client without changing their code. Does client receive all data from server?

            For alternative, take a look to Bryan's tutorial and code:
            "C++ Qt 65 - QTcpSocket basics":http://voidrealms.com/index.php?r=tutorial/view&id=251
            "C++ Qt 66 - QTCPSocket using signals and slots":http://voidrealms.com/index.php?r=tutorial/view&id=252
            "C++ Qt 67 - QTCPServer - a basic TCP server application":http://voidrealms.com/index.php?r=tutorial/view&id=253
            "C++ Qt 68 - QTcpServer using multiple threads":http://voidrealms.com/index.php?r=tutorial/view&id=254
            "C++ Qt 69 - QTcpServer using QThreadPool":http://voidrealms.com/index.php?r=tutorial/view&id=255
            "C++ Qt 70 Advanced Asynchronous QTcpServer with QThreadPool":http://voidrealms.com/index.php?r=tutorial/view&id=256

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              Zingam
              wrote on last edited by
              #6

              Thanx for the videos. I watched these.

              I guess I didn't explained myself well enough.

              What I currently have is a threaded server modeled after the Threaded Fortune example - it uses TcpServer. And I have a Client. I even have a GUI for both.

              The client queries the server every couple of seconds for data - temperatureCurrent or sends a manual request to set data on the server - temperatureTarget.

              The problem is: the client does not receive every answer by the server. In fact it misses most of them.
              I'll try to use a QThreadPool next to see if the server will work better.

              Generally the client should look for the server/device, connect to it and start requesting data. What I don't know is if I should open a socket and keep it open all the time (and if that's possible at all) or if I need to open a socket everytime when I need data. I'm completely lost here :)

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                good. What are you asking is understanding about network programming. I request you to write one simple "Hello World" data transfer between client and server. You will have answer for many questions you are asking here. Also questions fall into network programming domain.

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                0
                • IamSumitI Offline
                  IamSumitI Offline
                  IamSumit
                  wrote on last edited by
                  #8

                  [quote author="Zingam" date="1403690794"]

                  The problem is: the client does not receive every answer by the server. In fact it misses most of them.[/quote]

                  you have two approaches :
                  1)Blocking [Synchronous]-Client receives data from server until it gets all data.To do so use waitForReadyRead() function

                  //e.g.
                  @while(vpClientSocket->waitForReadyRead(500))
                  {
                  QByteArray bta = vpClientSocket->readAll();
                  vstrJsonData.append(bta);
                  }@

                  2)Non Blocking [Asynchronous]-Client receive data as soon as it comes from sever or there is a data to be read.
                  use readyread[signal] for that.
                  //e.g;
                  @
                  void CMessage::SlotReadyRead()
                  {

                  QByteArray ba = vpClientSocket->readAll();
                  vstrJsonData.append(ba);
                  if(vstrJsonData.contains(""SUCCESS"}"))
                  {
                  qDebug() << "Tot : " <<vstrJsonData;
                  vstrJsonData.clear();
                  }
                  }
                  @

                  [quote author="Zingam" date="1403690794"]
                  Generally the client should look for the server/device, connect to it and start requesting data. What I don't know is if I should open a socket and keep it open all the time (and if that's possible at all) or if I need to open a socket everytime when I need data. I'm completely lost here :)[/quote]

                  yeah it is possible to open a socket for all the time
                  yeah it can also be possible to open a socket everytime when I need data.
                  it's up to you.

                  Be Cute

                  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