PDA

View Full Version : Qt Socketprogramming, deliver data repetitively to server



soleil
11th October 2011, 08:17
Hi
I'd like to deliver the updated datastring every 20ms to the server on click. at the moment only one datastring is delivered on click.
I tried to set a loop in void MainWindow::ConnectToServer(), but it does not seem to work.
below is the affected part of the code

I would appreciate your help
thanks in advance





MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Initialize
p_MainThread = NULL;
p_SensorThreadArr = NULL;
p_TcpSocket = NULL;

// set GUI
SetGUIDialogBox();

///p_MainThread = new MainThread();

// ********************
// Connect to Server
// ********************
p_TcpSocket = &tcpSocket;
//connect(p_TcpSocket, SIGNAL(connected()), this, SLOT(SendRequest()));
connect(p_TcpSocket, SIGNAL(connected()), this, SLOT(StartTransfer()));
// Two Buttons
connect(searchButton, SIGNAL(clicked()), this, SLOT(ConnectToServer()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(StopSearch()));

// **********************
// Connect GUI Dialog Box
// **********************
connect(p_TcpSocket, SIGNAL(disconnected()), this, SLOT(ConnectionClosedByServer()));
connect(p_TcpSocket, SIGNAL(readyRead()), this, SLOT(UpdateTableWidget()));
connect(p_TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(Error()));
}


void MainWindow::ConnectToServer()
{
p_MainThread->start();

bool isRuning = true;
while (isRuning == true)
for (int Ind=0; Ind<5; Ind++)
{
qDebug( " connectToServer! " );

////p_ClientDataSocket->ConnectToServer(address, port);
quint16 port = 8888;
QString address = "127.0.0.1";
QHostAddress addr(address);
p_TcpSocket->connectToHost(addr, port);

UpdateConnectionGUI();
//sleep( 1 ); // Sleep xx ms
}

}


void MainWindow::StartTransfer()
{
// *********************************
// read sensor data from all sensors
// *********************************
for (int Ind=0; Ind<m_SensorSum; Ind++)
{
QByteArray qBetyArray = p_SensorThreadArr[Ind]->GetSensorData();

// *****************************************
// build client socket message as QByreArray
// *****************************************
// int sensorDataStatus = p_SensorThread->GetSensorDataStatus();
// no updated sensor data
// if (sensorDataStatus != 1)
// return;

// *******************************************
// send client socket message to server socket
// *******************************************
int len = qBetyArray.length();
tcpSocket.write(qBetyArray, len);
}

return;
}

wysota
11th October 2011, 08:28
Your loop doesn't make any sense. connectToHost() is a non-blocking call, it just schedules a connect and returns immediately. You need a timer that is started when the socket manages to connect and that triggers a slot that will write to the socket. Oh, and if you expect that you'll get the data on the receiving end with time differences of 20ms, that's probably not going to happen. I would expect to see it all coming at the same time (or almost at the same time) unless you send enough data to fill one TCP window each time.