How to correctly configure the address of a socket configured as TCP server?

Hi.

I’m trying to implement a TCP / IP server based on the following tutorial:

Create, Configure and Bind a TCP Socket

But I have a question as to how the IP address is assigned that the router that I imagine has provided it dynamically (DHCP)

The lines of code in question are as follows:

     /* Set the listening port to 10000. */
    xBindAddress.sin_port = ( uint16_t ) 10000;
    xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
    /* Bind the socket to the port that the client RTOS task will send to. */
    FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );

Is that way correct?

or should the following instruction be used?

xRemoteAddress.sin_addr

Thanks for any comments.

Where? You show three lines and I’m not sure where you are suggesting xRemoteAddress.sin_addr is used within those three. In any case, I think the code is correct. There are also examples you can reference in the download. For example here:

https://github.com/FreeRTOS/FreeRTOS/blob/main/FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Minimal_Windows_Simulator/DemoTasks/SimpleTCPEchoServer.c#L190.

Alternatively, but a bit more complicated, within this directory:

https://github.com/FreeRTOS/FreeRTOS/tree/main/FreeRTOS-Plus/Demo/Common/Demo_IP_Protocols

1 Like

Hi there.

The TCP / IP server has worked very well, it seems to me that the xRemoteAddress.sin_addr instruction is used when you want a TCP client and it is the IP of the server where you want to connect, otherwise, that is, it is how I initially published.

Thanks for writting