From 22b584b3b40ec0a4995b8e46b59b1c6c202eaa2b Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Thu, 15 Nov 2018 10:04:55 +0100 Subject: [PATCH] simulator: fix TCP connection init on macOS When `connect()` fails, the state of the socket is unspecified and we need to close the it and create it again. It turns out retrying connect worked on Linux but it didn't on macOS. --- src/modules/simulator/simulator_mavlink.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/modules/simulator/simulator_mavlink.cpp b/src/modules/simulator/simulator_mavlink.cpp index c87bf304b5..a4a466c234 100644 --- a/src/modules/simulator/simulator_mavlink.cpp +++ b/src/modules/simulator/simulator_mavlink.cpp @@ -683,20 +683,22 @@ void Simulator::pollForMAVLinkMessages(bool publish, InternetProtocol ip, int po PX4_INFO("Simulator connected on UDP port %u.", port); } else { - if ((_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - PX4_ERR("Creating TCP socket failed: %s", strerror(errno)); - return; - } PX4_INFO("Waiting for simulator to connect on TCP port %u", port); while (true) { + if ((_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + PX4_ERR("Creating TCP socket failed: %s", strerror(errno)); + return; + } + int ret = connect(_fd, (struct sockaddr *)&_myaddr, sizeof(_myaddr)); if (ret == 0) { break; } else { + close(_fd); system_sleep(1); } }