From 578fc92ffdf709ecdf9f2d8ef56bc87157cca80a Mon Sep 17 00:00:00 2001 From: Sebastian Doell Date: Tue, 19 Mar 2024 14:27:22 +0100 Subject: [PATCH] [NXDRVLINUX-149] - Fix server/client connection establishment failure due to a time consuming (>timeout on client side) call of getnameinfo(). --- examples/tcpserver/tcp_connector.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/tcpserver/tcp_connector.c b/examples/tcpserver/tcp_connector.c index adcf977..4bec32c 100644 --- a/examples/tcpserver/tcp_connector.c +++ b/examples/tcpserver/tcp_connector.c @@ -233,6 +233,16 @@ void* ServerThread(void* pvParam) /* print host-ip */ printf("Connected with client : %s\n", inet_ntoa(tSockAddr.sin_addr)); + /* NOTE: To prevent timeout on client side, as getnameinfo() may take */ + /* more time, first start client thread then retrieve host name. */ + if ( setsockopt(hClient, IPPROTO_TCP, TCP_NODELAY, (void*)&iNoDelay, sizeof(iNoDelay)) != 0 ) + { + printf("The server is not able to send small packets.\n"); + printf("So the communication could be very slow!\n"); + } + ptTcpData->hClient = hClient; + pthread_create(&ptTcpData->hClientThread, NULL,ClientThread, (void*)ptTcpData); + /* Query remote name */ if (0 == getnameinfo( (struct sockaddr *) &tSockAddr, sizeof (struct sockaddr), szHostname,NI_MAXHOST, NULL, 0, 0)) @@ -243,16 +253,6 @@ void* ServerThread(void* pvParam) { printf("Host name unknown!\n"); } - - if ( setsockopt(hClient, IPPROTO_TCP, TCP_NODELAY, (void*)&iNoDelay, sizeof(iNoDelay)) != 0 ) - { - printf("The server is not able to send small packets.\n"); - printf("So the communication could be very slow!\n"); - } - - ptTcpData->hClient = hClient; - pthread_create(&ptTcpData->hClientThread, NULL,ClientThread, (void*)ptTcpData); - } } }