浏览器代理设置带来的问题

测试代码时,发现vmware系统下的代码无法与win 下代码通讯。

为了找出原因,写了两个测试程序:

cmake_minimum_required(VERSION 3.14)

project(s_001 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core WebSockets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core WebSockets)

add_executable(s_001
  main.cpp
)
target_include_directories(s_001 PRIVATE "C:/Qt6.3/Tools/OpenSSL/Win_x64/include")
target_link_directories(s_001 PRIVATE "C:/Qt6.3/Tools/OpenSSL/Win_x64/lib")

target_link_libraries(s_001 Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::WebSockets)

install(TARGETS s_001
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
#include <QtCore/QCoreApplication>
#include <QtWebSockets/QWebSocketServer>
#include <QtWebSockets/QWebSocket>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QWebSocketServer server("My WebSocket Server", QWebSocketServer::NonSecureMode);
    QObject::connect(&server, &QWebSocketServer::newConnection, [&server]() {
        QWebSocket *socket = server.nextPendingConnection();
        QObject::connect(socket, &QWebSocket::textMessageReceived, [socket](const QString &message) {
            qDebug() << "Received message:" << message;
            socket->sendTextMessage("Server received message: " + message);
        });
    });

    if (server.listen(QHostAddress::AnyIPv4, 1234)) {
        qDebug() << "WebSocket server listening on port" << server.serverPort();
    } else {
        qDebug() << "Failed to start WebSocket server!";
    }

    return a.exec();
}

然后是client程序:

cmake_minimum_required(VERSION 3.14)

project(c_001 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core WebSockets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core WebSockets)

add_executable(c_001
  main.cpp
)
target_include_directories(c_001 PRIVATE "C:/Qt6.3/Tools/OpenSSL/Win_x64/include")
target_link_directories(c_001 PRIVATE "C:/Qt6.3/Tools/OpenSSL/Win_x64/lib")

target_link_libraries(c_001 Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::WebSockets)

install(TARGETS c_001
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
#include <QCoreApplication>
#include <QtWebSockets/QWebSocket>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QWebSocket webSocket;

    QObject::connect(&webSocket, &QWebSocket::connected, [&]() {
        qDebug() << "WebSocket connected!";
        qDebug() << webSocket.localAddress();
        webSocket.sendTextMessage("Hello, server!");
    });

    QObject::connect(&webSocket, &QWebSocket::disconnected, [&]() {
        qDebug() << "Close Reason:" <<webSocket.closeReason();
        qDebug() << webSocket.localAddress();
        qDebug() << "WebSocket disconnected!";
    });

    QObject::connect(&webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
        [=](QAbstractSocket::SocketError error)
    {
        qDebug() << "error: " << error;
    });
    QObject::connect(&webSocket, &QWebSocket::textMessageReceived, [](const QString &message) {
        qDebug() << "Received message:" << message;
    });

    QUrl url("ws://192.168.124.4:1234"); // 本机 IP 地址和端口号,不可以正常连接
    //QUrl url("ws://127.0.0.1:1234");   // 可以正常连接
    webSocket.open(url);

    return a.exec();
}

始终没有找到问题所在,意外地,还处理了ssl 连接的警告。

最后,关掉了浏览器代理,连接成功。

 

posted on 2023-02-21 18:52  金凯旋  阅读(40)  评论(0编辑  收藏  举报

导航