cancel
Showing results for 
Search instead for 
Did you mean: 

LWIP Socket API, TCP and UDP connection on the same STM32 MCU

voyvoda .
Senior

Hello,

I am trying to implement TCP and UDP server connection on my STM32F407-Discovery Board. I use Socket API with FreeRTOS.

I am able to run TCP or UDP seperately. Single server or client works fine.

When I combine TCP and UDP server tasks at the same code. None of them work. I can not even PING my STM32 board. I created different two tasks for each TCP and UDP server.

I tried to increase HEAP size of LWIP above 32KB. However, it did not resolved my issue.

Are the any solution to my issue ?

Thank you in advance for your help. Best Regards

17 REPLIES 17

How can I select or define local port number ?

You can create variable

struct sockaddr_in localhost;

And then

localhost.sin_port = htons (12345);

And bind socket with localhost struct.

Oh, I forgot close socket after unsuccessful attempt to connect.

newconn = connect(socket, (struct sockaddr*)&addr, sizeof (addr));
					if (newconn < 0){
						printf("2. Unable to connect to remote server, newconn = %i\r\n\r\n", newconn);
    close (socket);
						osDelay(10000);

voyvoda .
Senior

As you can see my code, my clients are not using the same struct. They are​dest_addr_tcp and dest_addr_udp.

Why you said that your clients are using the same local port ?​

Ivan Pletnev
Associate II

Because you assign destination port in that structures. Local port is assigned by LWIP at boot. And it is the same for your udp and udp clients. You can assign local port to your socket only by calling bind().

I have two tasks. StartDefaultTask and StartTask02

StartTask02 task called first and ıt stacks in line as shown in the picture below.

StartDefaultTask never called by OS.

I think there is a memory allocation problem

0693W000007ZnazQAC.png

Piranha
Chief II

@voyvoda .​​, the main problem is that ST's Ethernet link status and DHCP code uses RAW API, but doesn't do core locking. And it's also broken in other ways, as explained in my topic.

@Ivan Pletnev​, you are doing a bunch of needless work by reimplementing lwIP stack's functionality. Only servers have to bind local ports to a specific port. For clients the lwIP assigns local ports automatically from a proper range. As bind() and netconn_bind() uses RAW API underneath, read this:

https://www.nongnu.org/lwip/2_1_x/group__udp__raw.html#gac7fbda8b12b9b9360e92b51e805e799e

port local UDP port to bind with. Use 0 to automatically bind to a random port between UDP_LOCAL_PORT_RANGE_START and UDP_LOCAL_PORT_RANGE_END.

Optionally, if LWIP_RAND is defined, the start of the range is initialized to a random number in the range. Also the binding is done automatically on the first send operation, therefore clients don't have to do manual binding at all.

Ivan Pletnev
Associate II

@voyvoda .​ Yes, l found tcp_port_new() function in LWIP code. Thank you a lot for your comment, It was really helpfully for me. LWIP automatic port assigning is really works. I removed randomizer code from my client.