server.cpp
来自「Linux窗口程序设计,Qt4精彩实例分析,以循序渐进的方式介绍Qt4开发及其实」· C++ 代码 · 共 50 行
CPP
50 行
#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 + =
减小字号Ctrl + -
显示快捷键?