I tried to do a 3 or 4 tcp server on my STM32F107. I'm using freeRTOS and LwIP (and the CubeMX + ST HAL library).
I create a task tcp_server, and inside the task, I create 3 netconn structure each with different tcp port. Callback:
void socket_callback(struct netconn * conn, enum netconn_evt evt, u16_t len)
{
queue_event_t msg;
if (evt == NETCONN_EVT_RCVPLUS)
{
msg.cmd = MSG_TEST;
if (conn == user_conn.conn_modbus)
msg.cmd = MSG_MODBUS;
else if (conn == user_conn.conn_modbus_listen)
msg.cmd = MSG_MODBUS_LISTEN;
else if (conn == user_conn.conn_rs232)
msg.cmd = MSG_RS232;
else if (conn == user_conn.conn_rs232_listen)
msg.cmd = MSG_RS232_LISTEN;
else if (conn == user_conn.conn_rs485)
msg.cmd = MSG_RS485;
else if (conn == user_conn.conn_rs485_listen)
msg.cmd = MSG_RS485_LISTEN;
xQueueSend(user_conn.evtQueue, &msg, 1000);
}
}
Server creation :
static struct netconn * createServer(int port)
{
struct netconn * conn;
err_t err;
conn = netconn_new_with_callback(NETCONN_TCP, socket_callback);
if (conn == NULL)
{
char *msg = "Cannot create netconn\n\r";
HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), 0xFFFF);
return NULL;
}
err = netconn_bind(conn, NULL, port);
if (err != ERR_OK)
{
char *msg = "Error in Binding \n\r";
HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), 0xFFFF);
print_error(err);
netconn_delete(conn);
return NULL;
}
err = netconn_listen(conn);
if (err != ERR_OK)
{
char *msg = "Error in listenning \n\r";
HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), 0xFFFF);
print_error(err);
netconn_delete(conn);
return NULL;
}
return conn;
}
Thread :
static void servertcp_thread()
{
queue_event_t evt;
user_conn.evtQueue = xQueueCreate(10, sizeof (queue_event_t));
user_conn.conn_modbus_listen = createServer(502);
user_conn.conn_rs232_listen = createServer(10001);
user_conn.conn_rs485_listen = createServer(50001);
while(1)
{
waitServer(&user_conn, &evt);
send_rs232(&user_conn);
send_rs485(&user_conn);
if((user_conn.conn_modbus_listen != NULL) && (evt.cmd == MSG_MODBUS_LISTEN))
processModbusListen(&user_conn);
if((user_conn.conn_rs232_listen != NULL) && (evt.cmd == MSG_RS232_LISTEN))
processRS232Listen(&user_conn);
if((user_conn.conn_rs485_listen != NULL) && (evt.cmd == MSG_RS485_LISTEN))
processRS485Listen(&user_conn);
if((user_conn.conn_modbus != NULL) && (evt.cmd == MSG_MODBUS))
modbus_tcp_server(&user_conn);
if((user_conn.conn_rs232 != NULL) && (evt.cmd == MSG_RS232))
rs232_tcp_server(&user_conn);
if((user_conn.conn_rs485 != NULL) && (evt.cmd == MSG_RS485))
rs485_tcp_server(&user_conn);
}
}
The creation is successful (netconn_new_with_callback / netconn_bind and netconn_listen with no error).
When I tried to connect to 1 tcp port, it is accepted and everything is ok.
But when I tried to connect to a second port while the first connexion is still alive, the callback is called, but the netconn_accept(conn, &newconn); failed and report ERR_ABRT and I don't know why.
The 3 function (processModbusListen, processRS232Listen and processRS485Listen) are coded following the same prototype:
static int processXXXListen(user_conn_t * user_data)
{
struct netconn *newconn;
err_t error_accept;
error_accept = netconn_accept(user_data->conn_XXX_listen, &newconn);
if (error_accept == ERR_OK)
{
if (user_data->conn_rs485)
{
// Close unwanted connection
netconn_close(newconn);
netconn_delete(newconn);
char *msg = "Error XXX Connection during establishement\n\r";
HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), 0xFFFF);
}
else
{
// connection established
char *msg = "XXX Connection established\n\r";
HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), 0xFFFF);
user_data->conn_XXX = newconn;
}
}
else
{
char *msg = "Error in acceptation TCP XXX connection\n\r";
HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), 0xFFFF);
print_error(error_accept);
}
return 0;
}
The strange thing is, that when i only create 2 netconn structure, i have no problem with the 2 connection in the same time.
What can cause the connexion aborted ? How can i get the 3 connection in the same time ? If it is possible of course ...