I’m trying to run a TCP server and a UDP Client simultaneously on a STM32 Nucleo F746ZG. I’m using the freeRTOS and LWIP libraries and I´m getting trouble on running both network technologies (TCP and UDP) at the same time. However independently they work fine. Is not possible to run both at the same time or there is something wrong with my code?
This how I implemented the TCP server:
static void tcp_thread(void *arg)
{
struct_out *qstruct;
err_t err, recv_err;
struct netconn *conn;
struct netbuf *inbuf;
struct netconn *newconn;
struct_sock *arg_sock;
arg_sock = (struct_sock*) arg;
conn = arg_sock->conn;
u16_t buflen;
char* buf;
char* str2;
int ret;
for(;;)
{
err = netconn_accept(conn, &newconn);
if (err == ERR_OK)
{
for(;;)
{
recv_err = netconn_recv(newconn, &inbuf); //Receive data
if (recv_err == ERR_OK)
{
netbuf_data(inbuf, (void**)&buf, &buflen); //Get the data
if((buf[0]==0x0D)||(buf[0]==0x0A))//Compruebo si es un salto de
linea
{
netbuf_delete(inbuf); //Limpia el buffer de entrada de datos
continue;
}
//qstruct = osMailAlloc(strout_Queue, osWaitForever);
//qstruct->y_pos = arg_sock->y_pos;
strncpy(str_buf,buf,buflen);
str_buf[buflen]=0;
//sprintf(qstruct->str,"%-20s", str_buf);
osMailPut(strout_Queue, qstruct);
osMailFree(strout_Queue, qstruct);
//Salto de linea para a la hora de imprimir dejar espacio y que no
se peguen los mensajes
str_buf[buflen] = '\r';
str_buf[buflen+1] = '\n';
strcpy(str2, "hi");
ret = strncmp(str_buf, str2, 4);
if(ret == 0) {
netconn_write(newconn, str_buf, buflen+2, NETCONN_COPY);
//Escribe y si lo quito no hay ACK de llegada del paquete
netbuf_delete(inbuf); //Limpia el buffer de entrada de datos
} else {
netconn_write(newconn, "recived", 4, NETCONN_COPY);
//Escribe y
si lo quito no hay ACK de llegada del paquete
netbuf_delete(inbuf); //Limpia el buffer de entrada de datos
}
}
else
{
netbuf_delete(inbuf);
netconn_close(newconn);
break;
}
}
}
else
{
osDelay(1);
}
}
void StartDefaultTask(void const * argument)
{
/* init code for LWIP */
MX_LWIP_Init();
/* USER CODE BEGIN 5 */
struct netconn *conn;
err_t err;
//sock01.y_pos = 60;
//sock02.y_pos = 180;
conn = netconn_new(NETCONN_TCP);
if(conn!=NULL)
{
sock01.conn = conn;
sock02.conn = conn;
err = netconn_bind(conn, NULL, 80);
if (err == ERR_OK)
{
netconn_listen(conn);
sys_thread_new("tcp_thread1", tcp_thread, (void*)&sock01,
DEFAULT_THREAD_STACKSIZE, osPriorityNormal );
sys_thread_new("tcp_thread2", tcp_thread, (void*)&sock02,
DEFAULT_THREAD_STACKSIZE, osPriorityNormal );
}
else
{
netconn_delete(conn);
}
}
/* Infinite loop */
for(;;)
{
osDelay(1);
}
/* USER CODE END 5 */
}
This is the UDP client:
void udp_client_connect(void)
{
ip_addr_t DestIPaddr;
err_t err;
upcb = udp_new();
if (upcb!=NULL)
{
IP4_ADDR(&DestIPaddr, 192, 168, 1, 150);
upcb->local_port = 1555;
err= udp_connect(upcb, &DestIPaddr, 52709);
if (err == ERR_OK)
{
udp_recv(upcb, udp_receive_callback, NULL);
}
}
}
Thanks mates!!
I´ve solved the problem by not using FreeRTOS library because if you want to implement it you could only do one task at the same time. I recommend you, to have a look to the ST Microelectronics tutorials of your board in your case you can find it at the following link:
Here you will find some examples of UDP and TCP (Server and Client) only using LWIP library which is easier.
I hope that solves your problem ;)