Hubert_M

Untitled

May 28th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. /*
  2. TCP Echo server example in winsock
  3. Live Server on port 8888
  4. */
  5.  
  6. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  7. #include <iostream>
  8. #include<stdio.h>
  9. #include <cstdlib>
  10. #include <cstdio>
  11. #include <ctime>
  12. #include <cstdio>
  13. #include <chrono>
  14. #include <thread>
  15. #include <functional>
  16. #include <winsock2.h>
  17. #include <string>
  18. #include <sstream>
  19. #include <vector>
  20. #include <iterator>
  21.  
  22. #pragma comment(lib, "ws2_32.lib") //Winsock Library
  23. using std::string;
  24.  
  25. /* --- Implementacja timera --- */
  26. void timer_start(std::function<void(void)> func, unsigned int interval)
  27. {
  28. std::thread([func, interval]() {
  29. while (true)
  30. {
  31. func();
  32. std::this_thread::sleep_for(std::chrono::milliseconds(interval));
  33. }
  34. }).detach();
  35. }
  36. /* --- Implementacja timera --- */
  37.  
  38.  
  39. /* --- Odbior danych --- */
  40. template<typename Out>
  41. void split(const std::string &s, char delim, Out result) {
  42. std::stringstream ss(s);
  43. std::string item;
  44. while (std::getline(ss, item, delim)) {
  45. *(result++) = item;
  46. }
  47. }
  48.  
  49.  
  50. std::vector<std::string> split(const std::string &s, char delim) {
  51. std::vector<std::string> elems;
  52. split(s, delim, std::back_inserter(elems));
  53. return elems;
  54. }
  55. /* --- Odbior danych --- */
  56.  
  57. /* --- Definicja Zmiennych do programu --- */
  58.  
  59. int x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0, x7 = 0, x8 = 0, x9 = 0, x10 = 0, x11 = 0, x12 = 0;
  60. int T1 = 0, T2 = 0, T3 = 0, T4 = 0;
  61. char Z1 = 0, Z2 = 0, Z3 = 0, Z4 = 0, Z5 = 0, Z6 = 0;
  62. char G1 = 0, G2 = 0, G3 = 0, G4 = 0;
  63. char M1 = 0, M2 = 0, M3 = 0, M4 = 0;
  64. int stan1 = 1, stan2 = 1, stan3 = 1, stan4 = 1;
  65. char tim1 = 0, tim2 = 0, tim3 = 0, tim4 = 0;
  66.  
  67. //Bedzie 14 ale sa same zawory
  68. byte x[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  69.  
  70. //Będzie 16 ale bez temperatury
  71. int y[12] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
  72.  
  73. string stringX;
  74. string stringY;
  75.  
  76. string arrayY[12];
  77.  
  78. std::vector<int> vectorY;
  79.  
  80. /* --- Definicja Zmiennych do programu --- */
  81.  
  82. void implementacja()
  83. {
  84.  
  85. //Przypisanie otrzymanych wartosci do poziomow wody
  86. x1 = y[0];
  87. x2 = y[1];
  88. x3 = y[2];
  89. x4 = y[3];
  90. x5 = y[4];
  91. x6 = y[5];
  92. x7 = y[6];
  93. x8 = y[7];
  94. x9 = y[8];
  95. x10 = y[9];
  96. x11 = y[10];
  97. x12 = y[11];
  98.  
  99. /*
  100. //Przypisanie otrzymanych wartosci do poziomu termometrow
  101. T1 = y[12];
  102. T2 = y[13];
  103. T3 = y[14];
  104. T4 = y[15];
  105. */
  106.  
  107. //Zbiornik 1
  108. {
  109. Z1 = 1;
  110. }
  111.  
  112. //Zbiornik 2
  113. {
  114. switch (stan2)
  115. {
  116. case 1:
  117. Z2 = 1;
  118. Z4 = 0;
  119. if (x4) {
  120. stan2 = 2;
  121. }
  122. break;
  123.  
  124. case 2:
  125. Z2 = 0;
  126. Z4 = 1;
  127. if (!x4)
  128. stan2 = 1;
  129. break;
  130.  
  131.  
  132. }
  133.  
  134. std::cout << "stan : " << stan2 << std::endl;
  135. }
  136.  
  137. //Zbiornik 3
  138. {
  139.  
  140. }
  141.  
  142. //Zbiornik 4
  143. {
  144.  
  145. }
  146.  
  147.  
  148.  
  149. //Przekazanie danych do obiektu
  150. x[0] = Z1;
  151. x[1] = Z2;
  152. x[2] = Z3;
  153. x[3] = Z4;
  154. x[4] = Z5;
  155. x[5] = Z6;
  156. /*
  157. x[6] = G1;
  158. x[7] = G2;
  159. x[8] = G3;
  160. x[9] = G4;
  161. x[10] = M1;
  162. x[11] = M2;
  163. x[12] = M3;
  164. x[13] = M4;
  165. */
  166.  
  167. //Obsluga timerow
  168. if (tim1) --tim1;
  169. if (tim2) --tim2;
  170. if (tim3) --tim3;
  171. if (tim4) --tim4;
  172. }
  173.  
  174.  
  175.  
  176.  
  177. /* --- Implementacja timera --- */
  178. void timer_start2(std::function<void(void)> func, unsigned int interval)
  179. {
  180. std::thread([func, interval]() {
  181. while (true)
  182. {
  183. func();
  184. std::this_thread::sleep_for(std::chrono::milliseconds(interval));
  185. }
  186. }).detach();
  187. }
  188. /* --- Implementacja timera --- */
  189.  
  190.  
  191. void connects()
  192. {
  193. WSADATA wsa;
  194. SOCKET master, new_socket, client_socket[30], s;
  195. struct sockaddr_in server, address;
  196. int max_clients = 30, activity, addrlen, i, valread;
  197.  
  198. //size of our receive buffer, this is string length.
  199. int MAXRECV = 1024;
  200. //set of socket descriptors
  201. fd_set readfds;
  202. //1 extra for null character, string termination
  203. char *buffer;
  204. buffer = (char*)malloc((MAXRECV + 1) * sizeof(char));
  205.  
  206. for (i = 0; i < 30; i++)
  207. {
  208. client_socket[i] = 0;
  209. }
  210.  
  211. printf(" \nInitialising Winsock...");
  212. if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  213. {
  214. printf(" Failed.Error Code : %d", WSAGetLastError());
  215. exit(EXIT_FAILURE);
  216. }
  217.  
  218. printf(" Initialised.\n");
  219.  
  220. //Create a socket
  221. if ((master = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  222. {
  223. printf(" Could not create socket : %d", WSAGetLastError());
  224. exit(EXIT_FAILURE);
  225. }
  226.  
  227. printf(" Socket created.\n");
  228.  
  229. //Prepare the sockaddr_in structure
  230. server.sin_family = AF_INET;
  231. server.sin_addr.s_addr = INADDR_ANY;
  232. server.sin_port = htons(16666);
  233.  
  234. //Bind
  235. if (bind(master, (struct sockaddr *)& server, sizeof(server)) == SOCKET_ERROR)
  236. {
  237. printf(" Bind failed with error code : %d", WSAGetLastError());
  238. exit(EXIT_FAILURE);
  239. }
  240.  
  241. puts(" Bind done");
  242.  
  243. //Listen to incoming connections
  244. listen(master, 3);
  245.  
  246. //Accept and incoming connection
  247. puts(" Waiting for incoming connections...");
  248.  
  249. addrlen = sizeof(struct sockaddr_in);
  250.  
  251. while (TRUE)
  252. {
  253. //clear the socket fd set
  254. FD_ZERO(&readfds);
  255.  
  256. //add master socket to fd set
  257. FD_SET(master, &readfds);
  258.  
  259. //add child sockets to fd set
  260. for (i = 0; i < max_clients; i++)
  261. {
  262. s = client_socket[i];
  263. if (s > 0)
  264. {
  265. FD_SET(s, &readfds);
  266. }
  267. }
  268.  
  269. //wait for an activity on any of the sockets, timeout is NULL , so wait indefinitely
  270. activity = select(0, &readfds, NULL, NULL, NULL);
  271.  
  272. if (activity == SOCKET_ERROR)
  273. {
  274. printf(" select call failed with error code : %d", WSAGetLastError());
  275. exit(EXIT_FAILURE);
  276. }
  277.  
  278. //If something happened on the master socket , then its an incoming connection
  279. if (FD_ISSET(master, &readfds))
  280. {
  281. if ((new_socket = accept(master, (struct sockaddr *) &address, (int *)& addrlen)) < 0)
  282. {
  283. perror(" accept");
  284. exit(EXIT_FAILURE);
  285. }
  286.  
  287. //inform user of socket number - used in send and receive commands
  288. printf(" New connection, socket fd is %d, ip is : %s, port : %d \n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
  289.  
  290. //send new connection greeting message
  291. //stringX = std::to_string(x[0]) + std::to_string(x[1]) + std::to_string(x[2]) + std::to_string(x[3]) + std::to_string(x[4]) + std::to_string(x[5]) + std::to_string(x[6]) + std::to_string(x[7]) + std::to_string(x[8]) + std::to_string(x[9]) + std::to_string(x[10]) + std::to_string(x[11]) + std::to_string(x[12]) + std::to_string(x[13]) + std::to_string(x[14]);
  292. /*
  293. if (send(new_socket, stringX.c_str(), strlen(stringX.c_str()), 0) != strlen(stringX.c_str()))
  294. {
  295. perror(" send failed");
  296. }
  297.  
  298. */
  299.  
  300.  
  301. puts(" Welcome message sent successfully");
  302.  
  303.  
  304. //add new socket to array of sockets
  305. for (i = 0; i < max_clients; i++)
  306. {
  307. if (client_socket[i] == 0)
  308. {
  309. client_socket[i] = new_socket;
  310. printf(" Adding to list of sockets at index %d \n", i);
  311. break;
  312. }
  313. }
  314. }
  315.  
  316. //else its some IO operation on some other socket :)
  317. for (i = 0; i < max_clients; i++)
  318. {
  319. s = client_socket[i];
  320. //if client presend in read sockets
  321. if (FD_ISSET(s, &readfds))
  322. {
  323. //get details of the client
  324. getpeername(s, (struct sockaddr*)& address, (int*)& addrlen);
  325.  
  326. //Check if it was for closing , and also read the incoming message
  327. //recv does not place a null terminator at the end of the string (whilst printf %s assumes there is one).
  328. valread = recv(s, buffer, MAXRECV, 0);
  329.  
  330. if (valread == SOCKET_ERROR)
  331. {
  332. int error_code = WSAGetLastError();
  333. if (error_code == WSAECONNRESET)
  334. {
  335. //Somebody disconnected , get his details and print
  336. printf(" Host disconnected unexpectedly, ip %s, port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
  337.  
  338. //Close the socket and mark as 0 in list for reuse
  339. closesocket(s);
  340. client_socket[i] = 0;
  341. }
  342. else
  343. {
  344. printf(" recv failed with error code : %d", error_code);
  345. }
  346. }
  347. if (valread == 0)
  348. {
  349. closesocket(s);
  350. client_socket[i] = 0;
  351. }
  352.  
  353. //Echo back the message that came in
  354. else
  355. {
  356. vectorY.clear();
  357. //add null character, if you want to use with printf/puts or other string handling functions
  358. buffer[valread] = '\0';
  359.  
  360. stringX = std::to_string(x[0]) + ":" + std::to_string(x[1]) + ":" + std::to_string(x[2]) + ":" + std::to_string(x[3]) + ":" + std::to_string(x[4]) + ":" + std::to_string(x[5]) +
  361. ":" + std::to_string(x[6]) + ":" + std::to_string(x[7]) + ":" + std::to_string(x[8]) + ":" + std::to_string(x[9]) + ":" + std::to_string(x[10]) + ":" + std::to_string(x[11]) +
  362. ":" + std::to_string(x[12]) + ":" + std::to_string(x[13]) + ":" + std::to_string(x[14]);
  363.  
  364.  
  365. send(s, stringX.c_str(), strlen(stringX.c_str()), 0);
  366. std::cout << "Wyslano : " << stringX.c_str() << std::endl;
  367.  
  368. stringY = buffer;
  369.  
  370. std::cout << stringY << std::endl;
  371.  
  372. std::stringstream ss(stringY);
  373. int i;
  374.  
  375. while (ss >> i)
  376. {
  377. vectorY.push_back(i);
  378.  
  379. if (ss.peek() == ':')
  380. ss.ignore();
  381. }
  382.  
  383. for (i = 0; i < 12; i++)
  384. y[i] = vectorY.at(i);
  385.  
  386. }
  387.  
  388.  
  389. }
  390. }
  391. }
  392.  
  393. closesocket(s);
  394. WSACleanup();
  395.  
  396. }
  397.  
  398.  
  399.  
  400. int main(int argc, char *argv[])
  401. {
  402. // Uruchomienie symulacji co 100 ms
  403. timer_start(implementacja, 100);
  404. connects();
  405. //timer_start2(connects, 100);
  406. while(true){}
  407. }
Add Comment
Please, Sign In to add comment