📄 server.cpp
字号:
#include <QtNetwork>#include "server.h"Server::Server(QObject *parent,int port) : QTcpServer(parent){ listen(QHostAddress::Any,port);}void Server::incomingConnection(int socketDescriptor){ TcpClientSocket *tcpClientSocket = new TcpClientSocket(this); connect(tcpClientSocket,SIGNAL(updateClients(QString,int)),this,SLOT(updateClients(QString,int))); connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnected(int))); tcpClientSocket->setSocketDescriptor(socketDescriptor); tcpClientSocketList.append(tcpClientSocket);}void Server::updateClients(QString msg,int length){ emit updateServer(msg,length); for(int i=0;i<tcpClientSocketList.count();i++) { QTcpSocket *item=tcpClientSocketList.at(i); if(item->write(msg.toLatin1(), length)!=length) { continue ; } }}void Server::slotDisconnected(int descriptor){ for(int i=0;i<tcpClientSocketList.count();i++) { QTcpSocket *item=tcpClientSocketList.at(i); if(item->socketDescriptor ()==descriptor) { tcpClientSocketList.removeAt(i); return; } } return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -