+TCP. TCP connection is not established

I use the +TCP library. I’m trying to establish a TSP connection between a client and a server for the first time.
On the client side, the FreeRTOS_connect function always returns -128. pdFREERTOS_ERRNO_ENOTCONN
On the server side, the FreeRTOS_accept function always returns on timeout.

IP addresses are configured, different.
MAC addresses are configured, different.
Via UDP the boards exchange packets.

TCP connection is not established.

Did you already had a look with Wireshark what’s going on ?

Hi @hs2
There are also problems with viewing via Wareshark. The two boards I’m trying to connect to have the addresses 192.168.0.1 and 192.168.0.2. The laptop on which Wareshark is installed has the address 192.168.0.3. These three devices are connected via a router. Unfortunately, Wareshark does not see the exchange between …1 and …2. I send debug information from the client and server via UDP to …3. I see it in Wareshark.

I have this question too. How do I set up a router or what device should I use so that direct exchange between devices 192.168.0.1 and 192.168.0.2 is visible on the wareshark at 192.168.0.3?

How do I set up a router or what device should I use so that direct exchange between devices 192.168.0.1 and 192.168.0.2 is visible on the wareshark at 192.168.0.3?

One way to capture data traffic between two devices that doesn’t have Wireshark installed is to use a managed switch with port mirroring. [Example: How to Configure Port Mirror on TP-Link Smart Switches and L2 Managed Switches]

But for easy debugging purposes, you can connect your device directly to your PC, which has Wireshark installed. You can probably run a python echo server in the Windows machine and make your board connect to your PC.

On the client side, the FreeRTOS_connect function always returns -128. pdFREERTOS_ERRNO_ENOTCONN

Did you take a look at the TCP echo client demo that show cases usage of FreeRTOS+TCP to connect to a echo server: FreeRTOS/FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_IPv6_Demo/IPv6_Multi_WinSim_demo/TCPEchoClient_SingleTasks.c at main · FreeRTOS/FreeRTOS · GitHub

You should first verify that the network stack on your device is operational. One approach to test it would be to:

  1. Set up a TCP server on your PC.
  2. Program your device to act as a TCP client.
  3. Attempt to establish a connection between the device and your PC.

If the connection fails, you can capture Wireshark data on your PC since the server is running there.

Thanks for the advice. I’m on a break for now. When I return to research, I will make a server on Windows. I will leave the client on the board. And in the Wareshark I will see the entire exchange. I will also make a server on Windows under FreeRTOS.

the one thing that surprises me is that you wrote that you see UDP packets in your setup. If the rest is exactly the same (ie switch with .1,.2 and .3 devices attached), then the only reason why you would observe UDP traffic but not TCP would be that the switch swallows the packets due to some firewall policy setup. You might want to check that and possibly change the TCP port to something well known (eg 80) for testing.

In my attempts I use port 10000. Right now I’ll try port 80. Yes, UDP does get through. This is true.

No, the situation did not change after changing the server port number from 10000 to 80. All the same, I will make a server on Windows.

Another thing that comes to mind is that you may have tcp checksum offload enabled but the hardware does not support it. That would cause all tcp packets to be dropped by the receiver side; however, I would expect to see the packes in wireshark.

Very strange. Les us know the results with the direct communication against a Windows machine.

By the way, you would not even need a functioning Windows server, simply try to establish a connection to .3 on any port from your FreeRTOS client. The expected result is a SYN packet inbound and an RST packet outbound. That can give you a first indication as to whether TCP packets are emitted in the first place.

As I understand it, you are suggesting not to even create my server, but to try to establish a connection with some Window’s server by default. What port number should I try to establish a connection with?

Should not really matter for testing, 10000 should be fine. If you use a port on which your Windows machine does indeed listen (eg smb), your connection establishment (3 way handshake) should work, but of course the payload data exchange would fail.

The client is trying to connect to the server…3 port 10000.
tcp client ip1 to server ip3port10000.7z (963 Bytes)

Here is my client code:

static void prvSimpleClientTask( void *pvParameters )
{
#if (INFOINSHARK == 1)
	Socket_t xSharkWinSocket;
	struct freertos_sockaddr xSharkWinAddress;

#endif
	Socket_t xThisClientSocket, xThatServerSocket;
	struct freertos_sockaddr xThisClientSocketAddress,
        xThatServerSocketAddress;
	static const TickType_t xTxTimeOut = pdMS_TO_TICKS( 2000 );
	static const TickType_t xRxTimeOut = pdMS_TO_TICKS( 4000 );


	#if( ipconfigUSE_TCP_WIN == 1 )
	WinProperties_t xWinProps;
	xWinProps.lTxBufSize =  6 * ipconfigTCP_MSS;;
	xWinProps.lTxWinSize = 3;
	xWinProps.lRxBufSize = 6 * ipconfigTCP_MSS;
	xWinProps.lRxWinSize = 3;
	#endif /* ipconfigUSE_TCP_WIN */

	uint32_t ulCount = 0UL;
	volatile static int32_t status, xAlreadyTransmitted,
            xTotalLengthToSend, xLenToSend, xBytesSent;

	memset( &xThatServerSocketAddress, 0, sizeof(xThatServerSocketAddress) );
	xThatServerSocketAddress.sin_address = (IP_Address_t)FreeRTOS_inet_addr( "192.168.0.2" );
	xThatServerSocketAddress.sin_family = FREERTOS_AF_INET4;
	xThatServerSocketAddress.sin_port = FreeRTOS_htons( 10000 );

#if (INFOINSHARK == 1)
	xSharkWinSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP);
	configASSERT( xSharkWinSocket != FREERTOS_INVALID_SOCKET );

	memset( &xSharkWinAddress, 0, sizeof(xSharkWinAddress) );
	xSharkWinAddress.sin_address.ulIP_IPv4 = FreeRTOS_inet_addr( "192.168.0.3" );
	xSharkWinAddress.sin_family = FREERTOS_AF_INET4;
	xSharkWinAddress.sin_port = FreeRTOS_htons( 10000 );
#endif

	for( ;; )
	{
		xThisClientSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP  );
		configASSERT( xThisClientSocket != FREERTOS_INVALID_SOCKET );

		FreeRTOS_setsockopt( xThisClientSocket, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProps, sizeof( xWinProps ) );
		FreeRTOS_setsockopt( xThisClientSocket, 0, FREERTOS_SO_RCVTIMEO, &xRxTimeOut, sizeof( xRxTimeOut ) );
		FreeRTOS_setsockopt( xThisClientSocket, 0, FREERTOS_SO_SNDTIMEO, &xTxTimeOut, sizeof( xTxTimeOut ) );

		FreeRTOS_bind( xThisClientSocket, NULL, sizeof(xThisClientSocketAddress));
		#if (INFOINSHARK == 1)
		strcpy( WinInfoString, "FreeRTOS_bind");
		FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
		#endif


		status = FreeRTOS_connect( xThisClientSocket, &xThatServerSocketAddress, sizeof( xThatServerSocketAddress ) );
			#if (INFOINSHARK == 1)
			if (status == 0)
			{
				while(1){
					strcpy( WinInfoString, "!!!!!!!!!!!!!!!!!!! FreeRTOS_connect == 0 !!!!!!!!!!!");
					FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
				}
			}
			else
			{
				sprintf( WinInfoString, "%d = FreeRTOS_connect", status);
				FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
			}
			#endif
			vTaskDelay( pdTICKS_TO_MS( 100 ) );

		FreeRTOS_closesocket( xThisClientSocket );
		#if (INFOINSHARK == 1)
		strcpy( WinInfoString, "FreeRTOS_closesocket");
		FreeRTOS_sendto(xSharkWinSocket, WinInfoString, strlen (WinInfoString), 0, &xSharkWinAddress, sizeof(xSharkWinAddress));
		#endif

		vTaskDelay( pdTICKS_TO_MS( 1000 ) );
	}
}

Maybe someone will immediately understand what the error is?
If the FreeRTOS_connect function returns a value of 0, a connection will be established, and it returns -128.

the tcp traffic looks as expected, so the network basically works ok. However, udp packets are only exchanged between your FreeRTOS device and the Windows computer. To me it sounded as if UDP traffic was successful between your two FreeRTOS devices .2 and .3.

So the problem appears to be in the .2 device. So do the reverse test: Try to establish a TCP connection from your Windows machine (eg with a telnet client) to your .2 device and show us the trace.

…and another random suggestion: Is it possible that you assigned the same MAC address to both FreeRTOS devices? That of course would be a nono and would explain some of your observations.

Using the Putty utility I am trying to connect to the server…2 port 10000.

I understand about MAC. They are different. I wrote this in the very first post with a question.

Let me remind you that I do experiments without a switch, connecting a laptop and one of the boards using a point-to-point topology. Therefore, the second board is not physically on the network at all.
tcp puttytel ip3 to server ip2port10000.7z (1021 Bytes)

in the logs you sent, you attempt to connect to port 1000 on the .2 device from the Windows PC.

The data traffic looks as expected here as well (SYN followed by an RST), so on both devices, the network stacks appear to work fine - maybe you can try putty to port 10000 to ensure that you have setup the TCP server correctly?

I’m sorry
tcp puttytel ip3 to server ip2port10000.7z (942 Bytes)

ok, the server also responds with an RST packet on port 10000, so either no socket is listening on that port on the .2, or the server is miscoded to terminate a connection right after the accept, or some restriction (eg low memory) prevents the connection estabishment.

Time to show us your server code…

1 Like