最近遇到个问题,在STM32H750上跑的RTOS,创建多路的TCP Server分不同端口监听,与客户端分别连接通讯。但是遇到个问题,测试过程中发现频繁的通讯失败断连,过一点时间端口就挂了,再也连不上。这部分应用代码如下,有没有大佬遇到过
/**
* @brief TCP服务器 任务函数
*
* @paramarg
*/
staticvoidTCP_Server_Task(void*arg)
{
_TcpTaskConfig_*p_TcpTaskPkg= (_TcpTaskConfig_*)arg;
int16_tindex=p_TcpTaskPkg->num;
structsockaddr_inserver_addr, client_addr;
socklen_tsin_size;
intrecv_data_len=0;
p_TcpTaskPkg->sockTcp=socket(AF_INET, SOCK_STREAM, 0);
if (p_TcpTaskPkg->sockTcp<0)
{
DebugErr("\rSocket error\n");
goto__exit;
}
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=INADDR_ANY;
server_addr.sin_port=htons(p_TcpTaskPkg->port);
memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
if (bind(p_TcpTaskPkg->sockTcp, (structsockaddr*)&server_addr, sizeof(structsockaddr)) ==-1)
{
DebugErr("\rUnable to bind\n");
goto__exit;
}
if (listen(p_TcpTaskPkg->sockTcp, 5) ==-1)
{
DebugErr("\rListen error\n");
goto__exit;
}
while(1)
{
sin_size=sizeof(structsockaddr_in);
p_TcpTaskPkg->connectTcpClient=accept(p_TcpTaskPkg->sockTcp, (structsockaddr*)&client_addr, &sin_size);
if ((p_TcpTaskPkg->port==TCP_SER1_PORT) || (p_TcpTaskPkg->port==TCP_SER2_PORT))
{
DebugInfo("\r[Connect] Client(%s:%d) --> Tcp(%d) ...\n", inet_ntoa(client_addr.sin_addr), p_TcpTaskPkg->port, ntohs(client_addr.sin_port));
}
// TCP_NODELAY是设置Nagle算法,为解决网络中小包太多合并大包来提升网络传输效率的算法(会引起粘包)
// if (p_TcpTaskPkg->connectTcpClient >= 0)
// {
// int flag = 1;
// setsockopt(p_TcpTaskPkg->connectTcpClient,
// IPPROTO_TCP, /* set option at TCP level */
// TCP_NODELAY, /* name of option */
// (void *) &flag, /* the cast is historical cruft */
// sizeof(int)); /* length of option value */
// }
while(1)
{
recv_data_len=recv(p_TcpTaskPkg->connectTcpClient, mTcpRxHeap[index].data, CFG_LEN_TCP_RECV_BUFF, 0);
if (recv_data_len<=0) break; // Client Close
mTcpRxHeap[index].len=recv_data_len;
// 数字转换为端口号 1-8口 0x11-0x12口
if (index<0x08) mTcpRxHeap[index].port=index+1; // 1-8口
else mTcpRxHeap[index].port= (index-0x08) +0x11; // 0x11 0x12
Transmit_TcpDataToPort(&mTcpRxHeap[index]);
// Debug_BuffData(mTcpRxHeap[index].data, recv_data_len);
// write(p_TcpTaskPkg->connectTcpClient, mTcpRxHeap[index].data, 7); // test send
}
if (p_TcpTaskPkg->connectTcpClient>=0)
{
closesocket(p_TcpTaskPkg->connectTcpClient);
if ((p_TcpTaskPkg->port==TCP_SER1_PORT) || (p_TcpTaskPkg->port==TCP_SER2_PORT))
{
DebugInfo("\r[Disconnect] Client(%s:%d) Close ... \n", inet_ntoa(client_addr.sin_addr), p_TcpTaskPkg->port);
}
}
p_TcpTaskPkg->connectTcpClient=-1;
}
__exit:
if (p_TcpTaskPkg->sockTcp>=0)
{
closesocket(p_TcpTaskPkg->sockTcp);
DebugInfo("\rClose Tcp Socket ... \n");
}
// if (recv_data) free(recv_data);
}
/*-----------------------------------------------------------------------------------*/
voidtcpTaskManager_init(void)
{
charstrServerName[20];
for (uint16_ti=0; i<get_TcpTaskPkg_size(); i++)
{
sprintf(strServerName, "TCP Server_%d", i);
TaskThread_Create(strServerName, TCP_Server_Task, (void*)(&mTcpTaskPkg), mTcpTaskPkg.stackSize, 15, mTcpTaskPkg.handleTask);
}
}
/*-----------------------------------------------------------------------------------*/