Hi,
I am trying to work on TCP Socket example code and trying to implement a TCP server to which 4 TCP client will connect simultaneously.
These TCP client need to connect on single TCP socket.
I am not sure how to do this, tried few ways as shown in below code snippet which shows 2 TCP clients getting connected, but data reception from client is not consistent. Data from the first TCP client which gets connected, is received consistently but sometimes eventhough data from 2nd TCP client is not sent, I get a debug message showing previous data from 2nd TCP Client.
Data from 2nd TCP client is inconsistent with above scenario.
Please let me know if the handling of TCP server with multiple client is correct or is there any other methodology to work with multiple TCP clients getting connected to TCP server.
Thanks,
Ashutosh.
Code snippet -
// creating a TCP socket
iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
if( iSockID < 0 )
{
// error
ASSERT_ON_ERROR(TCP_SERVER_FAILED);
}
iAddrSize = sizeof(SlSockAddrIn_t);
// binding the TCP socket to the TCP server address
iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize);
if( iStatus < 0 )
{
// error
ASSERT_ON_ERROR(sl_Close(iSockID));
ASSERT_ON_ERROR(TCP_SERVER_FAILED);
}
// putting the socket for listening to the incoming TCP connection
iStatus = sl_Listen(iSockID, 0);
if( iStatus < 0 )
{
ASSERT_ON_ERROR(sl_Close(iSockID));
ASSERT_ON_ERROR(TCP_SERVER_FAILED);
}
// setting socket option to make the socket as non blocking
iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING,
&lNonBlocking, sizeof(lNonBlocking));
if( iStatus < 0 )
{
ASSERT_ON_ERROR(sl_Close(iSockID));
ASSERT_ON_ERROR(TCP_SERVER_FAILED);
}
iNewSockID = SL_EAGAIN;
iNewSockID2 = SL_EAGAIN;
// waiting for an incoming TCP connection for TCP client 1
while( iNewSockID < 0 )
{
// accepts a connection form a TCP client, if there is any
// otherwise returns SL_EAGAIN
iNewSockID = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr,
(SlSocklen_t*)&iAddrSize);
if( iNewSockID == SL_EAGAIN )
{
MAP_UtilsDelay(10000);
}
else if( iNewSockID < 0 )
{
// error
ASSERT_ON_ERROR(sl_Close(iNewSockID));
ASSERT_ON_ERROR(sl_Close(iSockID));
ASSERT_ON_ERROR(TCP_SERVER_FAILED);
}
}
Report("Client 1 connected!");
// waiting for an incoming TCP connection for TCP client 2
while( iNewSockID2 < 0 )
{
// accepts a connection form a TCP client, if there is any
// otherwise returns SL_EAGAIN
iNewSockID2 = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr,
(SlSocklen_t*)&iAddrSize);
if( iNewSockID2 == SL_EAGAIN )
{
MAP_UtilsDelay(10000);
}
else if( iNewSockID2 < 0 )
{
// error
ASSERT_ON_ERROR(sl_Close(iNewSockID2));
ASSERT_ON_ERROR(sl_Close(iSockID));
ASSERT_ON_ERROR(TCP_SERVER_FAILED);
}
}
Report("Client 2 connected!");
}
// data receive from TCP Client 1
SL_FD_SET(iNewSockID, &ReadFds);
ActiveReadFds = ReadFds;
iStatus = sl_Select(iNewSockID + 1, &ActiveReadFds, NULL, \
NULL, &timeout);
if((iStatus > 0))// && (FD_ISSET(iSockID, &ReadFds))) // Checks if current socket descriptor is set
{
iStatus = sl_Recv(iNewSockID, g_cBsdBuf, iTestBufLen, 0);
UART_PRINT("Data 1 is - %x\n\r",g_cBsdBuf[0]);
}
// data receive from TCP Client 2
SL_FD_SET(iNewSockID2, &ReadFds);
ActiveReadFds = ReadFds;
iStatus = sl_Select(iNewSockID2 + 1, &ActiveReadFds, NULL, \
NULL, &timeout);
if((iStatus > 0) && (FD_ISSET(iNewSockID2, &ReadFds))) // Checks if current socket descriptor is set
{
iStatus = sl_Recv(iNewSockID2, g_cBsdBuf2, iTestBufLen, 0);
UART_PRINT("Data 2 is - %x\n\r",g_cBsdBuf2[0]);
}