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

📄 tcpperfserver.cpp

📁 CIRRUS 93XX系列windows mobile 6.0 BSP
💻 CPP
字号:
/**********************************************************************
#                                                                      
# Filename: TcpPerfServer.cpp
#                                                                      
# Description:
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
# ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
# PARTICULAR PURPOSE.
#
# Use of this source code is subject to the terms of the Cirrus end-user
# license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
# If you did not accept the terms of the EULA, you are not authorized to 
# use this source code. For a copy of the EULA, please see the 
# EULA.RTF on your install media.
#
# Copyright(c) Cirrus Logic Corporation 2007, All Rights Reserved                       
#                                                                      
#**********************************************************************/
// TcpPerfServer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdio.h>


#define DEFAULT_PORT  "1234"
#define BUFFER_SIZE           65536  //64K. The size of one block of data            
#define NUM_OF_BLOCKS_TO_TEST 1600 //number of blocks to be transmitted 
                                 //in the tests of Tx and Rx 

#define PINGPONG_FRAME_SIZE   1448 //1448 bytes. The size of the frame used in Ping-pong test.
#define PINGPONG_LOOP_COUNT   16000 //The loop count of the ping-pong test.

#define DEFAULT_FAMILY  AF_UNSPEC
#define DEFAULT_SOCKTYPE SOCK_STREAM //For TCP connection

char pBuf[BUFFER_SIZE];

int _tmain(int argc, _TCHAR* argv[])
{
    SOCKET sock, SockServ[FD_SETSIZE];
    int nFamily = DEFAULT_FAMILY;
    int nSockType = DEFAULT_SOCKTYPE;
    char *szPort = DEFAULT_PORT;
    SOCKADDR_STORAGE ssRemoteAddr;
    int i, nNumSocks, cbRemoteAddrSize, cbXfer, cbTotalRecvd;
    WSADATA wsaData;
    ADDRINFO Hints, *AddrInfo = NULL, *AI;
    fd_set fdSockSet;
    char szRemoteAddrString[128];
	int index;
	unsigned long blocksLeft;
	
	//Prepare Tx data.
    memset(pBuf, 0x5a, BUFFER_SIZE);

    if(WSAStartup(MAKEWORD(2,2), &wsaData))
    {
		printf("Error: WSAStartup() failed!\r\n");
        // WSAStartup failed
        return 1;
    }

    sock = INVALID_SOCKET;

    for(i = 0; i < FD_SETSIZE; i++)
        SockServ[i] = INVALID_SOCKET;

    //
    // Get a list of available addresses to serve on
    //

    memset(&Hints, 0, sizeof(Hints));
    Hints.ai_family = nFamily;
    Hints.ai_socktype = nSockType;
    Hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;

    if( getaddrinfo(NULL, szPort, &Hints, &AddrInfo) )
    {
        printf("ERROR: getaddrinfo failed with error %d\r\n", WSAGetLastError());
        goto Cleanup;
    }

    //
    // Create a list of serving sockets, one for each address
    //

    i = 0;
    for(AI = AddrInfo; AI != NULL; AI = AI->ai_next) 
    {
        if (i == FD_SETSIZE) 
        {
            // getaddrinfo returned more addresses than we could use
            break;
        }

        if(AI->ai_family == PF_INET) // only want PF_INET 
        {
            SockServ[i] = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
            if (SockServ[i] != INVALID_SOCKET)
            {
                if (bind(SockServ[i], AI->ai_addr, AI->ai_addrlen) == SOCKET_ERROR)
                    closesocket(SockServ[i]);
                else 
                {
                    if(nSockType == SOCK_STREAM)
                    {
                        if (listen(SockServ[i], 5) == SOCKET_ERROR)
                        {
                            closesocket(SockServ[i]);
                            continue;
                        }
                    }

                    printf( "Socket 0x%08x ready for connection with %hs family, %hs type, on port %hs\r\n", 
                        SockServ[i], 
                        (AI->ai_family == AF_INET) ? "AF_INET" : ((AI->ai_family == AF_INET6) ? "AF_INET6" : "UNKNOWN"),
                        (AI->ai_socktype == SOCK_STREAM) ? "TCP" : ((AI->ai_socktype == SOCK_DGRAM) ? "UDP" : "UNKNOWN"),
                        szPort);
                    i++;
                }
            }
        }
    }
    
    freeaddrinfo(AddrInfo);
    
    if (i == 0) 
    {
        printf("ERROR: Unable to serve on any address. Error = %d\r\n", WSAGetLastError());
        goto Cleanup;
    }

    //
    // Wait for incomming data/connections
    //

    nNumSocks = i;

    FD_ZERO(&fdSockSet);

    for (i = 0; i < nNumSocks; i++)    // want to check all available sockets
        FD_SET(SockServ[i], &fdSockSet);

    if (select(nNumSocks, &fdSockSet, 0, 0, NULL) == SOCKET_ERROR)
    {
        printf("ERROR: select() failed with error = %d\r\n", WSAGetLastError());
        goto Cleanup;
    }

    for (i = 0; i < nNumSocks; i++)    // check which socket is ready to process
    {
        if (FD_ISSET(SockServ[i], &fdSockSet))    // proceed for connected socket
        {
            FD_CLR(SockServ[i], &fdSockSet);
            if(nSockType == SOCK_STREAM)
            {
                cbRemoteAddrSize = sizeof(ssRemoteAddr);
                sock = accept(SockServ[i], (SOCKADDR*)&ssRemoteAddr, &cbRemoteAddrSize);
                if(sock == INVALID_SOCKET) 
                {
                    printf("ERROR: accept() failed with error = %d\r\n", WSAGetLastError());
                    goto Cleanup;
                }

                printf("Accepted TCP connection from socket 0x%08x\r\n", sock);
            }
            else
            {
                sock = SockServ[i];
                printf("UDP data available on socket 0x%08x\r\n", sock);
            }
            break;        // Only need one socket
        }
    }

    //
    // Receive data from a client
    //

   cbRemoteAddrSize = sizeof(ssRemoteAddr);
   cbTotalRecvd = 0;
    do
    {
        cbXfer = recvfrom(sock, pBuf+cbTotalRecvd, 1024-cbTotalRecvd, 0,
            (SOCKADDR *)&ssRemoteAddr, &cbRemoteAddrSize);
        if(cbXfer == SOCKET_ERROR)
        {
           printf("ERROR: Couldn't receive the data! Error = %d\r\n", WSAGetLastError());
		   break;
		}
        cbTotalRecvd += cbXfer;
    } while(cbXfer > 0 && cbTotalRecvd < 1024);

    if(cbXfer != 1024)
    {
        printf("ERROR: Didn't get all the expected data from the client!\r\n");
        goto Cleanup;
    }

    if(nSockType == SOCK_STREAM)
    {
        cbRemoteAddrSize = sizeof(ssRemoteAddr);
        getpeername(sock, (SOCKADDR *)&ssRemoteAddr, &cbRemoteAddrSize);
    }
    
    if (getnameinfo((SOCKADDR *)&ssRemoteAddr, cbRemoteAddrSize,
        szRemoteAddrString, sizeof(szRemoteAddrString), NULL, 0, NI_NUMERICHOST) != 0)
        strcpy(szRemoteAddrString, "");

    printf("SUCCESS - Received %d bytes from client %hs\r\n", cbTotalRecvd, szRemoteAddrString);


    //
	// Do Rx Perf Test on the Client side.
	// Server does Transmit.
    //
    blocksLeft=NUM_OF_BLOCKS_TO_TEST;

    // Send the data back to the client
    while (blocksLeft > 0)
	{
      cbXfer = 0;
	  index=0;
      do
      {
        cbXfer = sendto(sock, pBuf+index, BUFFER_SIZE-index, 0, (SOCKADDR *)&ssRemoteAddr, cbRemoteAddrSize);
        index += cbXfer;
      }   while(cbXfer > 0 &&  index < BUFFER_SIZE);
      if(index != BUFFER_SIZE)
	  {
        printf("ERROR: S5 sendto() error=%d cbXfer=%d\r\n", WSAGetLastError(), cbXfer);
		break;
	  }

      blocksLeft--;
	}



    //
	// Do Tx Perf Test on the Client side.
	// Sever does Receive.
    //
    blocksLeft=NUM_OF_BLOCKS_TO_TEST;

    // Receive the data  from the client
    while (blocksLeft > 0)
	{
      cbXfer = 0;
	  index=0;
      do
      {
        cbXfer = recvfrom(sock, pBuf+index, BUFFER_SIZE-index, 0, (SOCKADDR *)&ssRemoteAddr, &cbRemoteAddrSize);
        index += cbXfer;
      }   while(cbXfer > 0 &&  index < BUFFER_SIZE);
      if(index != BUFFER_SIZE)
	  {
        printf("ERROR: S15 recvfrom() error=%d cbXfer=%d\r\n", WSAGetLastError(), cbXfer);
		break;
	  }

      blocksLeft--;
	}


    //
	// Do Ping-Pong Perf Test on the Client side.
	// Sever does Receive then Tx.
    //
    blocksLeft=PINGPONG_LOOP_COUNT;

    // Ping-Pong the data  from/to the client
    while (blocksLeft > 0)
	{
      cbXfer = 0;
	  index=0;
      do
      {
        cbXfer = recvfrom(sock, pBuf+index, PINGPONG_FRAME_SIZE-index, 0, (SOCKADDR *)&ssRemoteAddr, &cbRemoteAddrSize);
        index += cbXfer;
      }   while(cbXfer > 0 &&  index < PINGPONG_FRAME_SIZE);
      if(index != PINGPONG_FRAME_SIZE)
	  {
        printf("ERROR: S15 recvfrom() error=%d cbXfer=%d\r\n", WSAGetLastError(), cbXfer);
		break;
	  }

      cbXfer = 0;
	  index=0;
      do
      {
        cbXfer = sendto(sock, pBuf+index, PINGPONG_FRAME_SIZE-index, 0, (SOCKADDR *)&ssRemoteAddr, cbRemoteAddrSize);
        index += cbXfer;
      }   while(cbXfer > 0 &&  index < PINGPONG_FRAME_SIZE);
      if(index != PINGPONG_FRAME_SIZE)
	  {
        printf("ERROR: S16 sendto() error=%d cbXfer=%d\r\n", WSAGetLastError(), cbXfer);
		break;
	  }

      blocksLeft--;
	}




Cleanup:

    for(i = 0; i < nNumSocks && SockServ[i] != INVALID_SOCKET; i++)
        closesocket(SockServ[i]);

    if(sock != INVALID_SOCKET)
    {
        shutdown(sock, SD_BOTH);
        closesocket(sock);
    }

    WSACleanup();

    return 0;
}

⌨️ 快捷键说明

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