⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contest.c

📁 本软件是TI公司免费提供的网络开发包 现在好象很难找到,有黑心的公司把它改一改,就卖价5000元,对网络开发和网络驱动开发有参考价值
💻 C
📖 第 1 页 / 共 4 页
字号:
    // Start Test
    test = 8192;
    for( j=0; j<SEND_ITER; j++ )
    {
        // Send the buffer
        if( send( s, pBuf, (int)test, 0 ) < 0 )
        {
            ConPrintf("send failed (%d)\n",fdError());
            break;
        }
    }

    if( j == SEND_ITER )
    {
        endS = llTimerGetTime( &endMS );
        endS -= startS;
        endS *= 1000;
        endMS += endS;
        endMS -= startMS;
        endS = (endMS+50)/100;
        ConPrintf("Passed in %d ms, %d bytes/sec\n",endMS,(j*test*10)/endS);
    }

leave:
    if( pBuf )
        mmBulkFree( pBuf );
    if( s != INVALID_SOCKET )
        fdClose( s );

    ConPrintf("== End TCP Send Test ==\n\n");
}

static void RaceTest( IPN IPAddr )
{
    SOCKET  s1 = INVALID_SOCKET;
    SOCKET  s2 = INVALID_SOCKET;
    char    buffer[128];
    struct  sockaddr_in sin1;
    int     rc,size;

    ConPrintf("\n== Test Listen/Accept Race Condition ==\n");

    // Create test socket1
    s1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( s1 == INVALID_SOCKET )
    {
        ConPrintf("failed socket create (%d)\n",fdError());
        goto leave;
    }

    // Prepare address for bind
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family      = AF_INET;
    sin1.sin_len         = sizeof( sin1 );
    sin1.sin_addr.s_addr = IPAddr;
    sin1.sin_port        = htons(12345);

    // Bind the socket
    if( bind( s1, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed bind (%d)\n",fdError());
        goto leave;
    }

    // Start listening
    if( listen( s1, 1) < 0 )
    {
        ConPrintf("failed listen (%d)\n",fdError());
        goto leave;
    }

    // Create test socket2
    s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( s2 == INVALID_SOCKET )
    {
        ConPrintf("failed socket create2 (%d)\n",fdError());
        goto leave;
    }

    // Connect the socket
    if( connect( s2, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed connect (%d)\n",fdError());
        goto leave;
    }

    // Send some test data
    if( send( s2, "Hello World", 11, 0 ) < 0 )
    {
        ConPrintf("failed send (%d)\n",fdError());
        goto leave;
    }

    // Close test socket2
    fdClose( s2 );
    s2 = INVALID_SOCKET;

    // Get the accept socket
    size = sizeof( sin1 );
    s2 = accept( s1, (PSA)&sin1, &size );
    if( s2 == INVALID_SOCKET )
    {
        ConPrintf("failed accept (%d)\n",fdError());
        goto leave;
    }

    for(;;)
    {
        rc = recv( s2, buffer, 127, MSG_PEEK );

        if( rc >= 0 )
        {
            ConPrintf("Successful PEEK read (%d bytes)\n",rc);
            buffer[rc]=0;
            if( rc )
                ConPrintf("Data = '%s'\n",buffer);
        }
        else
        {
            ConPrintf("PEEK Read returned %d, error = %d\n",rc,fdError());
            break;
        }
        rc = recv( s2, buffer, 127, 0 );

        if( rc >= 0 )
        {
            ConPrintf("Successful read (%d bytes)\n",rc);
            buffer[rc]=0;
            if( rc )
                ConPrintf("Data = '%s'\n",buffer);
            else
                break;
        }
        else
        {
            ConPrintf("Read returned %d, error = %d\n",rc,fdError());
            break;
        }
     }

leave:
     if( s2 != INVALID_SOCKET )
         fdClose(s2);
     if( s1 != INVALID_SOCKET )
         fdClose(s1);

    ConPrintf("== End Test ==\n\n");
}

static void ShutdownTest( IPN IPAddr )
{
    SOCKET  s1 = INVALID_SOCKET;
    SOCKET  s2 = INVALID_SOCKET;
    SOCKET  s3 = INVALID_SOCKET;
    char    buffer[128];
    struct  sockaddr_in sin1;
    int     rc,size;

    ConPrintf("\n== Test shutdown(WRITE) Condition ==\n");

    // Create test socket1
    s1 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( s1 == INVALID_SOCKET )
    {
        ConPrintf("failed socket create (%d)\n",fdError());
        goto leave;
    }

    // Prepare address for bind
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family      = AF_INET;
    sin1.sin_len         = sizeof( sin1 );
    sin1.sin_addr.s_addr = IPAddr;
    sin1.sin_port        = htons(12345);

    // Bind the socket
    if( bind( s1, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed bind (%d)\n",fdError());
        goto leave;
    }

    // Start listening
    if( listen( s1, 1) < 0 )
    {
        ConPrintf("failed listen (%d)\n",fdError());
        goto leave;
    }

    // Create test socket2
    s2 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( s2 == INVALID_SOCKET )
    {
        ConPrintf("failed socket create2 (%d)\n",fdError());
        goto leave;
    }

    // Connect the socket
    if( connect( s2, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed connect (%d)\n",fdError());
        goto leave;
    }

    // Shut down the write side
    if( shutdown( s2, SHUT_WR ) < 0 )
    {
        ConPrintf("failed shutdown (%d)\n",fdError());
        goto leave;
    }

    // Get the accept socket
    size = sizeof( sin1 );
    s3 = accept( s1, (PSA)&sin1, &size );
    if( s3 == INVALID_SOCKET )
    {
        ConPrintf("failed accept (%d)\n",fdError());
        goto leave;
    }

    // Send some test data on s3
    if( send( s3, "Hello World", 11, 0 ) < 0 )
    {
        ConPrintf("failed send (%d)\n",fdError());
        goto leave;
    }

    // Close test socket3
    fdClose( s3 );
    s3 = INVALID_SOCKET;

    // We should be able to read data on s2
    for(;;)
    {
        rc = recv( s2, buffer, 127, MSG_PEEK );

        if( rc >= 0 )
        {
            ConPrintf("Successful PEEK read (%d bytes)\n",rc);
            buffer[rc]=0;
            if( rc )
                ConPrintf("Data = '%s'\n",buffer);
        }
        else
        {
            ConPrintf("PEEK Read returned %d, error = %d\n",rc,fdError());
            break;
        }
        rc = recv( s2, buffer, 127, 0 );

        if( rc >= 0 )
        {
            ConPrintf("Successful read (%d bytes)\n",rc);
            buffer[rc]=0;
            if( rc )
                ConPrintf("Data = '%s'\n",buffer);
            else
                break;
        }
        else
        {
            ConPrintf("Read returned %d, error = %d\n",rc,fdError());
            break;
        }
     }

leave:
     if( s2 != INVALID_SOCKET )
         fdClose(s2);
     if( s1 != INVALID_SOCKET )
         fdClose(s1);

    ConPrintf("== End Test ==\n\n");
}


//----------------------------------------------------------------------
// TimeoutTest()
//
// Test timeouts on TCP socket read and select
//----------------------------------------------------------------------
static void TimeoutTest()
{
    SOCKET  s;
    struct  sockaddr_in sin1;
    char    Buffer[32];
    struct  timeval timeout;
    fd_set  ibits;
    int     cnt;

    ConPrintf("\n== Start fdSelect()/recv() Timeout Test ==\n");

    // Create test socket
    s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if( s == INVALID_SOCKET )
    {
        ConPrintf("failed socket create (%d)\n",fdError());
        goto leave;
    }

    // Bind socket to port 12345 (we don't want any data)
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family      = AF_INET;
    sin1.sin_len         = sizeof( sin1 );
    sin1.sin_port        = htons(12345);

    if( bind( s, &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed socket bind (%d)\n",fdError());
        goto leave;
    }

    ConPrintf("\nSetting fdSelect() timeout to 2 sec (1s + 1000000uS)\n");

    timeout.tv_sec  = 1;
    timeout.tv_usec = 1000000;

    FD_ZERO(&ibits);
    FD_SET(s, &ibits);

    ConPrintf("Calling fdSelect() ...");
    cnt = fdSelect( (int)s, &ibits, 0, 0, &timeout );
    if( !cnt )
        ConPrintf(" timeout!\n");
    else
        ConPrintf(" input data detected - error!\n");

    ConPrintf("\nCalling fdSelect() [with no descriptors]...");
    cnt = fdSelect( (int)s, 0, 0, 0, &timeout );
    if( !cnt )
        ConPrintf(" timeout!\n");
    else
        ConPrintf(" input data detected - error!\n");

    ConPrintf("\nSetting recv() timeout to 2 sec (1s + 1000000uS)\n");

    setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof( timeout ) );

    ConPrintf("Calling recv() ...");
    cnt = recv( s, Buffer, 32, 0 );
    if( cnt < 0 )
    {
        if( fdError() == EWOULDBLOCK )
            ConPrintf(" timeout!\n");
        else
            ConPrintf(" unexpected error %d\n",fdError());
    }
    else
        ConPrintf(" input data detected - error!\n");

leave:
    if( s != INVALID_SOCKET )
        fdClose( s );

    ConPrintf("\n== End Timeout Test ==\n\n");
}


//----------------------------------------------------------------------
// ReuseTest()
//
// Test Socket Reuse
//----------------------------------------------------------------------
static void ReuseTest(IPN IPAddr)
{
    SOCKET  s;
    struct  sockaddr_in sin1;

    ConPrintf("\n== Test Error Socket Resue Test ==\n");

    // Create test socket
    s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if( s == INVALID_SOCKET )
    {
        ConPrintf("failed socket create (%d)\n",fdError());
        goto leave;
    }

    // Prepare address for connect
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family      = AF_INET;
    sin1.sin_len         = sizeof( sin1 );
    sin1.sin_addr.s_addr = IPAddr;
    sin1.sin_port        = htons(12345);

    // Connect the socket
    ConPrintf("Connecting to local port 12345 (should fail)\n");
    if( connect( s, (PSA) &sin1, sizeof(sin1) ) >=0 )
    {
        ConPrintf("First connect passed unexpectedly - abort\n");
        goto leave;
    }
    ConPrintf("Connect failed (%d)\n",fdError());

    sin1.sin_port        = htons(7);

    // Connect the socket again
    ConPrintf("Connecting to local port 7 (should pass)\n");
    if( connect( s, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("Connect failed (%d)\n",fdError());
        goto leave;
    }

    ConPrintf("Connect Passed\n");

leave:
    if( s != INVALID_SOCKET )
        fdClose( s );

    ConPrintf("\n== End Socket Error Reuse Test ==\n\n");
}


//----------------------------------------------------------------------
// ShareTest()
//
// Test ECHO with a shared TCP socket using two tasks
//----------------------------------------------------------------------
static void ShareTest( IPN IPAddr )
{
    SOCKET  s;
    struct  sockaddr_in sin1;
    struct  timeval timeout;
    fd_set  xbits;

    ConPrintf("\n== Start Shared TCP Socket Echo Test ==\n");

    // Create test socket
    s = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
    if( s == INVALID_SOCKET )
    {
        ConPrintf("failed socket create (%d)\n",fdError());
        goto leave;
    }

    // Prepare address for connect
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family      = AF_INET;
    sin1.sin_len         = sizeof( sin1 );
    sin1.sin_addr.s_addr = IPAddr;
    sin1.sin_port        = htons(7);

    // Configure our timeout to be 5 seconds
    timeout.tv_sec  = 5;
    timeout.tv_usec = 0;
    setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof( timeout ) );
    setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof( timeout ) );

    // Connect socket
    if ( connect( s, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed connect (%d)\n",fdError());
        goto leave;
    }

    // Create a thread to receive data
    if( !TaskCreate( TcpShareRX, "Receiver", OS_TASKPRINORM, 0x1000,
                     (UINT32)TaskSelf(), (UINT32)s, 0 ) )
        goto leave;

    // Create a thread to send us data
    TaskCreate( TcpShareTX, "Sender", OS_TASKPRINORM, 0x1000,
                (UINT32)TaskSelf(), (UINT32)s, 0 );

    ConPrintf("Main task waiting with select() for test to complete\n");
    FD_ZERO(&xbits);
    FD_SET(s, &xbits);
    fdSelect( (int)s, 0, 0, &xbits, 0 );

leave:
    if( s != INVALID_SOCKET )
        fdClose( s );

    ConPrintf("== End Shared TCP Socket Echo Test ==\n\n");
}


//----------------------------------------------------------------------
// TcpShareRX()
//
// Task to receive data on a shared socket
//----------------------------------------------------------------------
static void TcpShareRX( HANDLE hTaskParent, SOCKET s, int *pFlag )
{
    char    *pBufRx;
    HANDLE  hBuffer;
    UINT32  test,i,j;
    int     k;
    UINT32  startS, startMS;
    UINT32  endS, endMS;

    fdOpenSession( TaskSelf() );

    // Here we'll "share" socket 's' to up its reference count
    fdShare(s);

    ConPrintf("RX Child task spawned successfully\n");

    startS = llTimerGetTime( &startMS );

    // Start Test

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -