Guest User

Untitled

a guest
Mar 27th, 2018
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <float.h>
  6. #include <errno.h>  
  7.  
  8. #if defined (_MSC_VER )
  9. #   include <WinSock2.h>
  10. #   include <Ws2tcpip.h>
  11. #   pragma comment(lib, "ws2_32.lib")
  12. #   define INETPTON InetPton
  13. #else
  14. #   include <unistd.h>
  15. #   include <sys/socket.h>
  16. #   include <arpa/inet.h>
  17. #   include <netinet/tcp.h>
  18. #   define INETPTON inet_pton
  19. #endif
  20.  
  21. #define SERVER_PORT             36547
  22. #define DATA_BUFFER_SIZE        (1024 * 1024 * 4)         /* size of data content buffer that will be sent repeatedly */
  23. #define REPLY_BUFFER_SIZE       (1024)
  24. #define SOCKET_BUFFER_SIZE      (512 * 1024)
  25.  
  26. int dorecv(int socket_desc, char* buffer, int bufSize);
  27. int dosend(int socket_desc, char* buffer, int bufSize);
  28. int runServer();
  29. int runClient(const char* serverIp);
  30. double tick_sec();
  31. double currentLoad();
  32.  
  33. int main(int argc , char *argv[])
  34. {
  35.  
  36. #if defined (_MSC_VER )
  37.     WSADATA wsa;
  38.     if (WSAStartup(MAKEWORD(2, 0), &wsa))
  39.     {
  40.         printf("WSAStartup error\n");
  41.         return -1;
  42.     }
  43. #endif
  44.  
  45.     if (argc > 1)
  46.     {
  47.         printf("Run as client\n");
  48.         runClient(argv[1]);
  49.     }
  50.     else
  51.     {
  52.         printf("Run as server\n");
  53.         runServer();
  54.     }
  55.  
  56.     printf("Program ends!\n");
  57.  
  58.     return 0;
  59. }
  60.  
  61. int dorecv(int socket_desc, char* buffer, int bufSize)
  62. {
  63.     int bytesRead = 0;
  64.  
  65.     while (bytesRead < bufSize)
  66.     {
  67.         int r = recv(socket_desc , buffer + bytesRead, bufSize - bytesRead, 0);
  68.  
  69.         if (r <= 0)
  70.         {
  71.             printf("Could not receive (%s)\n", strerror(errno));
  72.             return -1;
  73.         }
  74.  
  75.         bytesRead += r;
  76.     }
  77.  
  78.     return 0;
  79. }
  80.  
  81. int dosend(int socket_desc, char* buffer, int bufSize)
  82. {
  83.     int written = 0, w;
  84.  
  85.     while (written < bufSize)
  86.     {
  87.         if ((w = send(socket_desc , buffer + written, bufSize - written, 0)) < 0)
  88.         {
  89.             printf("Could not send (%s)\n", strerror(errno));
  90.             return -1;
  91.         }
  92.  
  93.         written += w;
  94.     }
  95.  
  96.     return 0;
  97. }
  98.  
  99. double tick_sec()
  100. {
  101. #if defined (_MSC_VER )
  102.  
  103.     static LARGE_INTEGER frequency;
  104.     LARGE_INTEGER now;
  105.  
  106.     if (frequency.QuadPart == 0)
  107.     {
  108.         QueryPerformanceFrequency(&frequency);
  109.     }  
  110.  
  111.     QueryPerformanceCounter(&now);
  112.     return now.QuadPart / (double)frequency.QuadPart;
  113.  
  114. #else
  115.     struct timespec tp;
  116.    
  117.     if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
  118.     {
  119.         return 0;
  120.     }
  121.  
  122.     return (double)tp.tv_sec + (double)tp.tv_nsec / 1000000000.0;
  123. #endif
  124. }
  125.  
  126. int runServer()
  127. {
  128.     int sockt, sockfd, c;
  129.     struct sockaddr_in server, client;
  130.     socklen_t optLen = sizeof(int);
  131.     char* buffer = malloc(DATA_BUFFER_SIZE);
  132.     int sz = SOCKET_BUFFER_SIZE;
  133.     unsigned int i = 0;
  134.     double minVal = DBL_MAX, maxVal = DBL_MIN;
  135.     int shitCnt = 0;
  136.  
  137.     if (buffer == NULL)
  138.     {
  139.         printf("Could not allocate memory\n");
  140.         return -1;
  141.     }
  142.  
  143.     //Create socket
  144.     sockt = socket(AF_INET , SOCK_STREAM , 0);
  145.     if (sockt == -1)
  146.     {
  147.         printf("Could not create socket\n");
  148.         return -1;
  149.     }
  150.      
  151.     //Prepare the sockaddr_in structure
  152.     server.sin_family = AF_INET;
  153.     server.sin_addr.s_addr = INADDR_ANY;
  154.     server.sin_port = htons( SERVER_PORT );
  155.  
  156.     //Bind
  157.     if( bind(sockt, (struct sockaddr *)&server , sizeof(server)) < 0)
  158.     {
  159.         //print the error message
  160.         printf("bind failed. Error\n");
  161.         return 1;
  162.     }
  163.  
  164.     //Listen
  165.     listen(sockt , 1);
  166.      
  167.     //Accept and incoming connection
  168.     printf("Waiting for incoming connections...\n");
  169.  
  170.     //accept connection from an incoming client
  171.     c = sizeof(struct sockaddr_in);
  172.     sockfd = accept(sockt, (struct sockaddr *)&client, (socklen_t*)&c);
  173.     if (sockfd < 0)
  174.     {
  175.         printf("accept failed\n");
  176.         return -1;
  177.     }
  178.    
  179.     printf("Connection accepted, start receiving\n");
  180.  
  181.     if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char*)&sz, optLen) == -1)
  182.     {
  183.         printf("Could not set receive buffer size\n");
  184.         return -1;
  185.     }
  186.  
  187.     sz = 1;
  188.     if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&sz, optLen))
  189.     {
  190.         printf("Could not set TCP_NODELAY\n");
  191.         return -1;
  192.     }
  193.  
  194.     //Receive a message from client
  195.     while (1)
  196.     {
  197.         double startTime, endTime, val;
  198.  
  199.         startTime = tick_sec();
  200.         if (dorecv(sockfd, buffer, DATA_BUFFER_SIZE))
  201.         {
  202.             printf("Stop server loop\n");
  203.             return -1;
  204.         }
  205.  
  206.         endTime = tick_sec();
  207.         val = ((double)DATA_BUFFER_SIZE / 1048576.0) / (endTime - startTime);
  208.  
  209.         if      (val < minVal) minVal = val;
  210.         else if (val > maxVal) maxVal = val;
  211.  
  212.         printf("%.2lf\n", val);
  213.         //printf("%.2lf\t%.2lf\t%.2lf\t%.2lf\n", val, currentLoad(), minVal, maxVal);
  214.         //if (val < 100) printf ("%.2lf (%i)", val, ++shitCnt);
  215.  
  216.         if (dosend(sockfd, buffer, REPLY_BUFFER_SIZE))
  217.         {
  218.             printf("Stop server loop\n");
  219.             return -1;
  220.         }
  221.     }
  222.      
  223.     printf("\n");
  224.  
  225.     return 0;
  226. }
  227.  
  228. int runClient(const char* serverIp)
  229. {
  230.     int sockfd = 0, n = 0;
  231.     char* buffer = (char*)malloc(DATA_BUFFER_SIZE);
  232.     struct sockaddr_in serv_addr;
  233.     unsigned int iter = 0;
  234.     int sz = SOCKET_BUFFER_SIZE;
  235.     int optLen = sizeof(int);
  236.  
  237.     setvbuf(stdout, NULL, _IONBF, 0);
  238.     setvbuf(stderr, NULL, _IONBF, 0);
  239.  
  240.     if(serverIp == NULL)
  241.     {
  242.         printf("No server ip given!\n");
  243.         return 1;
  244.     }
  245.  
  246.     if (buffer == NULL)
  247.     {
  248.         printf("Could not allocate memory\n");
  249.         return -1;
  250.     }
  251.  
  252.     memset(buffer, '0', sizeof(buffer));
  253.  
  254.     if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  255.     {
  256.         printf("Could not create socket\n");
  257.         return -1;
  258.     }
  259.  
  260.     memset(&serv_addr, '0', sizeof(serv_addr));
  261.  
  262.     serv_addr.sin_family = AF_INET;
  263.     serv_addr.sin_port = htons(SERVER_PORT);
  264.  
  265.     if(INETPTON(AF_INET, serverIp, &serv_addr.sin_addr)<=0)
  266.     {
  267.         printf("inet_pton error occured\n");
  268.         return 1;
  269.     }
  270.  
  271.     if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
  272.     {
  273.        printf("Connect failed\n");
  274.        return -1;
  275.     }
  276.  
  277.     if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char*)&sz, optLen) == -1)
  278.     {
  279.         printf("Could not set send buffer size\n");
  280.         return -1;
  281.     }
  282.  
  283.     printf("\nStart send\n");
  284.  
  285.     while ( 1)
  286.     {
  287.         if (dosend(sockfd, buffer, DATA_BUFFER_SIZE))
  288.         {
  289.             printf("Stop client loop\n");
  290.             return -1;
  291.         }
  292.  
  293.         if ((iter % 50) == 0)
  294.         {
  295.             printf("\riter %i", iter + 1);
  296.         }
  297.  
  298.         iter++;
  299.  
  300.         if (dorecv(sockfd, buffer, REPLY_BUFFER_SIZE))
  301.         {
  302.             printf("Stop client loop\n");
  303.             return -1;
  304.         }
  305.     }
  306.  
  307.     printf("\n");
  308.  
  309.     return 0;
  310. }
  311.  
  312. double currentLoad()
  313. {
  314.     double usage;
  315.  
  316. #if defined (_MSC_VER)
  317.     usage = 0;
  318. #else
  319.     double loadavg;
  320.     double sum = 0, idle = 0;
  321.     double vals[4];
  322.     static double lastSum = 0, lastIdle = 0;
  323.     static int isInit = 0;
  324.     FILE* file = NULL;
  325.  
  326.     file = fopen("/proc/stat", "r");
  327.     if (file == NULL)
  328.     {
  329.         return 0;
  330.     }
  331.    
  332.     fscanf(file, "%*s %lf %lf %lf %lf", &vals[0], &vals[1], &vals[2], &vals[3]);
  333.  
  334.     idle = vals[3];
  335.     sum = vals[0] + vals[1] + vals[2] + vals[3];
  336.  
  337.     if (isInit)
  338.     {
  339.         usage = (1.f - (idle-lastIdle) * 1.f / (sum-lastSum)) * 100.f;
  340.     }
  341.     else
  342.     {
  343.         usage = 0;
  344.         isInit = 1;
  345.     }
  346.  
  347.     lastSum = sum;
  348.     lastIdle = idle;      
  349.    
  350.     fclose(file);
  351. #endif
  352.     return usage;
  353. }
RAW Paste Data