PDA

View Full Version : QTcpServer dosent revie msg



Zergi
29th December 2007, 18:08
Hi

I dont know why my server dosent recive any msg... I even copied code from example and it dosent work... Can u tell me what is wrong? I m sure thath client sendes msg but server dosent recive them... I tried to write in QDataStream in(tcpServer) but it didnt want to compile...

client.h

class klient : public QMainWindow,private Ui::klientClass
{
Q_OBJECT

public:
klient(QWidget *parent = 0);
~klient();
public slots:
// TCp
void polacz();
void Trejestracja();
private:
QTcpSocket *tcpSocket;


};

client.cpp


klient::klient(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
polacz();
Trejestracja();

}
void klient::polacz()
{
tcpSocket = new QTcpSocket(this);
tcpSocket->connectToHost("localhost",6112);

}

void klient::Trejestracja(QString l,QString h)
{
//COPIED FROM EXAMPLE
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << "heh";
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
tcpSocket->disconnectFromHost();
/*
QByteArray reje;
QDataStream out(&reje, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out<<"TEST";
tcpSocket->write(reje);*/
Lopis->setText("OK");
}

server.h

class server : public QWidget, private Ui::serverClass
{
Q_OBJECT

public:
server(QWidget *parent = 0);
~server();
public slots:
// TCP
void newconnection();
void odbierzw();


private:

// TCP
QTcpServer *tcpServer;
QTcpSocket *clientConnection;
quint16 blockSize;

};

server.cpp

server::server(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
// TCp
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any,6112);
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newconnection()));
connect(tcpServer, SIGNAL(readyRead()), this, SLOT(odbierzw()));




}


void server::newconnection()
{
clientConnection = tcpServer->nextPendingConnection();
QString ip;
ip = clientConnection->localAddress().toString();
QTreeWidgetItem *item = new QTreeWidgetItem;
item->setText(0,ip);
item->setText(1,QTime::currentTime().toString());
TWlist->addTopLevelItem(item);

}
void server::odbierzw()
{
// COPIED FROM EXAMPLE
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);

if (blockSize == 0) {
if (clientConnection->bytesAvailable() < (int)sizeof(quint16))
return;

in >> blockSize;
}

if (clientConnection->bytesAvailable() < blockSize)
return;
QString nextFortune;
in >> nextFortune;


/*
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);
QString sprawdz;
in >> sprawdz;
label->setText(sprawdz);*/
label_2->setText("DOSZLO");


}

Big thx for help
Best Regards

ps
sry for my english ;p

high_flyer
29th December 2007, 18:11
While I go through the code, try running the application from the Qt console, and see if you get any warnings in the console.

jacek
29th December 2007, 18:11
You should connect clientConnection's readyRead() to odbierzw().

Zergi
29th December 2007, 18:18
Yes i did thath before i posted ... When i do thath my program dosent want to open ;p I have msg with errors from windows... I tried to copy connect to newconnection() function, program opened but he still dosent want to recive msg....

thx for help

high_flyer
29th December 2007, 18:39
I have msg with errors from windows...
Post the errors you got.

Zergi
29th December 2007, 20:04
It's normal windows error ;p
something like thath http://www.oatmedia.com/support/windows_error.jpg

jacek
29th December 2007, 20:11
Compile your application in debug mode and start it within a debugger. You should get a real error message then. But first make sure that clientConnection is initialized at the time you establish the connection.

And a small question: What will happen if two clients connect to the server at the same time?