appclient.cpp

00001 #ifndef WIN32
00002    #include <unistd.h>
00003    #include <cstdlib>
00004    #include <cstring>
00005    #include <netdb.h>
00006 #else
00007    #include <winsock2.h>
00008    #include <ws2tcpip.h>
00009    #include <wspiapi.h>
00010 #endif
00011 #include <iostream>
00012 #include <udt.h>
00013 #include "cc.h"
00014 #include "test_util.h"
00015 
00016 using namespace std;
00017 
00018 #ifndef WIN32
00019 void* monitor(void*);
00020 #else
00021 DWORD WINAPI monitor(LPVOID);
00022 #endif
00023 
00024 int main(int argc, char* argv[])
00025 {
00026    if ((3 != argc) || (0 == atoi(argv[2])))
00027    {
00028       cout << "usage: appclient server_ip server_port" << endl;
00029       return 0;
00030    }
00031 
00032    // Automatically start up and clean up UDT module.
00033    UDTUpDown _udt_;
00034 
00035    struct addrinfo hints, *local, *peer;
00036 
00037    memset(&hints, 0, sizeof(struct addrinfo));
00038 
00039    hints.ai_flags = AI_PASSIVE;
00040    hints.ai_family = AF_INET;
00041    hints.ai_socktype = SOCK_STREAM;
00042    //hints.ai_socktype = SOCK_DGRAM;
00043 
00044    if (0 != getaddrinfo(NULL, "9000", &hints, &local))
00045    {
00046       cout << "incorrect network address.\n" << endl;
00047       return 0;
00048    }
00049 
00050    UDTSOCKET client = UDT::socket(local->ai_family, local->ai_socktype, local->ai_protocol);
00051 
00052    // UDT Options
00053    //UDT::setsockopt(client, 0, UDT_CC, new CCCFactory<CUDPBlast>, sizeof(CCCFactory<CUDPBlast>));
00054    //UDT::setsockopt(client, 0, UDT_MSS, new int(9000), sizeof(int));
00055    //UDT::setsockopt(client, 0, UDT_SNDBUF, new int(10000000), sizeof(int));
00056    //UDT::setsockopt(client, 0, UDP_SNDBUF, new int(10000000), sizeof(int));
00057    //UDT::setsockopt(client, 0, UDT_MAXBW, new int64_t(12500000), sizeof(int));
00058 
00059    // Windows UDP issue
00060    // For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold
00061    #ifdef WIN32
00062       UDT::setsockopt(client, 0, UDT_MSS, new int(1052), sizeof(int));
00063    #endif
00064 
00065    // for rendezvous connection, enable the code below
00066    /*
00067    UDT::setsockopt(client, 0, UDT_RENDEZVOUS, new bool(true), sizeof(bool));
00068    if (UDT::ERROR == UDT::bind(client, local->ai_addr, local->ai_addrlen))
00069    {
00070       cout << "bind: " << UDT::getlasterror().getErrorMessage() << endl;
00071       return 0;
00072    }
00073    */
00074 
00075    freeaddrinfo(local);
00076 
00077    if (0 != getaddrinfo(argv[1], argv[2], &hints, &peer))
00078    {
00079       cout << "incorrect server/peer address. " << argv[1] << ":" << argv[2] << endl;
00080       return 0;
00081    }
00082 
00083    // connect to the server, implict bind
00084    if (UDT::ERROR == UDT::connect(client, peer->ai_addr, peer->ai_addrlen))
00085    {
00086       cout << "connect: " << UDT::getlasterror().getErrorMessage() << endl;
00087       return 0;
00088    }
00089 
00090    freeaddrinfo(peer);
00091 
00092    // using CC method
00093    //CUDPBlast* cchandle = NULL;
00094    //int temp;
00095    //UDT::getsockopt(client, 0, UDT_CC, &cchandle, &temp);
00096    //if (NULL != cchandle)
00097    //   cchandle->setRate(500);
00098 
00099    int size = 100000;
00100    char* data = new char[size];
00101 
00102    #ifndef WIN32
00103       pthread_create(new pthread_t, NULL, monitor, &client);
00104    #else
00105       CreateThread(NULL, 0, monitor, &client, 0, NULL);
00106    #endif
00107 
00108    for (int i = 0; i < 1000000; i ++)
00109    {
00110       int ssize = 0;
00111       int ss;
00112       while (ssize < size)
00113       {
00114          if (UDT::ERROR == (ss = UDT::send(client, data + ssize, size - ssize, 0)))
00115          {
00116             cout << "send:" << UDT::getlasterror().getErrorMessage() << endl;
00117             break;
00118          }
00119 
00120          ssize += ss;
00121       }
00122 
00123       if (ssize < size)
00124          break;
00125    }
00126 
00127    UDT::close(client);
00128    delete [] data;
00129    return 0;
00130 }
00131 
00132 #ifndef WIN32
00133 void* monitor(void* s)
00134 #else
00135 DWORD WINAPI monitor(LPVOID s)
00136 #endif
00137 {
00138    UDTSOCKET u = *(UDTSOCKET*)s;
00139 
00140    UDT::TRACEINFO perf;
00141 
00142    cout << "SendRate(Mb/s)\tRTT(ms)\tCWnd\tPktSndPeriod(us)\tRecvACK\tRecvNAK" << endl;
00143 
00144    while (true)
00145    {
00146       #ifndef WIN32
00147          sleep(1);
00148       #else
00149          Sleep(1000);
00150       #endif
00151 
00152       if (UDT::ERROR == UDT::perfmon(u, &perf))
00153       {
00154          cout << "perfmon: " << UDT::getlasterror().getErrorMessage() << endl;
00155          break;
00156       }
00157 
00158       cout << perf.mbpsSendRate << "\t\t" 
00159            << perf.msRTT << "\t" 
00160            << perf.pktCongestionWindow << "\t" 
00161            << perf.usPktSndPeriod << "\t\t\t" 
00162            << perf.pktRecvACK << "\t" 
00163            << perf.pktRecvNAK << endl;
00164    }
00165 
00166    #ifndef WIN32
00167       return NULL;
00168    #else
00169       return 0;
00170    #endif
00171 }

Generated on 9 Feb 2013 for barchart-udt-core-2.2.2 by  doxygen 1.6.1