ahoj,pro muj server sem zacal delat i jednoduche gui. uz je hotove ale mam problem,potreboval bych aby se po zmacknuti tlacitka vyvolala nejaka funkce toho serveru,gui a samotny server sou samostatne tridy. lze to nejak udelat? napadlo me nekolik zpusobu. tady je kod
gui.h:
#ifndef GUI
#define GUI
#include "header.h"
#include "mujserver.h"
class gui:public QWidget
{
Q_OBJECT
private:
QWidget *widget,*widget2;
QComboBox * listClients;
QPushButton * kickClient,*kickAllClients,*sendMsg,*startServer,*stopServer,*nastavit;
QHBoxLayout * layout,*layout2;
QVBoxLayout * Vlayout;
QTextBrowser * stavovyText;
QLineEdit * adresaEdit,*portEdit;
public:
gui ();
public slots:
void stop ();
void start ();
void KickAll();
void kick (QString prezdivka);
void send ();
void SendAll ();
};
#endif GUI
gui.cpp:
#include "gui.h"
#include "mujserver.h"
gui::gui ()
{
QPushButton * kickClient=new QPushButton ("vykopnout klienta");
QPushButton *kickAllClients=new QPushButton ("vykopnout vsechny klienty");
QPushButton *sendMsg=new QPushButton ("posli zpravu");
QPushButton *startServer=new QPushButton ("nastartovat");
QPushButton *stopServer=new QPushButton ("stop");
QPushButton * nastavit=new QPushButton ("nastavit");
QHBoxLayout *layout=new QHBoxLayout ();
QHBoxLayout * layout2=new QHBoxLayout ();
QVBoxLayout * Vlayout=new QVBoxLayout ();
QComboBox * listClients=new QComboBox ();
QLineEdit * adresaEdit=new QLineEdit ();
QLineEdit *portEdit=new QLineEdit ();
QWidget * widget=new QWidget (this);
QWidget * widget2=new QWidget (this);
layout->addWidget (kickClient);
layout->addWidget (kickAllClients);
layout->addWidget (sendMsg);
layout->addWidget (startServer);
layout->addWidget (stopServer);
layout->addWidget (listClients);
layout2->addWidget (adresaEdit);
layout2->addWidget (portEdit);
layout2->addWidget (nastavit);
widget->setLayout (layout);
widget2->setLayout (layout2);
Vlayout->addWidget (widget);
Vlayout->addWidget (widget2);
this->setLayout (Vlayout);
connect (startServer,SIGNAL (clicked ()),this,SLOT (start()));
connect (stopServer,SIGNAL (clicked ()),this,SLOT (stop()));
connect (kickClient,SIGNAL (clicked ()),this,SLOT (kick()));
connect (kickAllClients,SIGNAL (clicked ()),this,SLOT (KickAll()));
connect (sendMsg,SIGNAL (clicked ()),this,SLOT (poslat()));
connect (nastavit,SIGNAL (clicked()),this,SLOT (nastav()));
show ();
}
void gui::start ()
{
}
void gui::stop ()
{
}
void gui::kick (QString prezdivka)
{
}
void gui::KickAll ()
{
}
void gui::send ()
{
}
void gui::SendAll ()
{
}
mujserver.h
#ifndef SERVER
#define SERVER
#include "header.h"
#define LOGIN 0
#define SEND 1
#define SENDALL 2
#define RECV 3
#define KICK 4
#define KICKALL 5
#define CHANGESTAT 6
#define LOGOUT 7
#define REGISTER 8
#define LOGINOK 9
#define LOGINFALSE 10
#define START 11
#define STOP 12
class mujserver:public QTcpServer
{
Q_OBJECT
public:
mujserver ();
void stop ();
void start ();
void KickAll();
void kick (QString prezdivka);
void send ();
void SendAll ();
private:
int port;
struct packet;
QMap <QString ,QTcpSocket*>* clients;
QByteArray data;
QTcpSocket * listenSocket;
QHostAddress addressForListen;
void SetPort ();
void PacketFromData (packet & p,QByteArray *data);
QByteArray DataFromPacket (packet p);
void CheckLogin ();
void registruj ();
public slots:
void AcceptConnection ();
void StartRead ();
};
#endif SERVER
mujserver.cpp:
#include "mujserver.h"
struct mujserver::packet
{
unsigned int flag;
QByteArray data;
QString sender;
QString receiver;
int options;
};
mujserver::mujserver ()
{
QString adresa ("86.49.56.76");
addressForListen =QHostAddress (adresa);
}
void mujserver::AcceptConnection ()
{
QTcpSocket *socket=nextPendingConnection ();
QByteArray prezdivka=socket->read (20);
clients->insert (prezdivka,socket);
connect (socket,SIGNAL (readyRead ()),this,SLOT (StartRead ()));
}
void mujserver::PacketFromData (packet & p,QByteArray * data)
{
QDataStream s (data,QIODevice::WriteOnly);
s<< p.flag;
s<<p.data;
s<<p.receiver;
s<<p.sender;
s<<p.options;
}
QByteArray mujserver::DataFromPacket (packet p)
{
QByteArray *data=new QByteArray ();
QDataStream s (data,QIODevice::WriteOnly);
s>>p.flag;
s>>p.data;
s>>p.receiver;
s>>p.sender;
s<<p.options;
s<<'\0';
return *data;
}
void mujserver::StartRead ()
{
QTcpSocket * socket=new QTcpSocket (QObject::sender ());
data=socket->readAll ();
packet mujpacket;
PacketFromData (mujpacket,&data);
switch (mujpacket.flag)
{
case LOGIN:
{
QFile soubor ("hesla.txt");
soubor.open (QIODevice::ReadOnly);
QTextStream stream (&soubor);
while (!soubor.atEnd ())
{
QString radek=stream.readLine ();
if (mujpacket.data==radek)
{
packet odeslani;
odeslani.flag=LOGINOK;
socket->write (DataFromPacket (odeslani));
}
if (soubor.atEnd ())
{
packet odeslani;
odeslani.flag=LOGINFALSE;
socket->write (DataFromPacket (odeslani));
}
}
}
break;
case SEND:
break;
case SENDALL:
break;
case RECV:
break;
case KICK:
break;
case KICKALL:
break;
case CHANGESTAT:
break;
case LOGOUT:
break;
case REGISTER:
{
QFile soubor ("hesla.txt");
soubor.open (QIODevice::Append);
QTextStream stream (&soubor);
stream >> mujpacket.data;
soubor.close ();
break;
}
}
}
void mujserver::start ()
{
listen (addressForListen,6000);
connect (this,SIGNAL (newConnection ()),this,SLOT (AcceptConnection()));
}
void mujserver::stop ()
{
close ();
}
void mujserver::kick (QString prezdivka)
{
}
void mujserver::KickAll ()
{
}
void mujserver::send ()
{
}
void mujserver::SendAll ()
{
}
nevim si rady s tim jak to udelat,dekuju za napady.