TcpServer
Verfasst: 26. August 2010 22:13
Hi Leute.
Ich wollte mal Tcp mit Qt ausprobieren, allerdings fange ich jetzt an langsam
gar nichts mehr zu verstehen.
Also ich habe einen Client geschrieben, und einen Server .... hier der Code.
Der Server:
Hier der Client:
Also wenn ich den Code ausführe bekomme ich zwar das "blablabla",
allerdings nicht das "tuuuut".
Was mache ich falsch?
Schon mal danke.
Gruss Irods.
Ich wollte mal Tcp mit Qt ausprobieren, allerdings fange ich jetzt an langsam
gar nichts mehr zu verstehen.
Also ich habe einen Client geschrieben, und einen Server .... hier der Code.
Der Server:
Code: Alles auswählen
#ifndef MYPROCESS_H
#define MYPROCESS_H
#include <QtGui>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
class MyProcess : public QMainWindow
{
Q_OBJECT
public:
MyProcess(QMainWindow *parent = 0);
private:
QTcpServer *tcpServer;
QTcpSocket *clientConnection;
QByteArray block;
QDataStream *out;
private slots:
void connection();
};
#endif
Code: Alles auswählen
#include "MyProcess.h"
MyProcess::MyProcess(QMainWindow *parent) : QMainWindow(parent)
{
out = new QDataStream(&block, QIODevice::WriteOnly);
out->setVersion(QDataStream::Qt_4_0);
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::Any, 3745);
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(connection()));
}
void MyProcess::connection()
{
clientConnection = tcpServer->nextPendingConnection();
*out << QString("blablabla");
clientConnection->write(block);
*out << QString("tuuuut");
clientConnection->write(block);
clientConnection->disconnectFromHost();
}
Code: Alles auswählen
#ifndef MYCLIENT_H
#define MYCLIENT_H
#include <QtGui>
#include <QTcpServer>
#include <QTcpSocket>
class MyClient : public QMainWindow
{
Q_OBJECT
public:
MyClient(QMainWindow *parent = 0);
private:
QTcpSocket* tcpSocket;
private slots:
void incomming();
};
#endif
Code: Alles auswählen
#include "MyClient.h"
MyClient::MyClient(QMainWindow *parent) : QMainWindow(parent)
{
tcpSocket = new QTcpSocket;
tcpSocket->connectToHost("localhost", 3745);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(incomming()));
}
void MyClient::incomming()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);
QString line;
in>>line;
QMessageBox msgBox;
msgBox.setText(line);
msgBox.exec();
}
allerdings nicht das "tuuuut".
Was mache ich falsch?
Schon mal danke.
Gruss Irods.