Hello,
I am developing an X-Plane plugin that communicates with the PX4 Software In The Loop (SITL) simulator using a TCP connection. However, I am encountering an issue where the connection fails without any specific error message.
Here’s the relevant code snippet from my plugin:
1void ConnectionManager::connectToSITL(const std::string& sitlIp) {
2 if (connected) return;
3
4 ConnectionManager::sockfd = socket(AF_INET, SOCK_STREAM, 0);
5 if (ConnectionManager::sockfd < 0) {
6 XPLMDebugString("px4xplane: Error opening socket\n");
7 return;
8 }
9
10 sockaddr_in serv_addr{};
11 serv_addr.sin_family = AF_INET;
12 serv_addr.sin_port = htons(ConnectionManager::sitlPort);
13 if (inet_pton(AF_INET, sitlIp.c_str(), &serv_addr.sin_addr) <= 0) {
14 XPLMDebugString("px4xplane: Invalid address or address not supported\n");
15 return;
16 }
17
18 if (connect(ConnectionManager::sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
19 char errorMsg[128];
20 snprintf(errorMsg, sizeof(errorMsg), "px4xplane: Connection failed. Error: %s\n", strerror(errno));
21 XPLMDebugString(errorMsg);
22 return;
23 }
24
25 XPLMDebugString("px4xplane: Connected to SITL\n");
26
27 connected = true;
28 status = "Connected";
29 setLastMessage("Connected to SITL successfully.");
30}
When I try to connect to the SITL, I get the following message in the log: “px4xplane: Connection failed. Error: No error”. The SITL is running on a different machine (a WSL node) with the IP address 172.21.170.14
.
I have also tried using the IP address 0.0.0.0
, but the connection still fails. When I use 0.0.0.0
, nothing happens, and when I use 172.21.170.14
, X-Plane freezes for a few seconds and then resumes, but the connection is not established.
On my WSL, I run the command px4_sitl none_iris
and I see the following output:
1INFO [simulator_mavlink] using TCP on remote host 172.21.160.1 port 4560
2WARN [simulator_mavlink] Please ensure port 4560 is not blocked by a firewall.
3INFO [simulator_mavlink] Resolved host '172.21.160.1' to address: 172.21.160.1
4INFO [simulator_mavlink] Waiting for simulator to accept connection on TCP port 4560
I have also set the environment variable PX4_SIM_HOSTNAME=172.21.160.1
.
I have checked that the SITL is running and listening on the correct port, and there are no firewalls or network issues that could be blocking the connection.
I would appreciate any help or suggestions on how to resolve this issue. Thank you.