#include #include #include #include #include #include #if defined (_MSC_VER ) # include # include # pragma comment(lib, "ws2_32.lib") # define INETPTON InetPton #else # include # include # include # include # define INETPTON inet_pton #endif #define SERVER_PORT 36547 #define DATA_BUFFER_SIZE (1024 * 1024 * 4) /* size of data content buffer that will be sent repeatedly */ #define REPLY_BUFFER_SIZE (1024) #define SOCKET_BUFFER_SIZE (512 * 1024) int dorecv(int socket_desc, char* buffer, int bufSize); int dosend(int socket_desc, char* buffer, int bufSize); int runServer(); int runClient(const char* serverIp); double tick_sec(); double currentLoad(); int main(int argc , char *argv[]) { #if defined (_MSC_VER ) WSADATA wsa; if (WSAStartup(MAKEWORD(2, 0), &wsa)) { printf("WSAStartup error\n"); return -1; } #endif if (argc > 1) { printf("Run as client\n"); runClient(argv[1]); } else { printf("Run as server\n"); runServer(); } printf("Program ends!\n"); return 0; } int dorecv(int socket_desc, char* buffer, int bufSize) { int bytesRead = 0; while (bytesRead < bufSize) { int r = recv(socket_desc , buffer + bytesRead, bufSize - bytesRead, 0); if (r <= 0) { printf("Could not receive (%s)\n", strerror(errno)); return -1; } bytesRead += r; } return 0; } int dosend(int socket_desc, char* buffer, int bufSize) { int written = 0, w; while (written < bufSize) { if ((w = send(socket_desc , buffer + written, bufSize - written, 0)) < 0) { printf("Could not send (%s)\n", strerror(errno)); return -1; } written += w; } return 0; } double tick_sec() { #if defined (_MSC_VER ) static LARGE_INTEGER frequency; LARGE_INTEGER now; if (frequency.QuadPart == 0) { QueryPerformanceFrequency(&frequency); } QueryPerformanceCounter(&now); return now.QuadPart / (double)frequency.QuadPart; #else struct timespec tp; if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) { return 0; } return (double)tp.tv_sec + (double)tp.tv_nsec / 1000000000.0; #endif } int runServer() { int sockt, sockfd, c; struct sockaddr_in server, client; socklen_t optLen = sizeof(int); char* buffer = malloc(DATA_BUFFER_SIZE); int sz = SOCKET_BUFFER_SIZE; unsigned int i = 0; double minVal = DBL_MAX, maxVal = DBL_MIN; int shitCnt = 0; if (buffer == NULL) { printf("Could not allocate memory\n"); return -1; } //Create socket sockt = socket(AF_INET , SOCK_STREAM , 0); if (sockt == -1) { printf("Could not create socket\n"); return -1; } //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons( SERVER_PORT ); //Bind if( bind(sockt, (struct sockaddr *)&server , sizeof(server)) < 0) { //print the error message printf("bind failed. Error\n"); return 1; } //Listen listen(sockt , 1); //Accept and incoming connection printf("Waiting for incoming connections...\n"); //accept connection from an incoming client c = sizeof(struct sockaddr_in); sockfd = accept(sockt, (struct sockaddr *)&client, (socklen_t*)&c); if (sockfd < 0) { printf("accept failed\n"); return -1; } printf("Connection accepted, start receiving\n"); if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char*)&sz, optLen) == -1) { printf("Could not set receive buffer size\n"); return -1; } sz = 1; if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&sz, optLen)) { printf("Could not set TCP_NODELAY\n"); return -1; } //Receive a message from client while (1) { double startTime, endTime, val; startTime = tick_sec(); if (dorecv(sockfd, buffer, DATA_BUFFER_SIZE)) { printf("Stop server loop\n"); return -1; } endTime = tick_sec(); val = ((double)DATA_BUFFER_SIZE / 1048576.0) / (endTime - startTime); if (val < minVal) minVal = val; else if (val > maxVal) maxVal = val; printf("%.2lf\n", val); //printf("%.2lf\t%.2lf\t%.2lf\t%.2lf\n", val, currentLoad(), minVal, maxVal); //if (val < 100) printf ("%.2lf (%i)", val, ++shitCnt); if (dosend(sockfd, buffer, REPLY_BUFFER_SIZE)) { printf("Stop server loop\n"); return -1; } } printf("\n"); return 0; } int runClient(const char* serverIp) { int sockfd = 0, n = 0; char* buffer = (char*)malloc(DATA_BUFFER_SIZE); struct sockaddr_in serv_addr; unsigned int iter = 0; int sz = SOCKET_BUFFER_SIZE; int optLen = sizeof(int); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); if(serverIp == NULL) { printf("No server ip given!\n"); return 1; } if (buffer == NULL) { printf("Could not allocate memory\n"); return -1; } memset(buffer, '0', sizeof(buffer)); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("Could not create socket\n"); return -1; } memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(SERVER_PORT); if(INETPTON(AF_INET, serverIp, &serv_addr.sin_addr)<=0) { printf("inet_pton error occured\n"); return 1; } if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf("Connect failed\n"); return -1; } if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char*)&sz, optLen) == -1) { printf("Could not set send buffer size\n"); return -1; } printf("\nStart send\n"); while ( 1) { if (dosend(sockfd, buffer, DATA_BUFFER_SIZE)) { printf("Stop client loop\n"); return -1; } if ((iter % 50) == 0) { printf("\riter %i", iter + 1); } iter++; if (dorecv(sockfd, buffer, REPLY_BUFFER_SIZE)) { printf("Stop client loop\n"); return -1; } } printf("\n"); return 0; } double currentLoad() { double usage; #if defined (_MSC_VER) usage = 0; #else double loadavg; double sum = 0, idle = 0; double vals[4]; static double lastSum = 0, lastIdle = 0; static int isInit = 0; FILE* file = NULL; file = fopen("/proc/stat", "r"); if (file == NULL) { return 0; } fscanf(file, "%*s %lf %lf %lf %lf", &vals[0], &vals[1], &vals[2], &vals[3]); idle = vals[3]; sum = vals[0] + vals[1] + vals[2] + vals[3]; if (isInit) { usage = (1.f - (idle-lastIdle) * 1.f / (sum-lastSum)) * 100.f; } else { usage = 0; isInit = 1; } lastSum = sum; lastIdle = idle; fclose(file); #endif return usage; }