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();
public slots:
// TCp
void polacz();
void Trejestracja();
private:
};
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;
};
To copy to clipboard, switch view to plain text mode
client.cpp
{
setupUi(this);
polacz();
Trejestracja();
}
void klient::polacz()
{
tcpSocket->connectToHost("localhost",6112);
}
{
//COPIED FROM EXAMPLE
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");
}
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");
}
To copy to clipboard, switch view to plain text mode
server.h
class server
: public QWidget,
private Ui
::serverClass{
Q_OBJECT
public:
~server();
public slots:
// TCP
void newconnection();
void odbierzw();
private:
// TCP
quint16 blockSize;
};
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;
};
To copy to clipboard, switch view to plain text mode
server.cpp
{
setupUi(this);
// TCp
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newconnection()));
connect(tcpServer, SIGNAL(readyRead()), this, SLOT(odbierzw()));
}
void server::newconnection()
{
clientConnection = tcpServer->nextPendingConnection();
ip = clientConnection->localAddress().toString();
item->setText(0,ip);
item
->setText
(1,
QTime::currentTime().
toString());
TWlist->addTopLevelItem(item);
}
void server::odbierzw()
{
// COPIED FROM EXAMPLE
if (blockSize == 0) {
if (clientConnection->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (clientConnection->bytesAvailable() < blockSize)
return;
in >> nextFortune;
/*
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);
QString sprawdz;
in >> sprawdz;
label->setText(sprawdz);*/
label_2->setText("DOSZLO");
}
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");
}
To copy to clipboard, switch view to plain text mode
Big thx for help
Best Regards
ps
sry for my english ;p
Bookmarks