Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. struct SocketState
  2. {
  3. SOCKET id; // Socket handle
  4. int recv; // Receiving?
  5. int send; // Sending?
  6. int sendSubType; // Sending sub-type
  7. char buffer[128];
  8. int len;
  9. }
  10.  
  11. #define _CRT_SECURE_NO_WARNINGS
  12. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  13. #include <iostream>
  14. using namespace std;
  15. #include <winsock2.h>
  16. #include <string.h>
  17. #include <time.h>
  18. # include <ctime>
  19. #include <ws2tcpip.h>
  20. #include <assert.h>
  21. #pragma comment(lib, "Ws2_32.lib")
  22.  
  23. struct SocketState
  24. {
  25. SOCKET id; // Socket handle
  26. int recv; // Receiving?
  27. int send; // Sending?
  28. int sendSubType; // Sending sub-type
  29. char buffer[128];
  30. int len;
  31. };
  32.  
  33. const int TIME_PORT = 27015;
  34. const int MAX_SOCKETS = 60;
  35. const int EMPTY = 0;
  36. const int LISTEN = 1;
  37. const int RECEIVE = 2;
  38. const int IDLE = 3;
  39. const int SEND = 4;
  40. const int SEND_TIME = 1;
  41. const int SEND_SECONDS = 2;
  42.  
  43. bool addSocket(SOCKET id, int what);
  44. void removeSocket(int index);
  45. void acceptConnection(int index);
  46. void receiveMessage(int index);
  47. void sendMessage(int index);
  48.  
  49. struct SocketState sockets[MAX_SOCKETS] = { 0 };
  50. int socketsCount = 0;
  51.  
  52.  
  53. void main()
  54. {
  55. WSAData wsaData;
  56. if (NO_ERROR != WSAStartup(MAKEWORD(2, 2), &wsaData))
  57. {
  58. cout << "Time Server: Error at WSAStartup()n";
  59. return;
  60. }
  61.  
  62. SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  63.  
  64.  
  65. if (INVALID_SOCKET == listenSocket)
  66. {
  67. cout << "Time Server: Error at socket(): " << WSAGetLastError() << endl;
  68. WSACleanup();
  69. return;
  70. }
  71.  
  72. sockaddr_in serverService;
  73.  
  74. serverService.sin_family = AF_INET;
  75. serverService.sin_addr.s_addr = INADDR_ANY;
  76. serverService.sin_port = htons(TIME_PORT);
  77.  
  78. if (SOCKET_ERROR == bind(listenSocket, (SOCKADDR*)& serverService, sizeof(serverService)))
  79. {
  80. cout << "Time Server: Error at bind(): " << WSAGetLastError() << endl;
  81. closesocket(listenSocket);
  82. WSACleanup();
  83. return;
  84. }
  85.  
  86. if (SOCKET_ERROR == listen(listenSocket, 5))
  87. {
  88. cout << "Time Server: Error at listen(): " << WSAGetLastError() << endl;
  89. closesocket(listenSocket);
  90. WSACleanup();
  91. return;
  92. }
  93. addSocket(listenSocket, LISTEN);
  94.  
  95.  
  96.  
  97. while (true)
  98. {
  99. fd_set waitRecv;
  100. FD_ZERO(&waitRecv);
  101. for (int i = 0; i < MAX_SOCKETS; i++)
  102. {
  103. if ((sockets[i].recv == LISTEN) || (sockets[i].recv == RECEIVE))
  104. FD_SET(sockets[i].id, &waitRecv);
  105. }
  106.  
  107.  
  108. fd_set waitSend;
  109. FD_ZERO(&waitSend);
  110. for (int i = 0; i < MAX_SOCKETS; i++)
  111. {
  112. if (sockets[i].send == SEND)
  113. FD_SET(sockets[i].id, &waitSend);
  114. }
  115.  
  116. struct timeval timeout;
  117. timeout.tv_sec = 15;
  118. timeout.tv_usec = 0;
  119. int nfd = select(0, &waitRecv, &waitSend, NULL, NULL);
  120. if (nfd == SOCKET_ERROR)
  121. {
  122. cout << "Time Server: Error at select(): " << WSAGetLastError() << endl;
  123. WSACleanup();
  124. return;
  125. }
  126. for (int i = 0; i < MAX_SOCKETS && nfd > 0; i++)
  127. {
  128. if (FD_ISSET(sockets[i].id, &waitRecv))
  129. {
  130. nfd--;
  131. switch (sockets[i].recv)
  132. {
  133. case LISTEN:
  134. acceptConnection(i);
  135. break;
  136.  
  137. case RECEIVE:
  138. receiveMessage(i);
  139. break;
  140. }
  141. }
  142. }
  143.  
  144. for (int i = 0; i < MAX_SOCKETS && nfd > 0; i++)
  145. {
  146. if (FD_ISSET(sockets[i].id, &waitSend))
  147. {
  148. nfd--;
  149. switch (sockets[i].send)
  150. {
  151. case SEND:
  152. sendMessage(i);
  153. break;
  154. }
  155. }
  156. }
  157.  
  158. }
  159.  
  160. // Closing connections and Winsock.
  161. cout << "Time Server: Closing Connection.n";
  162. closesocket(listenSocket);
  163. WSACleanup();
  164. }
  165.  
  166. bool addSocket(SOCKET id, int what)
  167. {
  168. for (int i = 0; i < MAX_SOCKETS; i++)
  169. {
  170. if (sockets[i].recv == EMPTY)
  171. {
  172. sockets[i].id = id;
  173. sockets[i].recv = what;
  174. sockets[i].send = IDLE;
  175. sockets[i].len = 0;
  176. socketsCount++;
  177. return (true);
  178. }
  179. }
  180. return (false);
  181. }
  182.  
  183. void removeSocket(int index)
  184. {
  185. sockets[index].recv = EMPTY;
  186. sockets[index].send = EMPTY;
  187. socketsCount--;
  188. }
  189.  
  190. void acceptConnection(int index)
  191. {
  192. SOCKET id = sockets[index].id;
  193. struct sockaddr_in from; // Address of sending partner
  194. int fromLen = sizeof(from);
  195.  
  196. SOCKET msgSocket = accept(id, (struct sockaddr*) & from, &fromLen);
  197. if (INVALID_SOCKET == msgSocket)
  198. {
  199. cout << "Time Server: Error at accept(): " << WSAGetLastError() << endl;
  200. return;
  201. }
  202. cout << "Time Server: Client " << inet_ntoa(from.sin_addr) << ":" << ntohs(from.sin_port) << " is connected." << endl;
  203.  
  204. //
  205. // Set the socket to be in non-blocking mode.
  206. //
  207. unsigned long flag = 1;
  208. if (ioctlsocket(msgSocket, FIONBIO, &flag) != 0)
  209. {
  210. cout << "Time Server: Error at ioctlsocket(): " << WSAGetLastError() << endl;
  211. }
  212.  
  213. if (addSocket(msgSocket, RECEIVE) == false)
  214. {
  215. cout << "ttToo many connections, dropped!n";
  216. closesocket(id);
  217. }
  218. return;
  219. }
  220.  
  221. void receiveMessage(int index)
  222. {
  223. SOCKET msgSocket = sockets[index].id;
  224.  
  225. int len = sockets[index].len;
  226. int bytesRecv = recv(msgSocket, &sockets[index].buffer[len], sizeof(sockets[index].buffer) - len, 0);
  227.  
  228. if (SOCKET_ERROR == bytesRecv)
  229. {
  230. cout << "Time Server: Error at recv(): " << WSAGetLastError() << endl;
  231. closesocket(msgSocket);
  232. removeSocket(index);
  233. return;
  234. }
  235. if (bytesRecv == 0)
  236. {
  237. closesocket(msgSocket);
  238. removeSocket(index);
  239. return;
  240. }
  241. else
  242. {
  243. sockets[index].buffer[len + bytesRecv] = ''; //add the null-terminating to make it a string
  244. cout << "Time Server: Recieved: " << bytesRecv << " bytes of "" << &sockets[index].buffer[len] << "" message.n";
  245.  
  246. sockets[index].len += bytesRecv;
  247.  
  248. if (sockets[index].len > 0)
  249. {
  250. if (strncmp(sockets[index].buffer, "TimeString", 10) == 0)
  251. {
  252. sockets[index].send = SEND;
  253. sockets[index].sendSubType = SEND_TIME;
  254. memcpy(sockets[index].buffer, &sockets[index].buffer[10], sockets[index].len - 10);
  255. sockets[index].len -= 10;
  256. return;
  257. }
  258. else if (strncmp(sockets[index].buffer, "SecondsSince1970", 16) == 0)
  259. {
  260. sockets[index].send = SEND;
  261. sockets[index].sendSubType = SEND_SECONDS;
  262. memcpy(sockets[index].buffer, &sockets[index].buffer[16], sockets[index].len - 16);
  263. sockets[index].len -= 16;
  264. return;
  265. }
  266. else if (strncmp(sockets[index].buffer, "Exit", 4) == 0)
  267. {
  268. closesocket(msgSocket);
  269. removeSocket(index);
  270. return;
  271. }
  272. }
  273. }
  274.  
  275. }
  276.  
  277. void sendMessage(int index)
  278. {
  279. int bytesSent = 0;
  280. char sendBuff[255];
  281.  
  282. SOCKET msgSocket = sockets[index].id;
  283. if (sockets[index].sendSubType == SEND_TIME)
  284. {
  285. // Answer client's request by the current time string.
  286.  
  287. // Get the current time.
  288. time_t timer;
  289. time(&timer);
  290. // Parse the current time to printable string.
  291. strcpy(sendBuff, ctime(&timer));
  292. sendBuff[strlen(sendBuff) - 1] = 0; //to remove the new-line from the created string
  293. }
  294. else if (sockets[index].sendSubType == SEND_SECONDS)
  295. {
  296. // Answer client's request by the current time in seconds.
  297.  
  298. // Get the current time.
  299. time_t timer;
  300. time(&timer);
  301. // Convert the number to string.
  302. _itoa((int)timer, sendBuff, 10);
  303. }
  304. bytesSent = send(msgSocket, sendBuff, (int)strlen(sendBuff), 0);
  305. if (SOCKET_ERROR == bytesSent)
  306. {
  307. cout << "Time Server: Error at send(): " << WSAGetLastError() << endl;
  308. return;
  309. }
  310.  
  311. cout << "Time Server: Sent: " << bytesSent << "\" << strlen(sendBuff) << " bytes of "" << sendBuff << "" message.n";
  312.  
  313. sockets[index].send = IDLE;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement