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

📄 recv.c

📁 本软件是TI公司免费提供的网络开发包 现在好象很难找到,有黑心的公司把它改一改,就卖价5000元,对网络开发和网络驱动开发有参考价值
💻 C
字号:
/*
 *  Copyright 2006 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *
 *  @(#) TCP/IP_Network_Developers_Kit 1.91.00.08 08-22-2006 (ndk-a08)
 */
//---------------------------------------------------------------------------
// TEST
//---------------------------------------------------------------------------
// RECV.C
//
// Author: Michael Denio
// Copyright 1999 by Texas Instruments Inc.
//---------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <winsock.h>

#define REQUEST_SIZE    46720

int main( int argc, char *argv[] )
{
    WORD         wVersionRequested;
    WSADATA      wsaData;
    SOCKET       s;
    struct sockaddr_in sin;
    char         *pBuf = 0;
    int          i,trial,tmp,skipoutput,update = 1;
    int          tmp1,tmp2,tmp3,tmp4;
    time_t       ts,tn;
    unsigned int test,bytes;

    if( (argc != 2 && argc != 3) ||
        sscanf(argv[1],"%03d.%03d.%03d.%03d",&tmp1,&tmp2,&tmp3,&tmp4)!=4 ||
        (tmp1 < 0 || tmp1 > 255 || tmp2 < 0 || tmp2 > 255 ||
         tmp3 < 0 || tmp3 > 255 || tmp4 < 0 || tmp4 > 255) ||
         (argc == 3 && !sscanf(argv[2],"%d",&update)) )
    {
        printf("\nUsage: RECV <x.x.x.x> [update]\n");
        exit(0);
    }

    tmp1 |= tmp2 << 8;
    tmp1 |= tmp3 << 16;
    tmp1 |= tmp4 << 24;

    wVersionRequested = MAKEWORD(1, 1);
    i = WSAStartup(wVersionRequested, &wsaData);
    if (i != 0)
    {
        printf("\r\nUnable to initialize WinSock for host info");
        exit(0);
    }

    s = socket( AF_INET, SOCK_STREAM, 0 );
    if( s < 0 )
    {
        printf("failed socket (%d)\n",WSAGetLastError());
        goto leave;
    }

    sin.sin_family      = AF_INET;
    sin.sin_addr.s_addr = tmp1;
    sin.sin_port        = htons(1000);

    if ( connect( s, &sin, sizeof(sin) ) < 0 )
    {
        printf("failed connect (%d)\n",WSAGetLastError());
        goto leave;
    }

    // Allocate a working buffer
    if( !(pBuf = malloc( REQUEST_SIZE )) )
    {
        printf("failed temp buffer allocation\n");
        goto leave;
    }

    test       = REQUEST_SIZE;
    trial      = 0;
    skipoutput = update;

    ts = time(0);
    bytes = 0;

    while( !kbhit() )
    {
        trial++;
        skipoutput--;

        // Send the buffer
        if( !skipoutput )
            printf("%d Requesting %d bytes ... ",trial,test);
        *(int *)pBuf = test;
        if( send( s, pBuf, sizeof(int), 0 ) < 0 )
        {
            printf("send failed (%d)\n",WSAGetLastError());
            break;
        }

        // Try and receive the test pattern
        if( !skipoutput )
            printf("receive... ");
        i = 0;
        while( i < (int)test )
        {
            tmp = recv( s, pBuf+i, REQUEST_SIZE-i, 0 );
            if( tmp < 0 )
            {
                printf("recv failed (%d)\n",WSAGetLastError());
                break;
            }
            if( tmp == 0 )
            {
                printf("recv failed - no data\n");
                break;
            }
            i += tmp;
        }

        // Verify reception size
        if( i != test )
        {
            printf("received %d (not %d) bytes\n",i,test);
            break;
        }

        if( (bytes+test) < bytes )
        {
            ts = time(0);
            bytes = 0;
            skipoutput = update;
        }
        else
        {
            bytes += test;

            if( !skipoutput )
            {
                tn = time(0) - ts;
                if( tn )
                   printf("passed - %u bytes/s\n",bytes/tn);
                else
                   printf("passed\n");
                skipoutput = update;
            }
        }
    }

leave:
    if( pBuf )
        free( pBuf );
    if( s >= 0 )
        closesocket( s );

    WSACleanup();
}

⌨️ 快捷键说明

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