Реализация Сервера (без .h файлов)
#include "server.h"
#include "ServerThread.h"
Server::Server(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
server = new CServerThread(this);
server->start();
}
#include "ServerThread.h"
#include "CServerTcp.h"
CServerThread::CServerThread(QObject *parent)
: QThread(parent)
{
moveToThread(this);
serverTcp = new CServerTcp(this);
}
void CServerThread::run()
{
if(!serverTcp->listen(QHostAddress::Any, 1204)) {
return;
}
while(1) {
sleep(2);
}
exec();
}
#include "CServerTcp.h"
CServerTcp::CServerTcp(QObject *parent)
: QTcpServer(parent)
{
}
void CServerTcp::incomingConnection(int socketId)
{
qDebug("New client connection!");
}
Реализация клиента
#include "client.h"
#include "ClientThread.h"
Client::Client(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
client = new CClientThread(this);
client->start();
}
#include "ClientThread.h"
#include "ClientTcp.h"
CClientThread::CClientThread(QObject *parent)
: QThread(parent)
{
moveToThread(this);
clientTcp = new CClientTcp(this);
}
void CClientThread::run()
{
clientTcp->connectToHost("192.168.0.1",1204);
if(!clientTcp->waitForConnected()) {
qDebug("connected faild");
return;
}
while(1) {
sleep(2);
}
exec();
}