QT开发中文网

用qt写一个tcp连接客户端接收消息

以下是一个使用Qt编写的TCP客户端,可以接收来自服务器的消息:

#include <QtNetwork>
#include <QtWidgets>

class TcpClient : public QWidget
{
    Q_OBJECT

public:
    TcpClient(QWidget *parent = nullptr);

private slots:
    void connectToServer();
    void readData();
    void displayError(QAbstractSocket::SocketError socketError);

private:
    QLineEdit *hostLineEdit;
    QLineEdit *portLineEdit;
    QTextEdit *messageTextEdit;
    QPushButton *connectButton;
    QTcpSocket *tcpSocket;
};

TcpClient::TcpClient(QWidget *parent)
    : QWidget(parent)
{
    QLabel *hostLabel = new QLabel(tr("Host name:"));
    hostLineEdit = new QLineEdit("localhost");
    QLabel *portLabel = new QLabel(tr("Port number:"));
    portLineEdit = new QLineEdit("1234");
    connectButton = new QPushButton(tr("Connect"));
    connectButton->setDefault(true);

    QHBoxLayout *topLayout = new QHBoxLayout;
    topLayout->addWidget(hostLabel);
    topLayout->addWidget(hostLineEdit);
    topLayout->addWidget(portLabel);
    topLayout->addWidget(portLineEdit);
    topLayout->addWidget(connectButton);

    messageTextEdit = new QTextEdit;
    messageTextEdit->setReadOnly(true);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    mainLayout->addWidget(messageTextEdit);
    setLayout(mainLayout);

    tcpSocket = new QTcpSocket(this);
    connect(tcpSocket, &QTcpSocket::readyRead, this, &TcpClient::readData);
    connect(tcpSocket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error),
            this, &TcpClient::displayError);

    connect(connectButton, &QPushButton::clicked, this, &TcpClient::connectToServer);
}

void TcpClient::connectToServer()
{
    QString hostName = hostLineEdit->text();
    quint16 port = portLineEdit->text().toUShort();

    tcpSocket->connectToHost(hostName, port);
}

void TcpClient::readData()
{
    QByteArray data = tcpSocket->readAll();
    messageTextEdit->append(data);
}

void TcpClient::displayError(QAbstractSocket::SocketError socketError)
{
    switch (socketError) {
    case QAbstractSocket::RemoteHostClosedError:
        break;
    case QAbstractSocket::HostNotFoundError:
        QMessageBox::information(this, tr("TCP Client"),
                                 tr("The host was not found. Please check the "
                                    "host name and port settings."));
        break;
    case QAbstractSocket::ConnectionRefusedError:
        QMessageBox::information(this, tr("TCP Client"),
                                 tr("The connection was refused by the peer. "
                                    "Make sure the server is running, "
                                    "and check that the host name and port "
                                    "settings are correct."));
        break;
    default:
        QMessageBox::information(this, tr("TCP Client"),
                                 tr("The following error occurred: %1.")
                                 .arg(tcpSocket->errorString()));
    }
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    TcpClient client;
    client.show();
    return app.exec();
}

#include "main.moc"

该客户端使用Qt的QTcpSocket类来与服务器建立TCP连接,并在接收到数据时将其显示在QTextEdit小部件中。在connectToServer()函数中,我们使用hostLineEdit和portLineEdit小部件中的值来连接到服务器。在readData()函数中,我们使用tcpSocket->readAll()读取来自服务器的所有数据。在displayError()函数中,我们处理与连接错误相关的各种情况。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=1085

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?