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] reading data serially in server side in tcp communication
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] reading data serially in server side in tcp communication

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

    hello,
    i am sending data in a qtablewidget into another qtablewidget using tcp communication . The code is as shown below. here no problem in client side but in server side while receiving it receive all data at a time and it get filled to item(0,0) position leaving all other position blank. how can i overcome this problem.

    client side:
    @int i,j;
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {
    QTableWidgetItem *itab= ui->tableWidget->item(i,j);
    QString str= itab->text();
    qDebug()<<"str=="<< str;
    sendsocket->write(qPrintable(str));
    }
    }@

    server side:
    @int i,j;
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {
    QByteArray data =recvsocket->readAll();
    qDebug()<<"data=="<<data;
    ui->tableWidget->setItem(i,j,new QTableWidgetItem(qPrintable(data)));
    }
    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Myzhar
      wrote on last edited by
      #2

      You need to add more information to your "Network data", such as "cell indices".

      ======================
      Myzhar
      www.robot-home.it
      myzharbot.robot-home.it

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ecmanja
        wrote on last edited by
        #3

        sorry i didn't get you. in my case what happening is if i send
        1
        2
        3
        4
        in the receiver side it receives "1234" at a time i want it to receive in
        1
        2
        3
        4
        manner

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Currently your are sending 1234 not
          1
          2
          3
          4

          you don't put any line end or line feed between two writes so all data are append as is.

          What you should do is setup a protocol for your client and server so that the receiving end can know when a new data has fully arrived and it can start parsing it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ecmanja
            wrote on last edited by
            #5

            actually i'm using 2x2 table i need to send it to server side table so i used above code now tell me what change i must do to achieve it

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

              You are getting confused by the screen output generated by qDebug(). It introduces after each call LF.
              The best thing is to think how to write to file and how to read it afterwards from the file. This is exactly the way what you do tcp.
              When writing and reading character string you always need to have a separator or a length stored as well. Alternatively you are using fixed length string, which is the only exception.

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

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ecmanja
                wrote on last edited by
                #7

                sender side:
                @#include "mainwindow.h"
                #include "ui_mainwindow.h"

                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this);
                ui->tableWidget->showMinimized();
                }

                MainWindow::~MainWindow()
                {
                delete ui;
                }

                void MainWindow::on_pushButton_clicked()
                {
                sendsocket = new QTcpSocket(this);
                // QString hostaddress = "192.168.1.183";
                QString hostaddress = "127.0.0.1";
                sendsocket->connectToHost(hostaddress,1234,QIODevice::ReadWrite);
                int i,j;
                for(i=0;i<2;i++)
                {
                for(j=0;j<2;j++)
                {
                QTableWidgetItem *itab= ui->tableWidget->item(i,j);
                QString str= itab->text();
                qDebug()<<"str=="<< str;
                ui->tableWidget_2->setItem(i,j,new QTableWidgetItem(str));
                sendsocket->write(qPrintable(str));
                }
                }
                }

                void MainWindow::on_pushButton_2_clicked()
                {
                ui->tableWidget_2->clear();
                }
                @

                receiver side :
                @#include "mainwindow.h"
                #include "ui_mainwindow.h"

                MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this);
                serversocket= new QTcpServer(this);
                serversocket->listen(QHostAddress::Any,1234);
                connect(serversocket,SIGNAL(newConnection()),this,SLOT(on_connection()));
                }

                void MainWindow::on_connection()
                {
                recvsocket=serversocket->nextPendingConnection();
                connect(recvsocket,SIGNAL(disconnected()),this,SLOT(on_disconnection()));
                connect(recvsocket,SIGNAL(readyRead()),this,SLOT(on_read_data()));
                }
                void MainWindow::on_read_data()
                {
                #if 0
                ui->textBrowser->clear();
                QByteArray data=recvsocket->readAll();
                qDebug()<<"recieved data=="<< data;
                ui->textBrowser->append(data);
                /* if(data=="ITEM 1")
                {
                ui->textBrowser->append(data);
                }
                if(data=="ITEM 4")
                {
                ui->textBrowser_2->append(data);
                }
                if(data=="ITEM 7")
                {
                ui->textBrowser_3->append(data);
                }*/
                #endif
                int i,j;
                for(i=0;i<2;i++)
                {
                for(j=0;j<2;j++)
                {
                QByteArray data =recvsocket->readAll();
                qDebug()<<"data=="<<data;
                ui->tableWidget->setItem(i,j,new QTableWidgetItem(qPrintable(data)));
                }
                }

                }
                void MainWindow::on_disconnection()
                {
                recvsocket->close();
                }

                MainWindow::~MainWindow()
                {
                delete ui;
                }
                @
                here is my code now u can get clear picture about the problem

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  ecmanja
                  wrote on last edited by
                  #8

                  i found solution if i use "read(no. of digits)" instead of readall() i getin that order.

                  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