Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- using namespace std;
- // Don't forget to include "ws2_32.lib" in the library list.
- #include <winsock2.h>
- #include <string.h>
- #define TIME_PORT 27015
- void main()
- {
- const int SIZE = 100;
- // Initialize Winsock (Windows Sockets).
- WSAData wsaData;
- if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData))
- {
- cout << "Time Client: Error at WSAStartup()\n";
- }
- // 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;
- }
- // For a client to communicate on a network, it must connect to a server.
- // Need to assemble the required data for connection in sockaddr structure.
- // Create a sockaddr_in object called server.
- 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);
- // Send and receive data.
- int bytesSent = 0;
- int bytesRecv = 0;
- char sendBuff[255] = "What's the time?";
- char recvBuff[255];
- int choise;
- bool stop = false;
- bool delayEstimationFlag = false;
- bool measureRTTFlag = false;
- while (stop == false) {
- cout << "\nCLIENT: Choose a message to send:\n\n";
- cout << "1 - GetTime\n";
- cout << "2 - GetTimeWithoutDate\n";
- cout << "3 - GetTimeSinceEpoch\n";
- cout << "4 - GetClientToServerDelayEstimation\n";
- cout << "5 - MeasureRTT\n";
- cout << "6 - GetTimeWithoutDateOrSeconds\n";
- cout << "7 - GetYear\n";
- cout << "8 - GetMonthAndDay\n";
- cout << "9 - GetSecondsSinceBeginingOfMonth\n";
- cout << "10 - GetDayOfYear\n";
- cout << "11 - GetDaylightSavings\n";
- cout << "12 - Exit\n\n";
- cout << "Your choise: ";
- cin >> choise;
- while (!(choise <= 12 && choise >= 1)) {
- cout << "\nInvalid input. Please enter a valid choise: ";
- cin >> choise;
- }
- if (choise == 1) {
- strcpy(sendBuff, "GetTime");
- }
- else if (choise == 2) {
- strcpy(sendBuff, "GetTimeWithoutDate");
- }
- else if (choise == 3) {
- strcpy(sendBuff, "GetTimeSinceEpoch");
- }
- else if (choise == 4) {
- strcpy(sendBuff, "GetClientToServerDelayEstimation");
- delayEstimationFlag = true;
- }
- else if (choise == 5) {
- strcpy(sendBuff, "MeasureRTT");
- measureRTTFlag = true;
- }
- else if (choise == 6) {
- strcpy(sendBuff, "GetTimeWithoutDateOrSeconds");
- }
- else if (choise == 7) {
- strcpy(sendBuff, "GetYear");
- }
- else if (choise == 8) {
- strcpy(sendBuff, "GetMonthAndDay");
- }
- else if (choise == 9) {
- strcpy(sendBuff, "GetSecondsSinceBeginingOfMonth");
- }
- else if (choise == 10) {
- strcpy(sendBuff, "GetDayOfYear");
- }
- else if (choise == 11) {
- strcpy(sendBuff, "GetDaylightSavings");
- }
- else if (choise == 12) {
- stop = true;
- WSACleanup();
- }
- if (delayEstimationFlag) {
- double sum = 0;
- int temp;
- for (int i = 0; i < SIZE; i++) {
- strcpy(sendBuff, "GetClientToServerDelayEstimation");
- 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";
- }
- for (int i = 0; i < SIZE; i++) {
- bytesRecv = recv(connSocket, recvBuff, 255, 0);
- recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
- if (i > 0) {
- sum += atoi(recvBuff) - temp;
- }
- temp = atoi(recvBuff);
- if (SOCKET_ERROR == bytesRecv)
- {
- cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
- closesocket(connSocket);
- WSACleanup();
- return;
- }
- cout << "Time Client: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
- }
- cout << "\n\nDelay Estimation between client and server is: " << sum / 100 << " ms\n";
- delayEstimationFlag = false;
- }
- else if (measureRTTFlag) {
- double sum = 0;
- DWORD temp;
- for (int i = 0; i < SIZE; i++) {
- strcpy(sendBuff, "MeasureRTT");
- temp = GetTickCount();
- 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";
- // Gets the server's answer using simple recieve (no need to hold the server's address).
- bytesRecv = recv(connSocket, recvBuff, 255, 0);
- recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
- sum += GetTickCount() - temp;
- if (SOCKET_ERROR == bytesRecv)
- {
- cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
- closesocket(connSocket);
- WSACleanup();
- return;
- }
- cout << "Time Client: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
- }
- cout << "\n\nDelay Estimation between client and server is: " << sum / 100 << " ms\n";
- measureRTTFlag = false;
- }
- else {
- // Asks the server what's the currnet time.
- // The send function sends data on a connected socket.
- // The buffer to be sent and its size are needed.
- // The fourth argument is an idicator specifying the way in which the call is made (0 for default).
- // The two last arguments hold the details of the server to communicate with.
- // NOTE: the last argument should always be the actual size of the client's data-structure (i.e. sizeof(sockaddr)).
- 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";
- // Gets the server's answer using simple recieve (no need to hold the server's address).
- 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";
- }
- }
- // Closing connections and Winsock.
- cout << "Time Client: Closing Connection.\n";
- closesocket(connSocket);
- WSACleanup();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement