Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(lib, "Ws2_32.lib")
- #pragma warning(disable: 4996)
- using namespace std;
- #include <winsock2.h>
- #include <string.h>
- #include <iostream>
- #include <ctime>
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #define TIME_PORT 27015
- void CheckSentMsg(int bytes_sent, SOCKET& connSocket)
- {
- if (SOCKET_ERROR == bytes_sent)
- {
- cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
- closesocket(connSocket);
- WSACleanup();
- exit(1);
- }
- }
- void CheckReciveMsg(int bytes_recived, SOCKET& connSocket)
- {
- if (SOCKET_ERROR == bytes_recived)
- {
- cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
- closesocket(connSocket);
- WSACleanup();
- exit(1);
- }
- }
- bool validateUserInput(char* user_input)
- {
- if (strcmp(user_input, "1") == 0 || strcmp(user_input, "2") == 0 || strcmp(user_input, "3") == 0 ||
- strcmp(user_input, "4") == 0 || strcmp(user_input, "5") == 0 || strcmp(user_input, "6") == 0 ||
- strcmp(user_input, "7") == 0 || strcmp(user_input, "8") == 0 || strcmp(user_input, "9") == 0 ||
- strcmp(user_input, "10") == 0 || strcmp(user_input, "11") == 0 || strcmp(user_input, "12") == 0)
- return true;
- else
- return false;
- }
- void ReciveMsgFromServer(SOCKET& connSocket)
- {
- int bytesRecv;
- char recvBuff[255];
- bytesRecv = recv(connSocket, recvBuff, 255, 0);
- CheckReciveMsg(bytesRecv, connSocket);
- recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
- cout << "Client: Recieved: " << bytesRecv << " bytes of \n\\" << recvBuff << "\\ message.\n\n";
- }
- void GetClientToServerDelayEstimation(char* user_input, SOCKET& connSocket, sockaddr_in& server)
- {
- int bytesSent;
- int tickCount[100];
- int bytesRecv;
- char recvBuff[255];
- long double sum = 0;
- for (int i = 0; i < 100; i++)
- {
- bytesSent = sendto(connSocket, user_input, (int)strlen(user_input), 0, (const sockaddr *)&server, sizeof(server));
- CheckSentMsg(bytesSent, connSocket);
- }
- for (int i = 0; i < 100; i++)
- {
- bytesRecv = recv(connSocket, recvBuff, 255, 0);
- CheckReciveMsg(bytesRecv, connSocket);
- tickCount[i] = atoi(recvBuff);
- }
- for (int i = 0; i < 99; i++)
- {
- sum += tickCount[i + 1] - tickCount[i];
- }
- sum = sum / 99;
- cout << "The delay estimation in milliseconds is: " << sum << endl << "in seconds: " << (sum / 1000) << endl;
- }
- void MeasureRTT(char* user_input, SOCKET& connSocket, sockaddr_in& server)
- {
- int bytesSent;
- int bytesRecv;
- char recvBuff[255];
- double RTTtime[100];
- clock_t begin_time;
- double sum = 0;
- for (int i = 0; i < 100; i++)
- {
- bytesSent = sendto(connSocket, user_input, (int)strlen(user_input), 0, (const sockaddr *)&server, sizeof(server));
- begin_time = clock();
- CheckSentMsg(bytesSent, connSocket);
- bytesRecv = recv(connSocket, recvBuff, 255, 0);
- RTTtime[i] = clock() - begin_time;
- CheckReciveMsg(bytesRecv, connSocket);
- }
- for (int i = 0; i < 100; i++)
- {
- sum += RTTtime[i];
- }
- sum /= 100;
- cout << "The RTT Time in milliseconds is: " << sum << endl << "in seconds: " << (sum / 1000) << endl;
- }
- void ActAccordingToInput(char* user_input, SOCKET& connSocket, sockaddr_in& server)
- {
- if (strcmp(user_input, "12") != 0)
- {
- if (strcmp(user_input, "4") == 0)
- {
- GetClientToServerDelayEstimation(user_input, connSocket, server);
- }
- if (strcmp(user_input, "5") == 0)
- {
- MeasureRTT(user_input, connSocket, server);
- }
- else
- {
- int bytesSent;
- bytesSent = sendto(connSocket, user_input, (int)strlen(user_input), 0, (const sockaddr *)&server, sizeof(server));
- CheckSentMsg(bytesSent, connSocket);
- cout << "Time Client: Sent: " << bytesSent << "/" << strlen(user_input) << " bytes of \"" << user_input << "\" message.\n";
- ReciveMsgFromServer(connSocket);
- }
- }
- else
- return;
- }
- int main()
- {
- // Initialize Winsock (Windows Sockets).
- WSAData wsaData;
- if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData))
- {
- cout << "Time Client: Error at WSAStartup()\n";
- return 0;
- }
- // Client side:
- // Create a socket and connect to an internet address.
- SOCKET connSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (INVALID_SOCKET == connSocket)
- {
- cout << "Time Client: Error at socket(): " << WSAGetLastError() << endl;
- WSACleanup();
- return 0;
- }
- sockaddr_in server;
- server.sin_family = AF_INET;
- server.sin_addr.s_addr = inet_addr("127.0.0.1");
- server.sin_port = htons(TIME_PORT);
- char UserInput[255] = "";
- bool validatedUserInput = true;
- do
- {
- cout << "What would you like to ask the server?\n";
- cout << "Enter 1 to Ask the time\n"
- << "Enter 2 to Ask the time without the date\n"
- << "Enter 3 to Ask the time since Epoch\n"
- << "Enter 4 to Ask the client to server delay estimation\n"
- << "Enter 5 to Ask the Measure RTT\n"
- << "Enter 6 to Ask the time without date or seconds\n"
- << "Enter 7 to Ask the year\n"
- << "Enter 8 to Ask the month & day\n"
- << "Enter 9 to Ask the seconds since begining of the month\n"
- << "Enter 10 to Ask the days since the beginning of the year\n"
- << "Enter 11 to Ask if it is day light saving time\n"
- << "Enter 12 to exit\n";
- cin >> UserInput;
- validatedUserInput = validateUserInput(UserInput);
- if (validatedUserInput)
- ActAccordingToInput(UserInput, connSocket , server);
- else
- cout << "The input that was given is not recognized, please try again\n";
- } while (strcmp(UserInput, "12") != 0);
- // Closing connections and Winsock.
- cout << "Time Client: Closing Connection.\n";
- closesocket(connSocket);
- system("pause");
- return 0;
- }
- /*
- #pragma warning(disable: 4996)
- while (UserChoise != 4)
- {
- cout << "What would you like to ask the server?\n";
- cout << "1) Ask the time?\n";
- cout << "2) Ask the server how are you?\n";
- cout << "3) Ask the server the wether?\n";
- cout << "4) exit\n";
- cin >> UserChoise;
- if (UserChoise == 4) { return; }
- if (UserChoise == 1)
- {
- strcpy(sendBuff, "1");
- bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
- if (SOCKET_ERROR == bytesSent)
- {
- cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
- closesocket(connSocket);
- WSACleanup();
- return;
- }
- cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
- }
- if (UserChoise == 2)
- {
- strcpy(sendBuff, "2");
- bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
- if (SOCKET_ERROR == bytesSent)
- {
- cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
- closesocket(connSocket);
- WSACleanup();
- return;
- }
- cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
- }
- if (UserChoise == 3)
- {
- strcpy(sendBuff, "3");
- bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
- if (SOCKET_ERROR == bytesSent)
- {
- cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
- closesocket(connSocket);
- WSACleanup();
- return;
- }
- cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
- }
- bytesRecv = recv(connSocket, recvBuff, 255, 0);
- if (SOCKET_ERROR == bytesRecv)
- {
- cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
- closesocket(connSocket);
- WSACleanup();
- return;
- }
- recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
- cout << "Time Client: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement