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

📄 contest.c

📁 闻停开发板视频程序
💻 C
📖 第 1 页 / 共 4 页
字号:
    {
        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 Echo Client Test ==\n\n");
}

//----------------------------------------------------------------------
// SendTest()
//
// Test sending on a TCP socket
//----------------------------------------------------------------------
#define 	SEND_ITER       25000
static void SendTest( IPN IPAddr )
{
    SOCKET  s;
    struct  sockaddr_in sin1;
    UINT32  test,j;
    char    *pBuf = 0;
    struct  timeval timeout;
    UINT32  startS, startMS;
    UINT32  endS, endMS;
//	UINT32	send_buf[SEND_ITER];
    ConPrintf("\n== Start TCP Send 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(1001);// ( ((a>>8)&0xff) + ((a<<8)&0xff00) )

    // 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 ) );
	//SOL_SOCKET=0xFFFF; SO_SNDTIMEO=0x1005 send timeout, SO_RCVTIMEO=0x1006   receive timeout
    // Connect socket
    if ( connect( s, (PSA) &sin1, sizeof(sin1) ) < 0 )
    {
        ConPrintf("failed connect (%d)\n",fdError());
        goto leave;
    }

    // Allocate a working buffer
    if( !(pBuf = mmBulkAlloc( 8192 )) )
    {
        ConPrintf("failed temp buffer allocation\n");
        goto leave;
    }

    startS = llTimerGetTime( &startMS );

    // Start Test
    test = 8192;
	endMS = SEND_ITER;
    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;
        }
	   /*
		send_buf[j] =  send( s, pBuf, (int)test, 0 );
		if(j==16800)
			test = 8192;
		if(send_buf[j]<0)
        {
            ConPrintf("send failed (%d)\n",fdError());
            break;
        }
		if(j==16850)
			test = 8192;
		if(j==16900)
			test = 8192;
		if(j==17000)
			test = 8192;
		if(j==17050)
			test = 8192;	
			*/
    }

    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;
    }

⌨️ 快捷键说明

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