diff --git a/src/c/profile/transport/tcp/tcp_transport_linux.c b/src/c/profile/transport/tcp/tcp_transport_linux.c index 1aea8137..66eb6bee 100644 --- a/src/c/profile/transport/tcp/tcp_transport_linux.c +++ b/src/c/profile/transport/tcp/tcp_transport_linux.c @@ -47,7 +47,25 @@ bool uxr_init_tcp_platform(struct uxrTCPPlatform* platform, const char* ip, uint bool uxr_close_tcp_platform(struct uxrTCPPlatform* platform) { - return (-1 == platform->poll_fd.fd) ? true : (0 == close(platform->poll_fd.fd)); + if (-1 == platform->poll_fd.fd) + { + return true; + } + + /* Synchronize the stream with the agent, to avoid losing bytes currently in flight. */ + shutdown(platform->poll_fd.fd, SHUT_WR); + fd_set read_fdset; + struct timeval timeout = { + 120, + 0 + }; + FD_ZERO(&read_fdset); + FD_SET(platform->poll_fd.fd, &read_fdset); + if (select(platform->poll_fd.fd + 1, &read_fdset, NULL, NULL, &timeout) > 0) { + char dummy; + while (recv(platform->poll_fd.fd, &dummy, sizeof(dummy), 0) > 0) {}; + } + return (0 == close(platform->poll_fd.fd)); } size_t uxr_write_tcp_data_platform(struct uxrTCPPlatform* platform, diff --git a/src/c/profile/transport/tcp/tcp_transport_windows.c b/src/c/profile/transport/tcp/tcp_transport_windows.c index a36fdc2d..6335422f 100644 --- a/src/c/profile/transport/tcp/tcp_transport_windows.c +++ b/src/c/profile/transport/tcp/tcp_transport_windows.c @@ -39,8 +39,31 @@ bool uxr_init_tcp_platform(struct uxrTCPPlatform* platform, const char* ip, uint bool uxr_close_tcp_platform(struct uxrTCPPlatform* platform) { - bool rv = (INVALID_SOCKET == platform->poll_fd.fd) ? true : (0 == closesocket(platform->poll_fd.fd)); - return (0 == WSACleanup()) && rv; + bool rv = false; + + if (INVALID_SOCKET == platform->poll_fd.fd) + { + return true; + } + + /* Synchronize the stream with the agent, to avoid losing bytes currently in flight. */ + shutdown(platform->poll_fd.fd, SD_SEND); + fd_set read_fdset; + struct timeval timeout = { + 120, + 0 + }; + FD_ZERO(&read_fdset); + FD_SET(platform->poll_fd.fd, &read_fdset); + if (select(platform->poll_fd.fd + 1, &read_fdset, NULL, NULL, &timeout) > 0) { + char dummy; + while (recv(platform->poll_fd.fd, &dummy, sizeof(dummy), 0) > 0) {}; + } + if (0 == closesocket(platform->poll_fd.fd)) + { + rv = (0 == WSACleanup()); + } + return rv; } size_t uxr_write_tcp_data_platform(struct uxrTCPPlatform* platform,