[SOLVED] reading data serially in server side in tcp communication
-
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)));
}
}
@ -
You need to add more information to your "Network data", such as "cell indices".
-
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 -
Hi,
Currently your are sending 1234 not
1
2
3
4you 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.
-
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
-
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. -
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 -
i found solution if i use "read(no. of digits)" instead of readall() i getin that order.