Qt Tcp服务器程序(可群发消息,定向IP发送消息)


1、用到的头文件

#include<QTcpServer>
#include<QTcpSocket>
#include<QHostAddress>

  

2、pro文件添加 QT += network

 

3、创建QTcpSocket的子类,tcpClient,用于接收客户端行为

 1 #ifndef CLIENT_H
 2 #define CLIENT_H
 3 
 4 #include<QTcpSocket>
 5 
 6 class Client : public QTcpSocket
 7 {
 8     Q_OBJECT
 9 public:
10     Client(QObject * parent = 0);
11 
12 protected slots:
13     void receiveData();     // signal readyRead() 触发该槽
14     void slotClientDisconnected();      // signal disconnected() 触发该槽
15 
16 signals:
17     void updateServer(QString);     // 该信号发送给tcpServer类
18     void clientDisconnected(qintptr);   // 该信号发送给tcpServer类
19 };
20 
21 #endif // CLIENT_H
View Code
 1 #include "tcpClient.h"
 2 
 3 Client::Client(QObject * parent):QTcpSocket(parent)
 4 {
 5     connect(this,&QTcpSocket::readyRead,this,&Client::receiveData);
 6     connect(this,&QTcpSocket::disconnected,this,&Client::slotClientDisconnected);
 7 
 8 }
 9 
10 void Client::receiveData()
11 {
12     QByteArray array = readAll();
13     QString msg = array;
14     emit updateServer(msg);
15 }
16 
17 void Client::slotClientDisconnected()
18 {
19     emit clientDisconnected(this->socketDescriptor());
20 }
View Code  
 

 4、创建QTcpServer的子类,tcpServer,用于更新客户端连接、与tcpClient交互等等

 1 #ifndef SERVER_H
 2 #define SERVER_H
 3 
 4 #include<QTcpServer>
 5 #include<QTcpSocket>
 6 #include<QHostAddress>
 7 #include<QList>
 8 #include"tcpClient.h"
 9 
10 class Server : public QTcpServer
11 {
12     Q_OBJECT
13 public:
14     Server(QObject *parent = 0 ,QString IP = "" ,quint16 port = 0);
15     QTcpSocket * deleteSocket;
16     quint16 qlistCount();
17     void sendMessage(QByteArray msg, int i);
18 
19 protected:
20     void incomingConnection(qintptr socketDescriptor) override;     // 只要出现一个新的连接,就会自动调用这个函数
21     QList<QTcpSocket*>clientList;
22     Client * client;
23     QTcpSocket * temp;
24 
25 protected slots:
26     void slotUpdateServer(QString);     // signal来自tcpClient
27     void slotClientDisconnected(qintptr);   // signal来自tcpClient
28 signals:
29     void updateServer(QString);     // 该信号发送给文本框
30     void newClientInfo(QHostAddress clientAddress,quint16 port);    // 该信号发送给文本框
31     void deleteClient(QHostAddress clientAddress,quint16 port);     // 该信号发送给文本框
32 
33 private:
34 
35 
36 };
37 
38 #endif // SERVER_H
View Code
 1 #include "tcpServer.h"
 2 #include<QDebug>
 3 
 4 Server::Server(QObject *parent, QString IP ,quint16 port):QTcpServer(parent)
 5 {
 6     QHostAddress address(IP);
 7     this->listen(address,port);
 8 }
 9 
10 quint16 Server::qlistCount()
11 {
12     return clientList.count();
13 }
14 
15 void Server::sendMessage(QByteArray msg, int i)
16 {
17     temp = new QTcpSocket(this);
18     temp = clientList.at(i);
19     temp->write(msg);
20 }
21 
22 void Server::incomingConnection(qintptr socketDescriptor)
23 {
24     client = new Client(this);
25 
26     //将新创建的通信套接字描述符指定为参数socketdescriptor
27     client->setSocketDescriptor(socketDescriptor);
28 //    qDebug()<<socketDescriptor<<QString::fromLocal8Bit("连接");
29     // 将这个套接字加入客户端套接字列表中
30     clientList.append(client);
31 
32     emit newClientInfo(client->peerAddress(),client->peerPort());
33 //    qDebug()<<client->peerAddress()<<client->peerPort();
34 
35 
36     connect(client,&Client::updateServer,this,&Server::slotUpdateServer);
37     connect(client,&Client::clientDisconnected,this,&Server::slotClientDisconnected);
38 }
39 
40 void Server::slotUpdateServer(QString msg)
41 {
42     // 将这个信号发送给文本框
43     emit updateServer(msg);
44 }
45 
46 void Server::slotClientDisconnected(qintptr socketDescriptor)
47 {
48     // 判断断开的客户端,并将其从表中移除
49     for(int i = 0; i< clientList.count(); i++)
50     {
51         deleteSocket = clientList.at(i);
52         if(socketDescriptor == deleteSocket->socketDescriptor())
53         {
54             emit deleteClient(deleteSocket->peerAddress(),deleteSocket->peerPort());
55 //            qDebug()<<deleteSocket->peerAddress()<<deleteSocket->peerPort()
56 //                   <<QString::fromLocal8Bit("断开");
57             clientList.removeAt(i);
58             return;
59         }
60     }
61 }
View Code

 

**注意

incomingConnection是虚函数,必须继承重写

 

5、创建一个管理类,tcp_Server,用于界面交互整合信息等等

界面:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM