hier der Quellcode
Code: Alles auswählen
#include <QtNetwork>
#include "iostream.h"
#include "client.h"
Client::Client()
{
socket = new QLocalSocket(this);
connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)),
this, SLOT(displayError(QLocalSocket::LocalSocketError)));
requestNewFortune();
}
void Client::requestNewFortune()
{
blockSize = 0;
socket->connectToServer("test");
sendFortune();
}
void Client::sendFortune()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << (QString)"hallo 1";
out << (QString)"hallo 2";
out << (QString)"hallo 3";
out << (QString)"hallo 4";
out << (QString)"hallo 5";
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
qint64 fehler;
fehler=socket->write(block);
cout <<fehler<<endl;
bool flush=socket->flush();
cout <<flush<<endl;
}Code: Alles auswählen
#include <QtNetwork>
#include "server.h"
#include <stdlib.h>
#include <qlocalserver.h>
#include <qlocalsocket.h>
#include "iostream.h"
Server::Server()
{
server = new QLocalServer(this);
if (!server->listen("test"))
{
cout<<"Unable to start the server"<<endl;
return;
}
cout <<"server is running"<<endl;
connect(server,SIGNAL(newConnection()),this,SLOT(readFortune()));
}
void Server::readFortune()
{
bool pruefe=false;
quint16 blockSize=0;
while(!pruefe)
{
if(server->hasPendingConnections())
{
msleep(100);
int i=server->serverError();
cout <<i<<endl;
QLocalSocket *clientConnection = server->nextPendingConnection();
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_0);
if (blockSize == 0)
{
while(clientConnection->bytesAvailable() < 1)
{
cout<<clientConnection->bytesAvailable()<<endl;
msleep(500);
}
in >> blockSize;
}
QString nextFortune;
while(!in.atEnd())
{
in >> nextFortune;
cout <<nextFortune.toLocal8Bit().constData()<<endl;
}
pruefe=true;
}
}
}