main.c
int BsdTcpServer(unsigned short usPort)
{
    SlSockAddrIn_t  sAddr;
    SlSockAddrIn_t  sLocalAddr;
    int             iCounter;
    int             iAddrSize;
    int             iSockID;
    int             iStatus;
    int             iNewSockID;
    long            lLoopCount = 0;
    long            lNonBlocking = 1;
    int             iTestBufLen;

    // filling the buffer
    for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
    {
        g_cBsdBuf[iCounter] = (char)(iCounter % 10);
    }

    iTestBufLen  = BUF_SIZE;

    //filling the TCP server socket address
    sLocalAddr.sin_family = SL_AF_INET;
    sLocalAddr.sin_port = sl_Htons((unsigned short)usPort);
    sLocalAddr.sin_addr.s_addr = 0;

    // creating a TCP socket
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
    if( iSockID < 0 )
    {
        // error
        ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
    }

    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
        sl_Close(iSockID);
        ASSERT_ON_ERROR(BIND_ERROR);
    }

    // putting the socket for listening to the incoming TCP connection
    iStatus = sl_Listen(iSockID, 0);
    if( iStatus < 0 )
    {
        sl_Close(iSockID);
        ASSERT_ON_ERROR(LISTEN_ERROR);
    }

    // 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 )
    {
        sl_Close(iSockID);
        ASSERT_ON_ERROR(SOCKET_OPT_ERROR);
    }
    iNewSockID = SL_EAGAIN;

    // waiting for an incoming TCP connection
    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
            sl_Close(iNewSockID);
            sl_Close(iSockID);
            ASSERT_ON_ERROR(ACCEPT_ERROR);
        }
    }

    // waits for 1000 packets from the connected TCP client
    while (lLoopCount < g_ulPacketCount)
    {
        iStatus = sl_Recv(iNewSockID, g_cBsdBuf, iTestBufLen, 0);
        if( iStatus <= 0 )
        {
          // error
          sl_Close(iNewSockID);
          sl_Close(iSockID);
          ASSERT_ON_ERROR(RECV_ERROR);
        }

        lLoopCount++;
    }

    Report("Recieved %u packets successfully\n\r",g_ulPacketCount);

    // close the connected socket after receiving from connected TCP client
    iStatus = sl_Close(iNewSockID);
    ASSERT_ON_ERROR(iStatus);
    // close the listening socket
    iStatus = sl_Close(iSockID);
    ASSERT_ON_ERROR(iStatus);

    return SUCCESS;
}
//****************************************************************************
//
//! \brief Opening a TCP server side socket and receiving data
//!
//! This function opens a TCP socket in Listen mode and waits for an incoming
//!    TCP connection.
//! If a socket connection is established then the function will try to read
//!    1000 TCP packets from the connected client.
//!
//! \param[in] port number on which the server will be listening on
//!
//! \return     0 on success, -1 on error.
//!
//! \note   This function will wait for an incoming connection till
//!                     one is established
//
//****************************************************************************

\brief Opening a TCP server side socket and receiving data
This function opens a TCP socket in Listen mode and waits for an incoming
TCP connection.
If a socket connection is established then the function will try to read
1000 TCP packets from the connected client.

\param[in] port number on which the server will be listening on

\return     0 on success, -1 on error.

\note   This function will wait for an incoming connection till
one is established
\ brief Opening a TCP server side socket and receiving data This function opens a TCP socket in Listen mode and waits for an incoming TCP connection . If a socket connection is established then the function will try to read 1000 TCP packets from the connected client . \ param [ in ] port number on which the server will be listening on \ return 0 on success - 1 on error . \ note This function will wait for an incoming connection till one is established
int BsdTcpServer(unsigned short usPort)
{
    SlSockAddrIn_t  sAddr;
    SlSockAddrIn_t  sLocalAddr;
    int             iCounter;
    int             iAddrSize;
    int             iSockID;
    int             iStatus;
    int             iNewSockID;
    long            lLoopCount = 0;
    long            lNonBlocking = 1;
    int             iTestBufLen;
    for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++)
    {
        g_cBsdBuf[iCounter] = (char)(iCounter % 10);
    }
    iTestBufLen  = BUF_SIZE;
    sLocalAddr.sin_family = SL_AF_INET;
    sLocalAddr.sin_port = sl_Htons((unsigned short)usPort);
    sLocalAddr.sin_addr.s_addr = 0;
    iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
    if( iSockID < 0 )
    {
        ASSERT_ON_ERROR(SOCKET_CREATE_ERROR);
    }
    iAddrSize = sizeof(SlSockAddrIn_t);
    iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize);
    if( iStatus < 0 )
    {
        sl_Close(iSockID);
        ASSERT_ON_ERROR(BIND_ERROR);
    }
    iStatus = sl_Listen(iSockID, 0);
    if( iStatus < 0 )
    {
        sl_Close(iSockID);
        ASSERT_ON_ERROR(LISTEN_ERROR);
    }
    iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING,
                            &lNonBlocking, sizeof(lNonBlocking));
    if( iStatus < 0 )
    {
        sl_Close(iSockID);
        ASSERT_ON_ERROR(SOCKET_OPT_ERROR);
    }
    iNewSockID = SL_EAGAIN;
    while( iNewSockID < 0 )
    {
        iNewSockID = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr,
                                (SlSocklen_t*)&iAddrSize);
        if( iNewSockID == SL_EAGAIN )
        {
           MAP_UtilsDelay(10000);
        }
        else if( iNewSockID < 0 )
        {
            sl_Close(iNewSockID);
            sl_Close(iSockID);
            ASSERT_ON_ERROR(ACCEPT_ERROR);
        }
    }
    while (lLoopCount < g_ulPacketCount)
    {
        iStatus = sl_Recv(iNewSockID, g_cBsdBuf, iTestBufLen, 0);
        if( iStatus <= 0 )
        {
          sl_Close(iNewSockID);
          sl_Close(iSockID);
          ASSERT_ON_ERROR(RECV_ERROR);
        }
        lLoopCount++;
    }
    Report("Recieved %u packets successfully\n\r",g_ulPacketCount);
    iStatus = sl_Close(iNewSockID);
    ASSERT_ON_ERROR(iStatus);
    iStatus = sl_Close(iSockID);
    ASSERT_ON_ERROR(iStatus);
    return SUCCESS;
}
int BsdTcpServer ( unsigned short usPort ) { SlSockAddrIn_t sAddr ; SlSockAddrIn_t sLocalAddr ; int iCounter ; int iAddrSize ; int iSockID ; int iStatus ; int iNewSockID ; long lLoopCount = 0 ; long lNonBlocking = 1 ; int iTestBufLen ; for ( iCounter = 0 ; iCounter < BUF_SIZE ; iCounter ++ ) { g_cBsdBuf [ iCounter ] = ( char ) ( iCounter % 10 ) ; } iTestBufLen = BUF_SIZE ; sLocalAddr . sin_family = SL_AF_INET ; sLocalAddr . sin_port = sl_Htons ( ( unsigned short ) usPort ) ; sLocalAddr . sin_addr . s_addr = 0 ; iSockID = sl_Socket ( SL_AF_INET , SL_SOCK_STREAM , 0 ) ; if ( iSockID < 0 ) { ASSERT_ON_ERROR ( SOCKET_CREATE_ERROR ) ; } iAddrSize = sizeof ( SlSockAddrIn_t ) ; iStatus = sl_Bind ( iSockID , ( SlSockAddr_t * ) & sLocalAddr , iAddrSize ) ; if ( iStatus < 0 ) { sl_Close ( iSockID ) ; ASSERT_ON_ERROR ( BIND_ERROR ) ; } iStatus = sl_Listen ( iSockID , 0 ) ; if ( iStatus < 0 ) { sl_Close ( iSockID ) ; ASSERT_ON_ERROR ( LISTEN_ERROR ) ; } iStatus = sl_SetSockOpt ( iSockID , SL_SOL_SOCKET , SL_SO_NONBLOCKING , & lNonBlocking , sizeof ( lNonBlocking ) ) ; if ( iStatus < 0 ) { sl_Close ( iSockID ) ; ASSERT_ON_ERROR ( SOCKET_OPT_ERROR ) ; } iNewSockID = SL_EAGAIN ; while ( iNewSockID < 0 ) { iNewSockID = sl_Accept ( iSockID , ( struct SlSockAddr_t * ) & sAddr , ( SlSocklen_t * ) & iAddrSize ) ; if ( iNewSockID == SL_EAGAIN ) { MAP_UtilsDelay ( 10000 ) ; } else if ( iNewSockID < 0 ) { sl_Close ( iNewSockID ) ; sl_Close ( iSockID ) ; ASSERT_ON_ERROR ( ACCEPT_ERROR ) ; } } while ( lLoopCount < g_ulPacketCount ) { iStatus = sl_Recv ( iNewSockID , g_cBsdBuf , iTestBufLen , 0 ) ; if ( iStatus <= 0 ) { sl_Close ( iNewSockID ) ; sl_Close ( iSockID ) ; ASSERT_ON_ERROR ( RECV_ERROR ) ; } lLoopCount ++ ; } Report ( " \n \r " , g_ulPacketCount ) ; iStatus = sl_Close ( iNewSockID ) ; ASSERT_ON_ERROR ( iStatus ) ; iStatus = sl_Close ( iSockID ) ; ASSERT_ON_ERROR ( iStatus ) ; return SUCCESS ; }
\brief Opening a TCP server side socket and receiving data
This function opens a TCP socket in Listen mode and waits for an incoming
TCP connection.
\ brief Opening a TCP server side socket and receiving data This function opens a TCP socket in Listen mode and waits for an incoming TCP connection .
// filling the buffer
 //filling the TCP server socket address
 // creating a TCP socket
 // error
 // binding the TCP socket to the TCP server address
 // error
 // putting the socket for listening to the incoming TCP connection
 // setting socket option to make the socket as non blocking
 // waiting for an incoming TCP connection
 // accepts a connection form a TCP client, if there is any
 // otherwise returns SL_EAGAIN
 // error
 // waits for 1000 packets from the connected TCP client
 // error
 // close the connected socket after receiving from connected TCP client
 // close the listening socket
