Advertisement
Guest User

Untitled

a guest
May 5th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. using namespace std;
  5. // Don't forget to include "ws2_32.lib" in the library list.
  6. #include <winsock2.h>
  7. #include <string.h>
  8. #define TIME_PORT 27015
  9.  
  10. void main()
  11. {
  12. const int SIZE = 100;
  13.  
  14. // Initialize Winsock (Windows Sockets).
  15.  
  16. WSAData wsaData;
  17. if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData))
  18. {
  19. cout << "Time Client: Error at WSAStartup()\n";
  20. }
  21.  
  22. // Client side:
  23. // Create a socket and connect to an internet address.
  24.  
  25. SOCKET connSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  26. if (INVALID_SOCKET == connSocket)
  27. {
  28. cout << "Time Client: Error at socket(): " << WSAGetLastError() << endl;
  29. WSACleanup();
  30. return;
  31. }
  32.  
  33. // For a client to communicate on a network, it must connect to a server.
  34.  
  35. // Need to assemble the required data for connection in sockaddr structure.
  36.  
  37. // Create a sockaddr_in object called server.
  38. sockaddr_in server;
  39. server.sin_family = AF_INET;
  40. server.sin_addr.s_addr = inet_addr("127.0.0.1");
  41. server.sin_port = htons(TIME_PORT);
  42.  
  43. // Send and receive data.
  44.  
  45. int bytesSent = 0;
  46. int bytesRecv = 0;
  47. char sendBuff[255] = "What's the time?";
  48. char recvBuff[255];
  49. int choise;
  50. bool stop = false;
  51. bool delayEstimationFlag = false;
  52. bool measureRTTFlag = false;
  53.  
  54. while (stop == false) {
  55.  
  56. cout << "\nCLIENT: Choose a message to send:\n\n";
  57. cout << "1 - GetTime\n";
  58. cout << "2 - GetTimeWithoutDate\n";
  59. cout << "3 - GetTimeSinceEpoch\n";
  60. cout << "4 - GetClientToServerDelayEstimation\n";
  61. cout << "5 - MeasureRTT\n";
  62. cout << "6 - GetTimeWithoutDateOrSeconds\n";
  63. cout << "7 - GetYear\n";
  64. cout << "8 - GetMonthAndDay\n";
  65. cout << "9 - GetSecondsSinceBeginingOfMonth\n";
  66. cout << "10 - GetDayOfYear\n";
  67. cout << "11 - GetDaylightSavings\n";
  68. cout << "12 - Exit\n\n";
  69. cout << "Your choise: ";
  70. cin >> choise;
  71.  
  72. while (!(choise <= 12 && choise >= 1)) {
  73. cout << "\nInvalid input. Please enter a valid choise: ";
  74. cin >> choise;
  75. }
  76.  
  77. if (choise == 1) {
  78. strcpy(sendBuff, "GetTime");
  79. }
  80. else if (choise == 2) {
  81. strcpy(sendBuff, "GetTimeWithoutDate");
  82. }
  83. else if (choise == 3) {
  84. strcpy(sendBuff, "GetTimeSinceEpoch");
  85. }
  86. else if (choise == 4) {
  87. strcpy(sendBuff, "GetClientToServerDelayEstimation");
  88. delayEstimationFlag = true;
  89. }
  90. else if (choise == 5) {
  91. strcpy(sendBuff, "MeasureRTT");
  92. measureRTTFlag = true;
  93. }
  94. else if (choise == 6) {
  95. strcpy(sendBuff, "GetTimeWithoutDateOrSeconds");
  96. }
  97. else if (choise == 7) {
  98. strcpy(sendBuff, "GetYear");
  99. }
  100. else if (choise == 8) {
  101. strcpy(sendBuff, "GetMonthAndDay");
  102. }
  103. else if (choise == 9) {
  104. strcpy(sendBuff, "GetSecondsSinceBeginingOfMonth");
  105. }
  106. else if (choise == 10) {
  107. strcpy(sendBuff, "GetDayOfYear");
  108. }
  109. else if (choise == 11) {
  110. strcpy(sendBuff, "GetDaylightSavings");
  111. }
  112. else if (choise == 12) {
  113. stop = true;
  114. WSACleanup();
  115. }
  116.  
  117. if (delayEstimationFlag) {
  118. double sum = 0;
  119. int temp;
  120. for (int i = 0; i < SIZE; i++) {
  121. strcpy(sendBuff, "GetClientToServerDelayEstimation");
  122. bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
  123. if (SOCKET_ERROR == bytesSent)
  124. {
  125. cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
  126. closesocket(connSocket);
  127. WSACleanup();
  128. return;
  129. }
  130. cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
  131. }
  132.  
  133. for (int i = 0; i < SIZE; i++) {
  134. bytesRecv = recv(connSocket, recvBuff, 255, 0);
  135. recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
  136. if (i > 0) {
  137. sum += atoi(recvBuff) - temp;
  138. }
  139. temp = atoi(recvBuff);
  140. if (SOCKET_ERROR == bytesRecv)
  141. {
  142. cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
  143. closesocket(connSocket);
  144. WSACleanup();
  145. return;
  146. }
  147.  
  148. cout << "Time Client: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
  149. }
  150.  
  151. cout << "\n\nDelay Estimation between client and server is: " << sum / 100 << " ms\n";
  152. delayEstimationFlag = false;
  153. }
  154. else if (measureRTTFlag) {
  155. double sum = 0;
  156. DWORD temp;
  157. for (int i = 0; i < SIZE; i++) {
  158. strcpy(sendBuff, "MeasureRTT");
  159. temp = GetTickCount();
  160. bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
  161. if (SOCKET_ERROR == bytesSent)
  162. {
  163. cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
  164. closesocket(connSocket);
  165. WSACleanup();
  166. return;
  167. }
  168. cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
  169.  
  170. // Gets the server's answer using simple recieve (no need to hold the server's address).
  171. bytesRecv = recv(connSocket, recvBuff, 255, 0);
  172. recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
  173. sum += GetTickCount() - temp;
  174. if (SOCKET_ERROR == bytesRecv)
  175. {
  176. cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
  177. closesocket(connSocket);
  178. WSACleanup();
  179. return;
  180. }
  181.  
  182. cout << "Time Client: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
  183. }
  184.  
  185. cout << "\n\nDelay Estimation between client and server is: " << sum / 100 << " ms\n";
  186. measureRTTFlag = false;
  187. }
  188. else {
  189. // Asks the server what's the currnet time.
  190. // The send function sends data on a connected socket.
  191. // The buffer to be sent and its size are needed.
  192. // The fourth argument is an idicator specifying the way in which the call is made (0 for default).
  193. // The two last arguments hold the details of the server to communicate with.
  194. // NOTE: the last argument should always be the actual size of the client's data-structure (i.e. sizeof(sockaddr)).
  195. bytesSent = sendto(connSocket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&server, sizeof(server));
  196. if (SOCKET_ERROR == bytesSent)
  197. {
  198. cout << "Time Client: Error at sendto(): " << WSAGetLastError() << endl;
  199. closesocket(connSocket);
  200. WSACleanup();
  201. return;
  202. }
  203. cout << "Time Client: Sent: " << bytesSent << "/" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
  204.  
  205. // Gets the server's answer using simple recieve (no need to hold the server's address).
  206. bytesRecv = recv(connSocket, recvBuff, 255, 0);
  207. if (SOCKET_ERROR == bytesRecv)
  208. {
  209. cout << "Time Client: Error at recv(): " << WSAGetLastError() << endl;
  210. closesocket(connSocket);
  211. WSACleanup();
  212. return;
  213. }
  214.  
  215. recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
  216. cout << "Time Client: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
  217. }
  218. }
  219.  
  220. // Closing connections and Winsock.
  221. cout << "Time Client: Closing Connection.\n";
  222. closesocket(connSocket);
  223. WSACleanup();
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement