peterzig

[PWŚS] Klient UDP Nowy

Mar 21st, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. // UDP komunikacja prosta.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #include <cstdio>
  7. //#include <WinSock2.h>
  8. #include <iostream>
  9. #include <winsock2.h>
  10. #include <vector>
  11.  
  12. #pragma comment(lib, "ws2_32.lib")
  13.  
  14. using namespace std;
  15.  
  16.  
  17. class wyjatek
  18. {
  19.     DWORD dwError;
  20. public:
  21.     wyjatek(int iResult)
  22.     {
  23.         printf("Winsock - Blad nr %d", iResult);
  24.     }
  25.  
  26.     wyjatek(hostent *rH)
  27.     {
  28.         dwError = WSAGetLastError();
  29.         if (dwError == WSAHOST_NOT_FOUND)
  30.         {
  31.             printf("host not found");
  32.         }
  33.         else if (dwError = WSANO_DATA)
  34.         {
  35.             printf("no data record");
  36.         }
  37.         else
  38.         {
  39.             printf("failed %d", dwError);
  40.         }
  41.     }
  42. };
  43.  
  44. class TInicjalizacja
  45. {
  46.     int iResult;
  47.     WSAData wsaData;
  48.     int dwError;
  49. public:
  50.     TInicjalizacja()
  51.     {
  52.         iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  53.         if (iResult != 0)
  54.         {
  55.  
  56.             throw wyjatek(iResult);
  57.         }
  58.         else
  59.         {
  60.             //  printf ("Winosck jest oki\n");
  61.         }
  62.     }
  63.     ~TInicjalizacja()
  64.     {
  65.         WSACleanup();
  66.     }
  67.  
  68. };
  69.  
  70. class THost
  71. {
  72.     int iResult;
  73.     DWORD dwError;
  74.     hostent *rH;
  75.     char hostname[100];
  76.     sockaddr_in serwer;
  77.     sockaddr_in klient;
  78. public:
  79.     THost(const char *nazwa)
  80.     {
  81.         strcpy(hostname, nazwa);
  82.         rH = gethostbyname(hostname);
  83.         if (rH == NULL) throw wyjatek(rH);
  84.         else
  85.         {
  86.             serwer.sin_family = AF_INET;
  87.             serwer.sin_port = htons(27015);  //PORT do ustawienia
  88.             serwer.sin_addr.s_addr = inet_addr(hostname);
  89.  
  90.             //
  91.             klient.sin_family = AF_INET;
  92.             klient.sin_port = htons(300);
  93.         }
  94.     }
  95.  
  96.     hostent ZwrocHostent()
  97.     {
  98.         return *rH;
  99.     }
  100.  
  101.     sockaddr_in Zwrocserwer()
  102.     {
  103.         return serwer;
  104.     }
  105.  
  106.     sockaddr_in Zwrocklient()
  107.     {
  108.         return klient;
  109.     }
  110.  
  111. };
  112. struct ADRESYIP { int SUMA; char* IP; };
  113.  
  114. int _tmain(int argc, _TCHAR* argv[])
  115. {
  116.  
  117.     TInicjalizacja TW;
  118.  
  119.     SOCKET Client;
  120.     Client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  121.     if (Client == INVALID_SOCKET)
  122.     {
  123.         cout << "Nie mozna stworzyc socket" << endl;
  124.         getch();
  125.         return 1;
  126.     }
  127.  
  128.     char host[128] = "127.0.0.1"; //adres IP serwera z którym się łączysz
  129.     THost Th(host);
  130.     char ciag[1024];
  131.  
  132.     char buff2[1024];
  133.     int buffLen = 1024;
  134.     int AddrSize; //= sizeof (Addr);
  135.  
  136.     int wynik;
  137.  
  138.     int Random = 0;
  139.     char wyslanyChar[1024];
  140.     bool adresJuzJest = false;
  141.  
  142.     //srand(20);
  143.     vector<ADRESYIP> adresyIP;
  144.     ADRESYIP tempadr;
  145.     tempadr.SUMA = 0;
  146.  
  147.     while (true)
  148.     {
  149.         //cout << "Podaj wartosc do wyslania\n";
  150.         //gets_s(ciag);
  151.         Random = 1 + (rand() % (int)(20 - 1 + 1));
  152.  
  153.         if (_getch() == VK_ESCAPE)
  154.             break;
  155.         {
  156.             itoa(Random, wyslanyChar, 10);
  157.             if (sendto(Client, wyslanyChar, strlen(wyslanyChar), 0, (SOCKADDR*)&Th.Zwrocserwer(), sizeof(Th.Zwrocserwer())) == SOCKET_ERROR)
  158.             {
  159.                 std::cout << "Blad wysylania nr:" << WSAGetLastError();
  160.                 shutdown(Client, 2);
  161.                 closesocket(Client);
  162.                 return 1;
  163.             }
  164.  
  165.             AddrSize = sizeof(Th.Zwrocklient());
  166.  
  167.             wynik = recvfrom(Client, buff2, buffLen, 0, (SOCKADDR*)&Th.Zwrocklient(), &AddrSize);
  168.             buff2[wynik] = 0;
  169.  
  170.             if (wynik == INVALID_SOCKET)
  171.             {
  172.                 std::cout << "Blad odbeirania nr" << WSAGetLastError();
  173.                 shutdown(Client, 2);
  174.                 closesocket(Client);
  175.                 getch();
  176.                 return 0;
  177.             }
  178.             //adresyIP.push_back(Th.ZwrocHostent().h_name);
  179.  
  180.             for (int i = 0; i < adresyIP.size(); i++)
  181.             {
  182.                 if (adresyIP[i].IP == Th.ZwrocHostent().h_name)
  183.                     adresJuzJest = true;
  184.             }
  185.             if (!adresJuzJest) {
  186.                 tempadr.IP = Th.ZwrocHostent().h_name;
  187.                 tempadr.SUMA = atoi(buff2);
  188.                 adresyIP.push_back(tempadr);
  189.             }
  190.             else
  191.             {
  192.                 for (int i = 0; i < adresyIP.size(); i++)
  193.                 {
  194.                     if (adresyIP[i].IP == Th.ZwrocHostent().h_name)
  195.                     {
  196.                         adresyIP[i].SUMA += atoi(buff2);
  197.                     }
  198.                 }
  199.             }
  200.             for (int i = 0; i < adresyIP.size(); i++)
  201.             {
  202.                 cout << adresyIP[i].IP << ":        " << adresyIP[i].SUMA << endl;
  203.             }
  204.         }
  205.     }
  206.  
  207.     std::cout << "Koniec programu" << endl;
  208.     getch();
  209.     return 0;
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment