QTcpSocket 连接

发布于 2024-11-09 10:43:45 字数 1461 浏览 0 评论 0原文

我正在尝试使用 QTcpSocket 和 QTcpServer 编写聊天。 我的几段代码

客户端

ChatClient::ChatClient(QObject *parent)
    : QObject(parent) {
    connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(error(QAbstractSocket::SocketError)));
    connect(&tcpSocket, SIGNAL(connected()),
            this, SLOT(requestForID()));
    connect(&tcpSocket, SIGNAL(readyRead()),
            this, SLOT(receiveMessage()));
    tcpSocket.connectToHost(QHostAddress::LocalHost, PORT);
}

void ChatClient::requestForID() {
    qDebug() << "Connected, requesting for ID";
    QByteArray segment;
    QDataStream out(segment);
    out.setVersion(QDataStream::Qt_4_7);
    out << (quint16)0 << ID;
    out.device()->seek(0);
    out << (quint16)(segment.size() - sizeof(quint16));
    tcpSocket.write(segment);
}

void ChatClient::error(QAbstractSocket::SocketError error) {
    qDebug() << "Socket error" << error;
}

服务器

ChatServer::ChatServer(QObject *parent)
    : QObject(parent) {
    if (!tcpServer.listen(QHostAddress::LocalHost, PORT)) {
        qDebug() << "Unable to start the server"
                 << tcpServer.errorString();
    }
    connect(&tcpServer, SIGNAL(newConnection()),
            this, SLOT(processConnection()));
}

客户端套接字永远无法连接。错误永远不会被打印。 端口=6178。 运行 KUbuntu。从 bash Ping 到 localhost 成功。 我做错了什么?

I'm trying to write a chat using QTcpSocket and QTcpServer.
Several pieces of my code

Client

ChatClient::ChatClient(QObject *parent)
    : QObject(parent) {
    connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(error(QAbstractSocket::SocketError)));
    connect(&tcpSocket, SIGNAL(connected()),
            this, SLOT(requestForID()));
    connect(&tcpSocket, SIGNAL(readyRead()),
            this, SLOT(receiveMessage()));
    tcpSocket.connectToHost(QHostAddress::LocalHost, PORT);
}

void ChatClient::requestForID() {
    qDebug() << "Connected, requesting for ID";
    QByteArray segment;
    QDataStream out(segment);
    out.setVersion(QDataStream::Qt_4_7);
    out << (quint16)0 << ID;
    out.device()->seek(0);
    out << (quint16)(segment.size() - sizeof(quint16));
    tcpSocket.write(segment);
}

void ChatClient::error(QAbstractSocket::SocketError error) {
    qDebug() << "Socket error" << error;
}

Server

ChatServer::ChatServer(QObject *parent)
    : QObject(parent) {
    if (!tcpServer.listen(QHostAddress::LocalHost, PORT)) {
        qDebug() << "Unable to start the server"
                 << tcpServer.errorString();
    }
    connect(&tcpServer, SIGNAL(newConnection()),
            this, SLOT(processConnection()));
}

Client socket never gets connected. Error is never being printed.
PORT = 6178.
Runnig KUbuntu. Ping to localhost from bash is successful.
What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼睛会笑 2024-11-16 10:43:45

我在你的代码中没有看到任何错误,你确定你的 Qt 和“网络”工作正常吗? Qt 应该会发出错误,但至少你这里的代码片段对我来说看起来是正确的。也许您的代码永远不会被调用,请向方法添加一些调试消息。

最后,您可以构建 Qt 网络示例并测试它是否在您的计算机上运行。如果您没有示例,请查看此处:http://doc。 qt.io/qt-5/examples-network.html(Fortune TCP 服务器/客户端)

I don't see any errors in your code, are you sure your Qt and "network" is working properly? Qt should emit an error but at least your code pieces here look correct to me. Maybe your code never gets called, add some debug messages to the methods.

At last you can build the Qt Network examples and test if that is working on your machine. If you don't have the examples take a look here: http://doc.qt.io/qt-5/examples-network.html (Fortune Server/Client for TCP)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文