Advertisement
Guest User

dwojkauczącasie

a guest
Mar 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include"stdafx.h"
  2. #include <winsock.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <sstream>
  6. #include <time.h>
  7. #include <string>
  8. #include <conio.h>
  9.  
  10. #pragma comment (lib,"ws2_32.lib")
  11. using namespace std;
  12.  
  13. enum { GETT, GETP, RANDOM, EXIT };
  14.  
  15. int main()
  16. {
  17. WSADATA wsaData;
  18.  
  19. int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
  20. if (result != NO_ERROR)
  21. {
  22. cout << "\nBiblioteka WinSock zainicjalizowana niepoprawnie. Kod bledu: " + WSAGetLastError() << endl;
  23. return 0;
  24. }
  25. cout << "\nBiblioteka WinSock zainicjalizowana poprawnie." << endl;
  26.  
  27. string sendBuff;
  28. char receiveBuff[1024];
  29. int SenderAddrSize;
  30. int ReceiverAddrSize;
  31. int iResult;
  32. SOCKET _socket = INVALID_SOCKET;
  33. _socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  34.  
  35. if (_socket == INVALID_SOCKET)
  36. {
  37. cout << "\nGniazdo utworzone niepoprawnie. Kod bledu: " + WSAGetLastError() << endl;
  38. WSACleanup();
  39. return 0;
  40. }
  41. cout << "\nGniazdo utworzone poprawnie." << endl;
  42.  
  43. SOCKADDR_IN serwer;
  44. serwer.sin_family = AF_INET;
  45. serwer.sin_addr.s_addr = INADDR_ANY;
  46. serwer.sin_port = htons(7404);
  47. SenderAddrSize = sizeof(serwer);
  48.  
  49. if ((bind(_socket, (SOCKADDR*)&serwer, sizeof(serwer))) == SOCKET_ERROR)
  50. {
  51. printf("Blad bindowania gniazda : %d", WSAGetLastError());
  52. shutdown(_socket, 2); closesocket(_socket); WSACleanup(); _getch(); return 0;
  53. }
  54.  
  55. SOCKADDR_IN client;
  56. ReceiverAddrSize = sizeof(client);
  57.  
  58. while (true)
  59. {
  60. sendBuff.clear();
  61. // dla zadanego polecenia (jeśli prawidłowe) odbieramy dane liczbowe
  62. iResult = recvfrom(_socket, receiveBuff, 1024, 0, (SOCKADDR *)& client, &ReceiverAddrSize);
  63.  
  64. if (iResult == SOCKET_ERROR)
  65. {
  66. printf("\nNieudana proba odebrania datagramu. Kod bledu: %d\n", +WSAGetLastError());
  67. continue;
  68. }
  69.  
  70. if (strncmp(receiveBuff, "GETT", 4) == 0)
  71. {
  72. sendBuff.append("T");
  73. }
  74. else if (strncmp(receiveBuff, "GETP", 4) == 0)
  75. {
  76. sendBuff.append("P");
  77. }
  78. else if (strncmp(receiveBuff, "RANDOM", 6) == 0)
  79. {
  80. rand() % 2 ? sendBuff.append("T") : sendBuff.append("P");
  81. }
  82. else
  83. {
  84. iResult = sendto(_socket, "UNKNOWN COMMAND", 15, 0, (SOCKADDR *)& client, ReceiverAddrSize);
  85. if (iResult == SOCKET_ERROR)
  86. {
  87. cout << "\nNieudana proba wyslania datagramu. Kod bledu: " + WSAGetLastError() << endl;
  88. }
  89. continue;
  90. }
  91.  
  92. srand(time(NULL));
  93. int size = rand() % 20 + 1;
  94. while (size--)
  95. {
  96. char temp[64];
  97. sprintf_s(temp, " %d.%d", rand() % 51, rand() % 1001);
  98. sendBuff.append(temp);
  99. }
  100. printf("\nWyslano:\n%s", sendBuff.c_str());
  101. // wysłanie polecenia do serwera. Prawidłowe polecenia: GETT - pobranie temperatur, GETP - pobranie ciśnień, RANDOM - losowa kategoria, EXIT - koniec
  102. iResult = sendto(_socket, sendBuff.c_str(), 1024, 0, (SOCKADDR *)& client, ReceiverAddrSize);
  103. if (iResult == SOCKET_ERROR)
  104. {
  105. cout << "\nNieudana proba wyslania datagramu. Kod bledu: " + WSAGetLastError() << endl;
  106. continue;
  107. }
  108. }
  109. iResult = closesocket(_socket);
  110. if (iResult == SOCKET_ERROR)
  111. {
  112. cout << "Nieudana proba zamknięcia gniazda. Kod bledu: %d" + WSAGetLastError() << endl;
  113. }
  114.  
  115. WSACleanup();
  116. cout << "\nUsunieto z pamieci biblioteke WinSock." << endl;
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement