外传篇 4 - TCP 服务端编程------------------狄泰软件学院

it2023-08-05  77

一、TCP 服务端编程

二、TCP 服务端编程类的调用

三、TCP服务端编程介绍

四、QTcpServer的使用方式

五、注意事项

六、服务端和客户端的交互流程

七、示例

ServerDemo.cpp

#include "ServerDemo.h" #include <QHostAddress> #include <QTcpSocket> #include <QObjectList> #include <QDebug> ServerDemo::ServerDemo(QObject* parent) : QObject(parent) { connect(&m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection())); } void ServerDemo::onNewConnection() { qDebug() << "onNewConnection"; QTcpSocket* tcp = m_server.nextPendingConnection(); connect(tcp, SIGNAL(connected()), this, SLOT(onConnected())); connect(tcp, SIGNAL(disconnected()), this, SLOT(onDisconnected())); connect(tcp, SIGNAL(readyRead()), this, SLOT(onDataReady())); connect(tcp, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64))); } void ServerDemo::onConnected() { QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender()); if( tcp != NULL ) { qDebug() << "onConnected"; qDebug() << "Local Address:" << tcp->localAddress(); qDebug() << "Local Port:" << tcp->localPort(); } } void ServerDemo::onDisconnected() { qDebug() << "onDisconnected"; } void ServerDemo::onDataReady() { QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender()); char buf[256] = {0}; if( tcp != NULL ) { qDebug() << "onDataReady:" << tcp->read(buf, sizeof(buf)-1); qDebug() << "Data:" << buf; } } void ServerDemo::onBytesWritten(qint64 bytes) { qDebug() << "onBytesWritten:" << bytes; } bool ServerDemo::start(int port) { bool ret = true; if( !m_server.isListening() ) { ret = m_server.listen(QHostAddress("127.0.0.1"), port); } return ret; } void ServerDemo::stop() { if( m_server.isListening() ) { m_server.close(); } } ServerDemo::~ServerDemo() { const QObjectList& list = m_server.children(); for(int i=0; i<list.length(); i++) { QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(list[i]); if( tcp != NULL ) { tcp->close(); } } }

 ServerDemo.h

#ifndef SERVERDEMO_H #define SERVERDEMO_H #include <QObject> #include <QTcpServer> class ServerDemo : public QObject { Q_OBJECT QTcpServer m_server; public: ServerDemo(QObject* parent = NULL); bool start(int port); void stop(); ~ServerDemo(); protected slots: void onNewConnection(); void onConnected(); void onDisconnected(); void onDataReady(); void onBytesWritten(qint64 bytes); }; #endif // SERVERDEMO_H

八、小结

最新回复(0)