赞
踩
#ifndef TCP4WEB_H
#define TCP4WEB_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
class Tcp4Web : public QObject
{
Q_OBJECT
public:
explicit Tcp4Web(QObject *parent = 0);
~Tcp4Web();
signals:
void dataArrive(QByteArray ba); //send data to deal
public slots:
void onNewConnection(); //new connection come
void socketReaddata(); //read socket data
void socketDisconnect();
private:
QTcpServer *m_tcpserver;
QByteArray tmpdata;
int tcpPort = 6000;
QByteArray m_buffer;
QHash<QString, QTcpSocket*>m_validClientList;// 有效列表<用户:密码,QTcpSocket* >
QList<QTcpSocket*>m_allClinet;
};
#endif // TCP4WEB_H
#include "tcp4web.h"
Tcp4Web::Tcp4Web(QObject *parent) : QObject(parent)
{
//m_thread = new QThread();
m_tcpserver = new QTcpServer();
connect(m_tcpserver, &QTcpServer::newConnection, this, &Tcp4Web::onNewConnection, Qt::DirectConnection);
if (!m_tcpserver->listen(QHostAddress::AnyIPv4, 6000))
{
m_tcpserver->close();
qDebug()<< "tcplistenerror:" << m_tcpserver->errorString();
}
}
Tcp4Web::~Tcp4Web()
{
m_tcpserver->close();
delete m_tcpserver;
}
void Tcp4Web::onNewConnection()
{
QTcpSocket *socket_Client= m_tcpserver->nextPendingConnection();
m_allClinet.append(socket_Client);
qDebug() << "new TCPconnection come.";
connect(socket_Client, SIGNAL(readyRead()), this, SLOT(socketReaddata()));
connect(socket_Client, SIGNAL(disconnected()), this, SLOT(socketDisconnect()));
QByteArray data("connect ... need uersname and pwd to login");
socket_Client->write(data, data.length());
}
void Tcp4Web::socketReaddata()
{
QTcpSocket *socket_Client = (QTcpSocket*)sender();
if(m_allClinet.contains(socket_Client))
{
qDebug() << "m_allClinet.contains TCPconnection."<< m_allClinet.indexOf(socket_Client);
}
QByteArray data = socket_Client->readAll();
QString str = data.data();
qDebug()<< " 11111111 "<<str;
emit dataArrive(tmpdata);
}
void Tcp4Web::socketDisconnect()
{
QTcpSocket *socket_Client = (QTcpSocket*)sender();
m_allClinet.removeOne(socket_Client);
socket_Client->deleteLater();
qDebug()<< " deleteLater "<<m_allClinet.size();
}
客户端
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_processPendingData();
private:
Ui::Widget *ui;
QList<QTcpSocket*> m_socketList;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
QTcpSocket *newSocket = new QTcpSocket(this);
connect(newSocket, SIGNAL(readyRead()), this, SLOT(on_processPendingData()));
newSocket->connectToHost("192.168.61.213", 6000);
if (newSocket->waitForConnected() && newSocket->error() != QAbstractSocket::SocketTimeoutError)
{
m_socketList.append(newSocket);
qDebug() << "waitForConnected OK!";
ui->comboBox->clear();
for(int i = 0; i < m_socketList.size(); i++)
{
QString str = QString("%1 Client").arg(i);
ui->comboBox->addItem(str);
}
ui->comboBox->setCurrentIndex(0);
}
else
{
qDebug() << "waitForConnected failed!";
}
}
void Widget::on_processPendingData()
{
QTcpSocket *socket_Client = (QTcpSocket*)sender();
QByteArray datagram = socket_Client->readAll();
if (datagram.size() > 0)
{
qDebug()<<"111111111"<<QString(datagram.data());
}
}
void Widget::on_pushButton_2_clicked()
{
QString str = ui->lineEdit->text();
QByteArray ba; ba.append(str);
m_socketList.at(ui->comboBox->currentIndex())->write(ba, ba.length());
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。