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

📄 tester.c

📁 在dsk6455开发平台上实现dsp与主机之间的通信实例
💻 C
字号:
/*
 *  ============================================================
 *  (c) Copyright 2007 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.
 *  ============================================================
 *  Automated Revision Information
 *  Changed: $Date: 2007-10-15 15:13:14 -0700 (Mon, 15 Oct 2007) $
 *  Revision: $Revision: 4244 $ 
 */
/* 
 *  ======== PCTester.c ========
 *
 *  Window PC Tester benchmark application
 */
#include <stdio.h>
#include <windows.h>
#include <winsock.h>

#include "../../noncopytcp.h"

int recvclient();
int sendclient();
int platform_sockinit();
int platform_sockuninit();
int platform_sockerr();


int main(int argc, char** args)
{
    
    int rv = 0;
  
    rv = platform_sockinit();

    if (rv != 0) {
        return rv;
    }

    printf("\nNon Copy TCP Tester Benchmark\n");

    sendclient();

    recvclient();  
  
    rv = platform_sockuninit();

    return rv;
}


int platform_sockinit()
{
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 2, 2 );

    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        printf("\r\nUnable to initialize WinSock for host info");
        return err;
    }
 
    if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
        WSACleanup( );
        return -1; 
    }
    return 0;
}

int platform_sockuninit()
{
    return WSACleanup();
}

int platform_sockerr()
{
    return WSAGetLastError();
}


int recvclient()
{
    char     *IPAddrRecv = TESTEE_IPADDR_STRING;
    SOCKET   stcp = INVALID_SOCKET;
    struct   sockaddr_in sin1;
    int      count, i, j, k,trial, skipoutput,tmp, bufferSize;
    char     *pBuf = 0;

    printf("    Receive Task started\n");

    for (i=0 ; i < sizeof(frameSize)/sizeof(frameSize[0]); i++) {

        for (j=0;  j < sizeof(frameRate)/sizeof(frameRate[0]);  j++) {

            printf("    Requesting %d bytes frames at ",frameSize[i]);

            if (frameRate[j] == 0) {
                printf ("maximum frame rate ...\n");
            }
            else {
                printf ("%d ms frame rate ...\n", frameRate[j]);
            }

            // Create the main TCP socket
            stcp = socket(AF_INET, SOCK_STREAM, 0);
            if( stcp == INVALID_SOCKET ) {
                goto leave;
            }
            
            // Set Port = 1000, IP address = IPAddrRecv

            memset( &sin1, 0, sizeof(sin1) );

            sin1.sin_family = AF_INET;
            sin1.sin_addr.s_addr = inet_addr(IPAddrRecv);
            sin1.sin_port   = htons(1000);

            // Connect socket
            if ( connect( stcp, &sin1, sizeof(sin1) ) < 0 ) {
                printf("failed to connect\n");
                goto leave;
            }
            
            // Allocate a working buffer
            if( !(pBuf = malloc( frameSize[i] )) ) {
                printf("failed temp buffer allocation\n");
                goto leave;
            }

            bufferSize = frameSize[i];
            trial      = 0;
            skipoutput = BENCH_FRAMES;

            /* Send data size requested */
            *(int *)pBuf = bufferSize;

            if( send( stcp, pBuf, sizeof(int), 0 ) < 0 ) {
                printf("    ...send failed (%d)\n",platform_sockerr());
                goto leave;
            }

            // Loop through the number of frames 
            for (count =0; count < BENCH_FRAMES; count++) {
                trial++;
                skipoutput--;

                // Try and receive the test pattern
                if( !skipoutput ) {
                    printf("    ...received %d frames\n", trial);
                }
            
                k = 0;

                while( k < (int)bufferSize ) {
                    tmp = recv( stcp, pBuf+k, bufferSize-k, 0 );
                    if( tmp < 0 ) {
                        printf("recv failed (%d)\n",platform_sockerr());
                        break;
                    }
                    if( tmp == 0 ) {
                        printf("recv failed - no data\n");
                        break;
                    }
                    k += tmp;
                }

               if (frameRate[j] > 0) {
                   Sleep(frameRate[j]);
               }

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

            }

leave:
            if( pBuf ) {
                free( pBuf );
            }

            // We only get here on an error - close the sockets
            if( stcp != INVALID_SOCKET ) {
                closesocket( stcp );
            }

            Sleep (10);
        
        }
    }

    printf("    Receive Task terminated\n");

    return 0;
}

int sendclient()
{
    char     *IPAddrSend = TESTEE_IPADDR_STRING;
    SOCKET   stcp = INVALID_SOCKET;
    struct   sockaddr_in sin1;
    int      count, i, j, bufferSize;
    char     *pBuf = "Hello World\n";

    printf("    Sending Task started\n");

    for (i=0 ; i < sizeof(frameSize)/sizeof(frameSize[0]); i++) {
    
        for (j=0;  j < sizeof(frameRate)/sizeof(frameRate[0]);  j++) {

            printf ("    Sending %d frames of %d bytes at ", BENCH_FRAMES, frameSize[i]);
            
            if (frameRate[j] == 0) {
                printf ("maximum frame rate\n");
            } 
            else {
                printf ("%d ms frame rate\n", frameRate[j]);
            }
            
            // Create the main TCP listen socket
            stcp = socket(AF_INET, SOCK_STREAM, 0);
            if( stcp == INVALID_SOCKET ) {
                printf("Fail socket, %d\n", platform_sockerr());
                goto leave;
            }

            // Set Port = 1001, IP address = IPAddrSend
            memset( &sin1, 0, sizeof(sin1) );

            sin1.sin_family = AF_INET;
            sin1.sin_addr.s_addr = inet_addr( IPAddrSend );
            sin1.sin_port   = htons(1001);

            // Connect socket
            if ( connect( stcp, &sin1, sizeof(sin1) ) < 0 ) {
                printf("Fail connect, %d\n", platform_sockerr());
                goto leave;
            }

            bufferSize = frameSize[i];

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

            *(int *)pBuf = bufferSize;


            if( send( stcp, pBuf, sizeof(int), 0 ) < 0 ) {
                printf("send failed (%d)\n",platform_sockerr());
                goto leave;
            }

            for( count =0; count < BENCH_FRAMES; count++ ) {

                if( send( stcp, pBuf, (int)bufferSize, 0 ) < 0 ) {
                    printf("send failed (%d)\n",platform_sockerr());
                    break;
                }

                if (frameRate[j] > 0) {
                    Sleep(frameRate[j]);
                }

            }

leave:
            if( pBuf ) {
               free( pBuf );
            }

            // We only get here on an error - close the sockets
            if( stcp != INVALID_SOCKET ) {
                closesocket( stcp );
            }

            Sleep(10);
        }
    }
    
    printf("    Send Task terminated\n");
    
    return 0;
}

⌨️ 快捷键说明

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