#include "qtnetwork.h"
#include <QtNetwork\qtcpsocket.h>
#include <QtNetwork\qtcpserver.h>
#include <QtCore\qthread.h>
qtnetwork
::qtnetwork(QWidget *parent
){
ui.setupUi(this);
//connect(ui.actionTest, SIGNAL(triggered(bool)), this, SLOT(on_actionTest_triggered(bool)));
}
qtnetwork::~qtnetwork()
{
}
void qtnetwork::on_actionTest_triggered(bool)
{
qDebug() << "hitme";
connect(sv, SIGNAL(newConnection(void)), this, SLOT(on_server_newConnection(void)));
s->connectToHost("localhost", 80);
connect(s, SIGNAL(connected()), this, SLOT(on_socket_connect(void)));
connect(s, SIGNAL(readyRead()), this, SLOT(on_socket_readyread(void)));
//delete s;
}
class MyReadThread
:public QThread{ Q_OBJECT
public:
ms = s;
connect(ms, SIGNAL(readyRead(void)), this, SLOT(on_socket_readyRead(void)));
}
void run(){
qDebug() << "mythread run";
}
private slots:
void on_socket_readyRead(void){
qDebug() << ms->readAll();
}
};
void qtnetwork::on_server_newConnection(void)
{
qDebug() << "new connection";
MyReadThread *t = new MyReadThread(s);
t->start();
}
void qtnetwork::on_socket_connect(void)
{
qDebug() << "connected";
const char buffer[] = { "Hello's world" };
int len = sizeof(buffer);
s->write(buffer, len);
}
void qtnetwork::on_socket_readyread(void)
{
qDebug() << s->readLine();
ui.plainTextEdit->appendPlainText("server:"+t);
}
#include "qtnetwork.h"
#include <QtNetwork\qtcpsocket.h>
#include <QtNetwork\qtcpserver.h>
#include <QtCore\qthread.h>
qtnetwork::qtnetwork(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//connect(ui.actionTest, SIGNAL(triggered(bool)), this, SLOT(on_actionTest_triggered(bool)));
}
qtnetwork::~qtnetwork()
{
}
void qtnetwork::on_actionTest_triggered(bool)
{
qDebug() << "hitme";
sv = new QTcpServer(this);
sv->listen(QHostAddress::LocalHost, 80);
connect(sv, SIGNAL(newConnection(void)), this, SLOT(on_server_newConnection(void)));
s = new QTcpSocket(this);
s->connectToHost("localhost", 80);
connect(s, SIGNAL(connected()), this, SLOT(on_socket_connect(void)));
connect(s, SIGNAL(readyRead()), this, SLOT(on_socket_readyread(void)));
//delete s;
}
class MyReadThread :public QThread{
Q_OBJECT
public:
QTcpSocket *ms;
MyReadThread(QTcpSocket*s){
ms = s;
connect(ms, SIGNAL(readyRead(void)), this, SLOT(on_socket_readyRead(void)));
}
void run(){
qDebug() << "mythread run";
QThread::run();
}
private slots:
void on_socket_readyRead(void){
qDebug() << ms->readAll();
}
};
void qtnetwork::on_server_newConnection(void)
{
qDebug() << "new connection";
QTcpSocket *s = sv->nextPendingConnection();
MyReadThread *t = new MyReadThread(s);
t->start();
}
void qtnetwork::on_socket_connect(void)
{
qDebug() << "connected";
const char buffer[] = { "Hello's world" };
int len = sizeof(buffer);
s->write(buffer, len);
}
void qtnetwork::on_socket_readyread(void)
{
qDebug() << s->readLine();
QByteArray t = s->readAll();
ui.plainTextEdit->appendPlainText("server:"+t);
}
To copy to clipboard, switch view to plain text mode
Bookmarks