Qt建立TCP/IP连接

Qt 有封装好的QTcpServer和QTcpSocket类,使用比较方便。

服务器端代码:

头文件

#include 
#include 
#include 

class server : public QObject
{
    Q_OBJECT
public:
    explicit server(QObject *parent = nullptr);
    void     beingListen(QHostAddress addr = QHostAddress::Any, unsigned port = 5800);
    void     sendMessage(QByteArray msg);

private slots:
    void     connectClient();
    void     msgFromClient();
    void     disconnectClient();

private:
    QTcpServer* _server;
    QTcpSocket* _socket;
};

源文件

#include "server.h"

server::server(QObject *parent)
    : QObject(parent),
      _server(new QTcpServer()),
      _socket(nullptr)
{

}

void server::beingListen(QHostAddress addr, unsigned port)
{
    if (_server->listen(addr, port)) {
        printf("begin listening!\n");
    } else {
        printf("%s\n", _server->errorString().toLatin1().data());
    }

    connect(_server, SIGNAL(newConnection()), this, SLOT(connectClient()));
}

void server::sendMessage(QByteArray msg)
{
    _socket->write(msg);
}

void server::connectClient()
{
    _socket = _server->nextPendingConnection();
    printf("server connected client\n");

    connect(_socket, SIGNAL(readyRead()), this, SLOT(msgFromClient()));
    connect(_socket, SIGNAL(disconnected()), this, SLOT(disconnectClient()));
}

void server::msgFromClient()
{
    QByteArray str = _socket->readAll();
    printf("Message form client: %s\n", str.data());
}

void server::disconnectClient()
{
    printf("client disconnected!\n");
}
//服务端main.cpp
#include 
#include "server.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    server s;
    s.beingListen();

    return a.exec();
}

客户端代码

头文件

#include 
#include 

class client : public QObject
{
    Q_OBJECT
public:
    explicit client(QObject *parent = nullptr);
    void     connectHost(QString ip, unsigned port);
    void     sendMessage(QByteArray msg);

private slots:
    void     connected();
    void     disConnected();
    void     msgFromServer();


private:
    QTcpSocket*     _socket;
};

源文件

#include "client.h"
#include 
client::client(QObject *parent)
    : QObject(parent),
      _socket(new QTcpSocket())
{

}

void client::connectHost(QString ip, unsigned port)
{
    printf("connect host\n");
    QHostAddress addr = QHostAddress(ip);
    _socket->connectToHost(addr, port);
    connect(_socket, SIGNAL(connected()), this, SLOT(connected()));
    connect(_socket, SIGNAL(disconnected()), this, SLOT(disConnected()));
    connect(_socket, SIGNAL(readyRead()), this, SLOT(msgFromServer()));
}

void client::sendMessage(QByteArray msg)
{
    _socket->write(msg);
}

void client::connected()
{
    printf("client connected server!\n");
}

void client::disConnected()
{
    printf("client disConnected server!\n");
}

void client::msgFromServer()
{
    QByteArray msg = _socket->readAll();
    printf("message from server: %s\n", msg.data());
}

//客户端main.cpp
#include 
#include "client.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    client c;
    c.connectHost("127.0.0.1", 5800);
    c.sendMessage("dddd");
    return a.exec();
}

由于是在同一台电脑上运行的,客户端连接的hostAddress为本机地址:127.0.0.1

如果服务器端要连接多个客户端,可以用map 保存连接的socket,int为socket的编号。

//接受消息时,通过下面代码获取发送方是哪个socket
QTcpSocket *socket = static_cast<QTcpSocket*>(sender());

你可能感兴趣的:(qt,tcp/ip,c++)