PDA

View Full Version : Server can't receive RawData



Tadas
18th September 2010, 22:50
Hi,

I'm trying to develop client and server.

client connects to server and sends raw data.
This is how client sends data: (I need to send it like raw data)

client->abort();
client->connectToHost("my Ip",2342); // for ip I use dyndns
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out << out.writeRawData("353184039611442", 14);
client->write(block);
client->disconnectFromHost();
Client code is good, I just can't read it on server.

Server listens for connection and tries to read data.


//constructor//
clientConnection=new QTcpSocket (this);
ServerStart();
connect(&Server, SIGNAL(newConnection()),
this, SLOT(ServerAcceptConnection()));

void MainWindow::ServerStart()
{
Server.listen(QHostAddress::Any, 2342);
}
void MainWindow::ServerAcceptConnection()
{
clientConnection = Server.nextPendingConnection();

connect(clientConnection, SIGNAL(readyRead()),
this, SLOT(readData()));
}

void MainWindow::readData()
{
QString srt, a;
QByteArray byteArray,b,byteArray1;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);

QDataStream in(clientConnection);
while (clientConnection->bytesAvailable())
{
qDebug()<<"data reading started";
in >> b;
byteArray1.append(b);
}
srt.append(b);
qDebug()<< byteArray1.toHex();
qDebug()<< srt;
}

I get this result

"192.168.1.1"
data reading started
""
""

Can't understand, what I'm doing wrong.

wysota
18th September 2010, 23:50
You can simpify your client code to:

client->abort();
client->connectToHost("my Ip",2342); // for ip I use dyndns
client->waitForConnected(); // note this
QByteArray block("353184039611442");
client->write(block);
client->waitForBytesWritten(); // note this
client->disconnectFromHost();

Now what you read at the server is totally incompatible with what you send with the client - if you use writeRawData() on the client, you have to use readRawData() on the server. But again, what you want to do can be simplified to:

void MainWindow::readData()
{
if(clientConnection->bytesAvailable() < 14) return; // wait until there are 14 (shouldn't it be 15?) bytes ready
QByteArray data = clientConnection->read(14); // or readAll()
qDebug() << data;
}

Tadas
19th September 2010, 22:42
Thanks wysota, it worked,

But after testing I had some problems if client is device, which send binary data.
I have changed server function to get data like this:


qDebug() << "bytes recieved";
QByteArray buffer;
qDebug() << "avaliable on start" << clientConnection->bytesAvailable();
buffer = clientConnection->readAll();
qDebug() << "read to buffer" << buffer.toHex();
qDebug() << "availiable now" << clientConnection->bytesAvailable();
This code seems to be more portable, I can receive data from device and client you wrote above.
I'm just wondering why this code can read data from device, and why my code which I wrote before and yours, can't do this?

wysota
19th September 2010, 23:04
But after testing I had some problems if client is device, which send binary data.
I have changed server function to get data like this:
Using readAll() in this context is dangerous as you can read too much data and you will lose sync with your device.


I'm just wondering why this code can read data from device, and why my code which I wrote before and yours, can't do this?
I don't understand what you are asking about.