Below is a section of code. Basically what is going on when a device with for example IP 172.22.1.32 on port 512
will connect create a task result = taskcreate(0, ModBus_C_TASK, child_sock); and pass in the child client
that just connected. The while loop will loop back around and the same IP 172.22.1.32 will again connect create
another task task result = taskcreate(0, ModBus_C_TASK, child_sock); and so on. the printf "Client dispatched…"
will print every second. Constantly creating new tasks task result = taskcreate(0, ModBus_C_TASK, child_sock);…………….
listensock_one = socket(AF_INET, SOCK_STREAM, 0);//PF_INET
if (listensock_one == RTCS_SOCKET_ERROR)
{
error = RTCSERR_OUT_OF_SOCKETS;
shutdown(listensock_one, FLAG_ABORT_CONNECTION);//FLAG_CLOSE_TX);
statusWord= 0x0800;
//printf("Fg");
return;
}
BufferValue = 512; // 512 bytes send and receive
setsockopt(listensock_one, SOL_TCP,OPT_TBSIZE,&BufferValue,sizeof(BufferValue));
setsockopt(listensock_one, SOL_TCP,OPT_RBSIZE,&BufferValue,sizeof(BufferValue));
// BufferValue = TRUE;
// setsockopt(listensock_one, SOL_TCP,OPT_RECEIVE_NOWAIT,&BufferValue,sizeof(BufferValue));
// setsockopt(listensock_one, SOL_TCP,OPT_NO_NAGLE_ALGORITHM,&BufferValue,sizeof(BufferValue));
// BufferValue = 5000;//ms
// setsockopt(listensock_one, SOL_TCP,OPT_CONNECT_TIMEOUT,&BufferValue,sizeof(BufferValue));
//BufferValue = 1;//ms
//setsockopt(listensock_one, SOL_TCP,OPT_KEEPALIVE,&BufferValue,sizeof(BufferValue));
BufferValue = 100;
setsockopt(listensock_one, SOL_TCP,OPT_TIMEWAIT_TIMEOUT,&BufferValue,sizeof(BufferValue));
error = bind(listensock_one, &laddr, sizeof(laddr));
if (!error)
{
statusWord= 0x0400;
//printf("RR"); // socket initial bind, TCP READY
error = listen(listensock_one, 0);
}
else
{
shutdown(listensock_one, FLAG_ABORT_CONNECTION);//FLAG_CLOSE_TX);
statusWord= 0x0500;
//printf("Fb"); // TCP failed to bind
RTCS_detachsock(listensock_one);
return;
}
uint_16 remote_addr_len;
timedelay(250);
remote_addr_len = sizeof(remote_addr);
do
{
//child_sock = RTCS_selectall(0);
statusWord= 0x1000; //ModBusServerTask waiting to connect
child_sock = accept(listensock_one, &remote_addr, &remote_addr_len);//accept(listensock, &remote_addr, &remote_addr_len);
if (child_sock==RTCS_SOCKET_ERROR)
{
shutdown((uint_32)child_sock, FLAG_ABORT_CONNECTION);
statusWord = 0x0600; //printf("Fl"); // modbus listen socket abort error
continue;
}
else
{
printf("add: 0x%X\n %d",remote_addr.sin_addr,remote_addr.sin_port);
/////////////////////////////////////////////////////
///Grab child connected socket and pass no new task.
if (RTCS_detachsock(child_sock) == RTCS_OK)
{
printf("server child task.\n");
result = taskcreate(0, ModBus_C_TASK, child_sock);
if (result == MQX_NULL_TASK_ID)
{
shutdown(child_sock, FLAG_CLOSE_TX);
printf("\nCould not create server child task.\n\r");
}
}
else
{
printf("\nCould not create server child task.\n");
}
timedelay(1000);
printf("Client dispatched...\n\r");
}
////////////////////////////////////////////////////////
///Check linkActive flag
if(linkActive == false)
{
shutdown(listensock_one, FLAG_ABORT_CONNECTION);//FLAG_CLOSE_TX);
statusWord = 0x0900; //printf("Rv"); // modbus Restart modbus socket
return;
}
}
while( child_sock != RTCS_SOCKET_ERROR );