Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  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. #include <time.h>
  9. #include <Windows.h>
  10.  
  11. #define TIME_PORT 27015
  12.  
  13. void main()
  14. {
  15. // Initialize Winsock (Windows Sockets).
  16.  
  17. // Create a WSADATA object called wsaData.
  18. // The WSADATA structure contains information about the Windows
  19. // Sockets implementation.
  20. WSAData wsaData;
  21.  
  22. // Call WSAStartup and return its value as an integer and check for errors.
  23. // The WSAStartup function initiates the use of WS2_32.DLL by a process.
  24. // First parameter is the version number 2.2.
  25. // The WSACleanup function destructs the use of WS2_32.DLL by a process.
  26. if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData))
  27. {
  28. cout << "Time Server: Error at WSAStartup()\n";
  29. }
  30.  
  31. // Server side:
  32. // Create and bind a socket to an internet address.
  33.  
  34. // After initialization, a SOCKET object is ready to be instantiated.
  35.  
  36. // Create a SOCKET object called m_socket.
  37. // For this application: use the Internet address family (AF_INET),
  38. // datagram sockets (SOCK_DGRAM),
  39. // and the UDP/IP protocol (IPPROTO_UDP).
  40. SOCKET m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  41.  
  42. // Check for errors to ensure that the socket is a valid socket.
  43. // Error detection is a key part of successful networking code.
  44. // If the socket call fails, it returns INVALID_SOCKET.
  45. // The "if" statement in the previous code is used to catch any errors that
  46. // may have occurred while creating the socket. WSAGetLastError returns an
  47. // error number associated with the last error that occurred.
  48. if (INVALID_SOCKET == m_socket)
  49. {
  50. cout << "Time Server: Error at socket(): " << WSAGetLastError() << endl;
  51. WSACleanup();
  52. return;
  53. }
  54.  
  55. // For a server to communicate on a network, it must first bind the socket to
  56. // a network address.
  57.  
  58. // Need to assemble the required data for connection in sockaddr structure.
  59.  
  60. // Create a sockaddr_in object called serverService.
  61. sockaddr_in serverService;
  62. // Address family (must be AF_INET - Internet address family).
  63. serverService.sin_family = AF_INET;
  64. // IP address. The sin_addr is a union (s_addr is a unsigdned long (4 bytes) data type).
  65. // INADDR_ANY means to listen on all interfaces.
  66. // inet_addr (Internet address) is used to convert a string (char *) into unsigned int.
  67. // inet_ntoa (Internet address) is the reverse function (converts unsigned int to char *)
  68. // The IP address 127.0.0.1 is the host itself, it's actually a loop-back.
  69. serverService.sin_addr.s_addr = INADDR_ANY; //inet_addr("127.0.0.1");
  70. // IP Port. The htons (host to network - short) function converts an
  71. // unsigned short from host to TCP/IP network byte order (which is big-endian).
  72. serverService.sin_port = htons(TIME_PORT);
  73.  
  74. // Bind the socket for client's requests.
  75.  
  76. // The bind function establishes a connection to a specified socket.
  77. // The function uses the socket handler, the sockaddr structure (which
  78. // defines properties of the desired connection) and the length of the
  79. // sockaddr structure (in bytes).
  80. if (SOCKET_ERROR == bind(m_socket, (SOCKADDR *)&serverService, sizeof(serverService)))
  81. {
  82. cout << "Time Server: Error at bind(): " << WSAGetLastError() << endl;
  83. closesocket(m_socket);
  84. WSACleanup();
  85. return;
  86. }
  87.  
  88. // Waits for incoming requests from clients.
  89.  
  90. // Send and receive data.
  91. sockaddr client_addr;
  92. int client_addr_len = sizeof(client_addr);
  93. int bytesSent = 0;
  94. int bytesRecv = 0;
  95. char sendBuff[255];
  96. char recvBuff[255];
  97.  
  98. // Get client's requests and answer them.
  99. // The recvfrom function receives a datagram and stores the source address.
  100. // The buffer for data to be received and its available size are
  101. // returned by recvfrom. The fourth argument is an idicator
  102. // specifying the way in which the call is made (0 for default).
  103. // The two last arguments are optional and will hold the details of the client for further communication.
  104. // NOTE: the last argument should always be the actual size of the client's data-structure (i.e. sizeof(sockaddr)).
  105. cout << "Time Server: Wait for clients' requests.\n";
  106.  
  107. while (true)
  108. {
  109. bytesRecv = recvfrom(m_socket, recvBuff, 255, 0, &client_addr, &client_addr_len);
  110. if (SOCKET_ERROR == bytesRecv)
  111. {
  112. cout << "Time Server: Error at recvfrom(): " << WSAGetLastError() << endl;
  113. closesocket(m_socket);
  114. WSACleanup();
  115. return;
  116. }
  117.  
  118. recvBuff[bytesRecv] = '\0'; //add the null-terminating to make it a string
  119. cout << "Time Server: Recieved: " << bytesRecv << " bytes of \"" << recvBuff << "\" message.\n";
  120.  
  121. // Answer client's request by the current time.
  122.  
  123. if (strcmp(recvBuff, "GetTime") == 0) {
  124. // Get the current time.
  125. time_t timer;
  126. time(&timer);
  127. // Parse the current time to printable string.
  128. strcpy(sendBuff, ctime(&timer));
  129. sendBuff[strlen(sendBuff) - 1] = '\0'; //to remo
  130. }
  131. else if (strcmp(recvBuff, "GetTimeWithoutDate") == 0) {
  132. // Get the current time.
  133. time_t timer;
  134. time(&timer);
  135. // Parse the current time to printable string.
  136. struct tm * t = localtime(&timer);
  137. sprintf(sendBuff, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec);
  138. }
  139. else if (strcmp(recvBuff, "GetTimeSinceEpoch") == 0) {
  140. // Get the current time.
  141. time_t timer;
  142. time(&timer);
  143. // Parse the current time to printable string.
  144. struct tm * t = localtime(&timer);
  145. sprintf(sendBuff, "%d", timer);
  146. }
  147. else if (strcmp(recvBuff, "GetClientToServerDelayEstimation") == 0) {
  148. sprintf(sendBuff, "%d", GetTickCount());
  149. }
  150. else if (strcmp(recvBuff, "MeasureRTT") == 0) {
  151. sprintf(sendBuff, "%d", GetTickCount());
  152. }
  153. else if (strcmp(recvBuff, "GetTimeWithoutDateOrSeconds") == 0) {
  154. // Get the current time.
  155. time_t timer;
  156. time(&timer);
  157. // Parse the current time to printable string.
  158. struct tm * t = localtime(&timer);
  159. sprintf(sendBuff, "%02d:%02d", t->tm_hour, t->tm_min);
  160. }
  161. else if (strcmp(recvBuff, "GetYear") == 0) {
  162. // Get the current time.
  163. time_t timer;
  164. time(&timer);
  165. // Parse the current time to printable string.
  166. struct tm * t = localtime(&timer);
  167. sprintf(sendBuff, "%d", t->tm_year + 1900);
  168. }
  169. else if (strcmp(recvBuff, "GetMonthAndDay") == 0) {
  170. // Get the current time.
  171. time_t timer;
  172. time(&timer);
  173. // Parse the current time to printable string.
  174. struct tm * t = localtime(&timer);
  175. strftime(sendBuff, 254, "%B %d", t);
  176. }
  177. else if (strcmp(recvBuff, "GetSecondsSinceBeginingOfMonth") == 0) {
  178. // Get the current time.
  179. time_t timer;
  180. time(&timer);
  181. // Getting the beginning of the month.
  182. struct tm * t = localtime(&timer);
  183. t->tm_sec = 0;
  184. t->tm_min = 0;
  185. t->tm_hour = 0;
  186. t->tm_mday = 1;
  187. // Parse the current time to printable string.
  188. sprintf(sendBuff, "%.f", difftime(timer, mktime(t)));
  189. }
  190. else if (strcmp(recvBuff, "GetDayOfYear") == 0) {
  191. // Get the current time.
  192. time_t timer;
  193. time(&timer);
  194. // Parse the current time to printable string.t
  195. struct tm * t = localtime(&timer);
  196. sprintf(sendBuff, "%d", t->tm_yday);
  197. }
  198. else if (strcmp(recvBuff, "GetDaylightSavings") == 0) {
  199. // Get the current time.
  200. time_t timer;
  201. time(&timer);
  202. // Parse the current time to printable string.
  203. struct tm * t = localtime(&timer);
  204. sprintf(sendBuff, "%d", t->tm_isdst);
  205. }
  206.  
  207. // Sends the answer to the client, using the client address gathered
  208. // by recvfrom.
  209. bytesSent = sendto(m_socket, sendBuff, (int)strlen(sendBuff), 0, (const sockaddr *)&client_addr, client_addr_len);
  210. if (SOCKET_ERROR == bytesSent)
  211. {
  212. cout << "Time Server: Error at sendto(): " << WSAGetLastError() << endl;
  213. closesocket(m_socket);
  214. WSACleanup();
  215. return;
  216. }
  217.  
  218. cout << "Time Server: Sent: " << bytesSent << "\\" << strlen(sendBuff) << " bytes of \"" << sendBuff << "\" message.\n";
  219. }
  220.  
  221. // Closing connections and Winsock.
  222. cout << "Time Server: Closing Connection.\n";
  223. closesocket(m_socket);
  224. WSACleanup();
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement