Tady je zdrojovy kod. server.cpp:
#include "server.h"
server::server (QWidget* parent):QWidget (parent)
{
prijem=new QTcpServer (this);
hostAddress=QHostAddress ("192.168.0.103");
prijem->listen (hostAddress,25000);
socket=new QTcpSocket ();
sockety=QMap <QString,QTcpSocket *> ();
connect (prijem,SIGNAL (newConnection ()),this,SLOT (vytvorSlot()));
}
void server::vytvorSlot ()
{
socket=prijem->nextPendingConnection ();
connect (socket,SIGNAL (readyRead()),this,SLOT (startRead()));
socket->write ("LOGIN");
rawdata = socket->readAll();
prezdivka=rawdata;
sockety.insert (prezdivka,socket);
}
void server::startRead ()
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(this->sender());
// požadavek
rawdata = socket->readAll();
// argumenty požadavku
args = rawdata.split(' ');
// samotný příkaz
command = args.takeFirst();
qDebug() << rawdata;
}
server.h:
#ifndef _SERVER_H
#define _SERVER_H
#include <qtcpserver.h>
#include <qtcpsocket.h>
#include <qabstractsocket.h>
#include <qapplication.h>
#include <qwidget.h>
class server:public QWidget
{
Q_OBJECT
private:
QTcpServer * prijem;
QHostAddress hostAddress;
QTcpSocket * socket;
QStringList tokens;
QByteArray rawdata;
QList<QByteArray> args;
QMap <QString,QTcpSocket *> sockety;
QString command;
QString prezdivka;
public:
server (QWidget* parent=0);
public slots:
void vytvorSlot ();
void startRead ();
};
#endif
je to naslouchani na novou zpravu spravne? Neni to uz vyresene? nejsem si moc jisty.