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.
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.