lwip + rtos reconnection

891 views Asked by At

I configured a TCP connection using LwIP + RTOS in microcontroller (server) connected to a PC(client program)

conn = netconn_new(NETCONN_TCP); 
netconn_bind(conn, &MyIPAddr, PORT);

do 
{ 
osDelay(5); 
} 
while((netconn_connect(conn, &DestIPaddr, TCP_PORT)!= ERR_OK)  //wait until a PC client will be available 

If a TCP client program is already running on PC , everything is ok. But if I start PC client after executing netconn_connect(..) function on microcontroller, while() condition will never be ERR_OK. How should I modify the code to correctly connect to a PC client?

1

There are 1 answers

0
Freddie Chopin On

You are using it wrong. If your application is a server, then the correct usage looks more-or-less like this:

listenNetconn = netconn_new(NETCONN_TCP);
netconn_bind(listenNetconn, IP_ADDR_ANY, port);
netconn_listen(listenNetconn);
netconn_accept(listenNetconn, &clientNetconn); // wait for connection

Now you should use clientNetconn to receive/send data. listenNetconn is used only to listen for new incoming connections and nothing more.

Here's an example from the unofficial lwIP wiki - http://lwip.wikia.com/wiki/Netconn_Accept