【问题标题】:Qt cannot connect to the serverQt 无法连接到服务器
【发布时间】:2014-11-04 23:10:59
【问题描述】:

我正在尝试创建一个简单的服务器,它只会在有人连接到我时显示。当客户端和服务器使用“localhost”作为主机名进行连接时,一切正常,但是当我将 localhost 更改为我的 IP 地址时,出现超时错误:( 有我的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
    server_status=0;
}

void MainWindow::on_starting_clicked()
{
    tcpServer = new QTcpServer(this);
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newuser()));
    if (!tcpServer->listen(QHostAddress::Any, 33333) && server_status==0) {
        qDebug() <<  QObject::tr("Unable to start the server: %1.").arg(tcpServer->errorString());
        ui->textinfo->append(tcpServer->errorString());
    } else {
        server_status=1;
        qDebug() << tcpServer->isListening() << "TCPSocket listen on port";
        ui->textinfo->append(QString::fromUtf8("Server started!"));
        qDebug() << QString::fromUtf8("Server started!");
    }
}

void MainWindow::on_stoping_clicked()
{
    if(server_status==1){
        foreach(int i,SClients.keys()){
            QTextStream os(SClients[i]);
            os.setAutoDetectUnicode(true);
            os << QDateTime::currentDateTime().toString() << "\n";
            SClients[i]->close();
            SClients.remove(i);
        }
        tcpServer->close();
        ui->textinfo->append(QString::fromUtf8("Server stopped!"));
        qDebug() << QString::fromUtf8("Server stopped!");
        server_status=0;
    }
}


void MainWindow::newuser()
{
    if(server_status==1){
        qDebug() << QString::fromUtf8("New connection!");
        ui->textinfo->append(QString::fromUtf8("New connection!"));
        QTcpSocket* clientSocket=tcpServer->nextPendingConnection();
        int idusersocs=clientSocket->socketDescriptor();
        SClients[idusersocs]=clientSocket;
        connect(SClients[idusersocs],SIGNAL(readyRead()),this, SLOT(slotReadClient()));
    }
}

对于客户:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    socket = new QTcpSocket(this);
    socket->connectToHost("95.220.162.117", 33333);
    socket->waitForConnected();
}

MainWindow::~MainWindow()
{
    delete ui;
}

我是网络领域的新手,所以请解释一下我做错了什么

【问题讨论】:

  • 听起来防火墙正在阻止您。服务器在什么操作系统上?
  • OSX 10.9 且防火墙已关闭
  • 当使用 localhost 连接时,防火墙规则通常允许一切通过回到自身。从外部网络接口连接时 - 防火墙规则有助于防止入侵。端口 33333 是您必须在防火墙设置中取消阻止的端口。这个Apple how to可以帮你打开端口使用。
  • 啊错过了“防火墙已关闭”哎呀。我刚看到 OS/X ;-) 。我假设您的意思是服务器上的防火墙已关闭?如果它位于公共互联网上,可能不是一个好主意

标签: qt network-programming


【解决方案1】:

“95.220.162.117”看起来像一个公共欧洲 IP 地址。此公共 IP 地址由所有计算机/平板电脑/电话/等共享。在你的家/办公室。可以这样想:公共 IP 地址指向您的路由器,而不是特定的计算机。

当客户端向公共 IP 地址发送请求时,路由器首先接收请求。然后路由器的工作就是将请求转发到正确的设备......但在你的情况下,你的路由器不知道哪个设备应该接收请求!

连接工作有两个层次:

1.私有:在您的本地网络内(更简单)

在您的服务器上,打开您的控制台并输入以下内容以查找您的私有/本地 IP 地址:

ifconfig -a

您的本地 IP 地址与您的公共 IP 地址不同。它标识本地网络上的特定计算机。让你的客户端连接到这个地址,你的服务器应该会收到请求。

这仅适用于客户端和服务器在同一个本地网络上(例如连接到同一个路由器)

2。公开:通过互联网(更难)

您需要设置Port Forwarding -- 这告诉您的路由器接收在端口 33333 收到的请求并将它们转发到您的服务器。

要了解有关此主题的更多信息,请阅读Network Address Translation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    相关资源
    最近更新 更多