Can't Qt connect to the Linux server?

1. Qt connection code

void Client::toConnect()
{
    
    
    if (isConnecting || QAbstractSocket::ConnectingState == tcpSocket->state())
        return;
    //    tcpSocket->abort();
    QString ip = SERVER_IP;
    quint16 port = SERVER_PORT;
    //	 qDebug()<<"服务器IP:"<<ip<<", 端口号:"<<port;
    tcpSocket->connectToHost(QHostAddress(ip), port); //核心语句
    //    tcpSocket->waitForConnected();
}

The Linux processing code is omitted here...


2. Problem Analysis

2.1 Whether the ip and port are filled in correctly

The function prototypes of the two functions connecting to the server in Qt:

Note: quint16 = unsigned short

(1) The first parameter: IP in the form of QString, the second parameter: the parameter of quint16, and the last two parameters do not need to be filled.

[virtual] void QAbstractSocket::connectToHost(
						const QString &hostName, 
						quint16 port, 
						QIODevice::OpenMode openMode = ReadWrite, 
						QAbstractSocket::NetworkLayerProtocol protocol = AnyIPProtocol)

(2) The first parameter: IP in the form of QHostAddress, the second parameter: the parameter of quint16 (quint16 = unsigned short), and the last two parameters do not need to be filled.

[virtual] void QAbstractSocket::connectToHost(
						const QHostAddress &address, 
						quint16 port, 
						QIODevice::OpenMode openMode = ReadWrite)

2.2 Whether the code processing of Linux is correct

Here I write several client test tests on Linux


2.3 The Linux server port is not open (this is very important!)

There are several ways to refuse

Method 1: Open the port once and for all

First list the commands that need to be used:

(1) View all open ports

firewall-cmd --list-port

(2) Check whether a port is open

firewall-cmd --query-port=端口号/协议(tcp/udp)

(3) Open the port

firewall-cmd --zone=public --add-port=端口号/协议(tcp/udp) --permanent

(4) Remove the port

firewall-cmd --zone=public --remove-port=端口号/协议(tcp/udp) --permanent

(5) Restart the firewall

firewall-cmd --reload

Example: For example, I want to open port 7799

Step 1: Open port 7799 for TCP communication

firewall-cmd --zone=public --add-port=7799/tcp --permanent

Step 2: Restart the firewall

firewall-cmd --reload

Step 3: Add the firewall rules of the server, if you do not do this step, you will not be able to connect

insert image description here

Step analysis:

1) If you do not do the third step, you will not be able to connect, because the firewall rules of the server have not been added.

2) If you only do the third step, you will not be able to connect, because you firewall-cmd --list-portcannot find the firewall rules added by the server.

Method 2: Turn off the firewall and open the port

Turn off the firewall command:systemctl stop firewalld

or

Directly disable firewall self-start: systemctl disable firewalld— prevent firewall self-start from causing ports to be unusable

Recovery operation:

Start the firewall command:systemctl start firewalld

Enable the firewall to start automatically at boot time:systemctl enable firewalld


Guess you like

Origin blog.csdn.net/CSDN_Yuanyuan/article/details/129573415