#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include <QByteArray>
class ServerThread;
class Dialog;
{
Q_OBJECT
private:
Dialog *parent_;
public:
explicit Server(Dialog *parent);
void incomingConnection(int socketDescriptor
);
void startServer(int);
signals:
public slots:
};
#endif // SERVER_H
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H
#include <QThread>
#include <QByteArray>
#include "dialog.h"
class ServerThread
: public QThread{
Q_OBJECT
private:
int socketDescriptor_;
Dialog *parent_;
public:
explicit ServerThread(int socketDescriptor,Dialog *parent);
void run(void);
bool emit_signal;
signals:
public slots:
void received(void);
void disconnected(void);
};
#endif // SERVERTHREAD_H
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QString>
#include <QByteArray>
namespace Ui {
class Dialog;
}
class Server;
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
Server *s;
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
void received();
signals:
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
#include <QByteArray>
class ServerThread;
class Dialog;
class Server : public QTcpServer
{
Q_OBJECT
private:
Dialog *parent_;
public:
explicit Server(Dialog *parent);
void incomingConnection(int socketDescriptor
);
void startServer(int);
signals:
void received(QString data);
public slots:
void onreceived(QString data);
};
#endif // SERVER_H
#ifndef SERVERTHREAD_H
#define SERVERTHREAD_H
#include <QThread>
#include <QByteArray>
#include "dialog.h"
class QTcpSocket;
class ServerThread : public QThread
{
Q_OBJECT
private:
int socketDescriptor_;
Dialog *parent_;
public:
explicit ServerThread(int socketDescriptor,Dialog *parent);
QTcpSocket *tcpSocket;
void run(void);
bool emit_signal;
signals:
void received_sgl(QString str);
public slots:
void received(void);
void disconnected(void);
};
#endif // SERVERTHREAD_H
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QString>
#include <QByteArray>
namespace Ui {
class Dialog;
}
class QTcpSocket;
class Server;
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
QTcpSocket *socket;
Server *s;
void update(QString s);
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
void received();
void rcd_(QByteArray arr);
signals:
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
To copy to clipboard, switch view to plain text mode
SERVER.cpp
#include "server.h"
#include "serverthread.h"
#include <QDebug>
Server::Server(Dialog *parent) :
{
}
void Server::startServer(int port)
{
qDebug()<<"listening!";
}
void Server::incomingConnection(int socketDescriptor)
{
qDebug()<<"Server::incomming connection";
ServerThread *thread=new ServerThread(socketDescriptor,parent_);
connect(thread,
SIGNAL(received_sgl
(QString)),
this,
SLOT(onreceived
(QString)));
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()));
thread->start();
}
void Server
::onreceived(QString data
) {
qDebug()<<"updating...::::"<<QString(data);
parent_->update("updating..."+data);
}
ServerThread.cpp
#include "serverthread.h"
#include <QTcpSocket>
#include <QDebug>
ServerThread::ServerThread(int socketDescriptor,Dialog *parent) :
QThread(parent
),socketDescriptor_
(socketDescriptor
),parent_
(parent
) {
}
void ServerThread::run()
{
tcpSocket->setSocketDescriptor(socketDescriptor_);
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(received()),Qt::DirectConnection);
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
qDebug()<<"ServerThread::run";
exec();
}
void ServerThread::received()
{
qDebug()<<"ServerThread::received()"<<QString(tcpSocket->readAll());
emit received_sgl
(QString(tcpSocket
->readAll
()));
if (tcpSocket->isWritable())
{
}
}
void ServerThread::disconnected()
{
tcpSocket->deleteLater(); //delete tcpSocket once exit() for thread is executed
exit(); //exit the thread
}
Dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "server.h"
#include <QTcpSocket>
#include <QDebug>
ui(new Ui::Dialog)
{
ui->setupUi(this);
s=new Server(this);
QObject::connect(socket,
SIGNAL(readyRead
()),
this,
SLOT(received
()));
}
Dialog::~Dialog()
{
delete ui;
}
{
qDebug()<<QString(arr);
}
void Dialog::on_pushButton_2_clicked()
{
s->startServer(ui->lineEdit->text().toInt());
ui->lineEdit->setEnabled(false);
}
void Dialog::received()
{
qDebug()<<"received by client";
update
(QString(socket
->readAll
()));
}
void Dialog::on_pushButton_clicked()
{
qDebug()<<"sending...";
socket->connectToHost(ui->lineEdit_3->text(),ui->lineEdit_2->text().toInt());
socket->write("write now");
socket->close();
// socket->close();
//delete socket;
}
{
ui->lblmsg->setText(s);
}
SERVER.cpp
#include "server.h"
#include "serverthread.h"
#include <QDebug>
Server::Server(Dialog *parent) :
QTcpServer(parent),parent_(parent)
{
}
void Server::startServer(int port)
{
this->listen(QHostAddress::Any,port);
qDebug()<<"listening!";
}
void Server::incomingConnection(int socketDescriptor)
{
qDebug()<<"Server::incomming connection";
ServerThread *thread=new ServerThread(socketDescriptor,parent_);
connect(thread,SIGNAL(received_sgl(QString)),this,SLOT(onreceived(QString)));
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()));
thread->start();
}
void Server::onreceived(QString data)
{
qDebug()<<"updating...::::"<<QString(data);
parent_->update("updating..."+data);
}
ServerThread.cpp
#include "serverthread.h"
#include <QTcpSocket>
#include <QDebug>
ServerThread::ServerThread(int socketDescriptor,Dialog *parent) :
QThread(parent),socketDescriptor_(socketDescriptor),parent_(parent)
{
}
void ServerThread::run()
{
tcpSocket=new QTcpSocket(parent_);
tcpSocket->setSocketDescriptor(socketDescriptor_);
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(received()),Qt::DirectConnection);
connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(disconnected()),Qt::DirectConnection);
qDebug()<<"ServerThread::run";
exec();
}
void ServerThread::received()
{
qDebug()<<"ServerThread::received()"<<QString(tcpSocket->readAll());
emit received_sgl(QString(tcpSocket->readAll()));
if (tcpSocket->isWritable())
{
tcpSocket->write(QByteArray("writing..."));
}
}
void ServerThread::disconnected()
{
tcpSocket->deleteLater(); //delete tcpSocket once exit() for thread is executed
exit(); //exit the thread
}
Dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include "server.h"
#include <QTcpSocket>
#include <QDebug>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
s=new Server(this);
socket=new QTcpSocket(this);
QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(received()));
QObject::connect(s, SIGNAL(received(QByteArray)), this, SLOT(rcd_(QByteArray)));
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::rcd_(QByteArray arr)
{
ui->lblmsg->setText(QString(arr));
qDebug()<<QString(arr);
}
void Dialog::on_pushButton_2_clicked()
{
s->startServer(ui->lineEdit->text().toInt());
ui->lineEdit->setEnabled(false);
}
void Dialog::received()
{
qDebug()<<"received by client";
update(QString(socket->readAll()));
}
void Dialog::on_pushButton_clicked()
{
qDebug()<<"sending...";
socket->open(QAbstractSocket::ReadWrite);
socket->connectToHost(ui->lineEdit_3->text(),ui->lineEdit_2->text().toInt());
socket->write("write now");
socket->close();
// socket->close();
//delete socket;
}
void Dialog::update(QString s)
{
ui->lblmsg->setText(s);
}
To copy to clipboard, switch view to plain text mode
Bookmarks