在UBUNTU20.04,QT5写了一个TCP服务器端的小程序。左边是Client,右边是server。
1.服务器端可以连接多个客户端。
2.服务器接收各个客户端的写入。
3.服务器向所有客户端发送数据。
实现主要两个类:
QTcpServer:用来管理server ,每个server,可以有很多个socket.
QTcpSocket : 数据传输
demo
//QTcpServer 用来监听端口,启动服务
listen(QHostAddress::Any, port)
newConnection()
onServerDataReady()
m_server.findChildren()
QTcpSocket 用来进行数据交换
connectToHost();
readyRead();
bytesWritten();
以下是CPP源码
#include "mytcpserver.h"
#include "ui_mytcpserver.h"
#include<QMessageBox>
myTcpServer::myTcpServer(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::myTcpServer)
{
ui->setupUi(this);
ptcpserver_=new QTcpServer();
initServerSignals();
}
void myTcpServer::initServerSignals() //初始化信号槽
{
connect(ptcpserver_, SIGNAL(newConnection()), this, SLOT(onServerNewConnection_slot()));
}
void myTcpServer::on_startServerBt_clicked()
{
qDebug() << "on_startServerBt_clicked:" ;
ptcpserver_->listen(QHostAddress::Any,ui->portEdit->text().toUInt());
}
void myTcpServer::onServerNewConnection_slot()
{
qDebug() << "onServerNewConnection";
QTcpSocket* ptcpsocket = ptcpserver_->nextPendingConnection();
QString info=ptcpsocket->peerAddress().toString()+":"+QString("%1").arg(ptcpsocket->peerPort());
QMessageBox::information(this,"提示",QString("新的客户端连入:%1").arg(info),QMessageBox::Ok);
ptcpsocket->setObjectName(info); //设置名称,方便查找
connect(ptcpsocket, SIGNAL(connected()), this, SLOT(onServerConnected_slot()));
connect(ptcpsocket, SIGNAL(disconnected()), this, SLOT(onServerDisconnected_slot()));
connect(ptcpsocket, SIGNAL(readyRead()), this, SLOT(onServerDataReady_slot()));
connect(ptcpsocket, SIGNAL(bytesWritten(qint64)), this, SLOT(onServerBytesWritten_slot(qint64)));
}
void myTcpServer::onServerConnected_slot()
{
}
void myTcpServer::onServerDisconnected_slot()
{
QTcpSocket* ptcpsocket = dynamic_cast<QTcpSocket*>(sender());
if( ptcpsocket != NULL ) //从连接对象中移除掉
{
qDebug() << "onServerDisconnected";
qDebug() << "Local Address:" << ptcpsocket->peerAddress();
qDebug() << "Local Port:" << ptcpsocket->peerPort();
QString info=ptcpsocket->peerAddress().toString()+":"+QString("%1").arg(ptcpsocket->peerPort());
QMessageBox::information(this,"提示",QString("客户端断开连接:%1").arg(info),QMessageBox::Ok);
}
}
void myTcpServer::onServerDataReady_slot()
{
/* QString buf;
buf =ptcpsocket_->readAll();
ui->recvEdit->appendPlainText(buf);*/
//ui->recvEdit->insertPlainText(QString::fromLocal8Bit(ptcpsocket_->readAll())+"\r\n");
QTcpSocket* ptcpsocket = dynamic_cast<QTcpSocket*>(sender());
ui->recvEdit->insertPlainText("[接受来自"+ ptcpsocket->peerAddress().toString()+":"+QString("%1").arg(ptcpsocket->peerPort())+"]:\r\n");
ui->recvEdit->moveCursor(QTextCursor::End);
ui->recvEdit->insertPlainText(QString::fromLocal8Bit(ptcpsocket->readAll())+"\r\n");
}
void myTcpServer::onServerBytesWritten_slot(qint64 bytes)
{
qDebug() << "onServerBytesWritten_slot:" << bytes;
// ui->sendLenLabel->setText(QString("%1").arg(ui->sendLenLabel->text().toInt()+bytes));
}
myTcpServer::~myTcpServer()
{
delete ui;
QList<QTcpSocket *> m_tcps = ptcpserver_->findChildren<QTcpSocket *>();
foreach (QTcpSocket *tcp, m_tcps)
{
tcp->close();
}
}
void myTcpServer::on_closeBt_clicked()
{
ptcpserver_->close();
QList<QTcpSocket *> m_tcps = ptcpserver_->findChildren<QTcpSocket *>();
foreach (QTcpSocket *tcp, m_tcps)
{
tcp->close();
}
}
void myTcpServer::on_sendBt_clicked()
{
qDebug() << "on_sendBt_clicked" ;
// ptcpsocket_->write(ui->sendEdit->text().toLocal8Bit().data());
QList<QTcpSocket *> m_tcps = ptcpserver_->findChildren<QTcpSocket *>();
foreach (QTcpSocket *tcp, m_tcps)
{
tcp->write(ui->sendEdit->text().toLocal8Bit().data());
}
}
void myTcpServer::on_clearBt_clicked()
{
}