Hyluss

udp broadcast

Mar 21st, 2017
554
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. // UDP komunikacja prsota.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 <cstdlib>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. class wyjatek
  16. {
  17.     DWORD dwError;
  18. public:
  19.     wyjatek(int iResult)
  20.     {
  21.         printf ("Winsock - Blad nr %d",iResult);       
  22.     }
  23.  
  24.     wyjatek (hostent *rH)
  25.     {
  26.             dwError = WSAGetLastError();
  27.             if (dwError == WSAHOST_NOT_FOUND)
  28.             {
  29.                 printf ("host not found");
  30.             }
  31.             else if (dwError = WSANO_DATA)
  32.             {
  33.                 printf ("no data record");
  34.             }
  35.             else
  36.             {
  37.                 printf ("failed %d",dwError);
  38.             }
  39.     }
  40. };
  41.  
  42. class TInicjalizacja
  43. {
  44.     int iResult;
  45.     WSAData wsaData;
  46.     int dwError;
  47. public:
  48.     TInicjalizacja()
  49.     {
  50.         iResult = WSAStartup (MAKEWORD (2,2), &wsaData);
  51.         if (iResult != 0)
  52.         {
  53.            
  54.             throw wyjatek(iResult);
  55.         }
  56.         else
  57.         {
  58.         //  printf ("Winosck jest oki\n");
  59.         }
  60.     }
  61.     ~TInicjalizacja()
  62.     {
  63.         WSACleanup();
  64.     }
  65.  
  66. };
  67.  
  68. class THost
  69. {
  70.     int iResult;
  71.     DWORD dwError;
  72.     hostent *rH;
  73.     char hostname[100];
  74.     sockaddr_in serwer;
  75.     sockaddr_in klient;
  76. public:
  77.     THost (const char *nazwa)
  78.     {
  79.         strcpy (hostname,nazwa);
  80.         rH = gethostbyname(hostname);
  81.         if (rH == NULL) throw wyjatek (rH);    
  82.         else
  83.         {
  84.             serwer.sin_family = AF_INET;
  85.             serwer.sin_port = htons (5005);
  86.             serwer.sin_addr.s_addr = inet_addr (hostname);
  87.  
  88.             //
  89.             klient.sin_family = AF_INET;
  90.             klient.sin_port = htons (300);
  91.             klient.sin_addr.s_addr = (INADDR_BROADCAST);
  92.     //      klient.sin_addr.s_addr = inet_addr ("255.255.255.255");
  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.  
  113. struct TAdresyIP
  114. {
  115.     int suma;
  116.     char* IP;
  117. };
  118.  
  119.  
  120. int _tmain(int argc, _TCHAR* argv[])
  121. {
  122.    
  123.     TInicjalizacja TW;
  124.    
  125.    
  126. /////////////////////////////////////////////////////////////
  127.     SOCKET Client;
  128.     Client = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  129.     if (Client == INVALID_SOCKET)
  130.     {
  131.         cout << "Nie mozna stworzyc socket" << endl;
  132.         getch();
  133.         return 1;
  134.     }
  135.     const char on = 1;
  136.     if (setsockopt (Client, SOL_SOCKET, SO_BROADCAST,&on,sizeof (on) ) < 0)
  137.     {
  138.         cout << "blad" << endl;
  139.         getch();
  140.         closesocket(Client);
  141.         return 1;
  142.     }
  143. //////////////////////////////////////////////////////////////////
  144.     char host[128] = "127.0.0.1";
  145.     THost Th (host);
  146. //////////////////////////////////////////////////////////////////////////////////////
  147.     char ciag [1024];
  148.  
  149.     char buff2[1024];
  150.     int buffLen = 1024;
  151.     int AddrSize; //= sizeof (Addr);
  152.  
  153.     int wynik;
  154.     bool koncz = false;
  155.  
  156.     int Random = 0;
  157.     char wyslanymsg[1024];
  158.     bool sprawdzAdres = false;
  159.  
  160.     vector <TAdresyIP> adresyIP;
  161.     TAdresyIP tempadr;
  162.     tempadr.suma = 0;
  163.  
  164.  
  165.  
  166.    
  167.  
  168.    
  169.     while (true)
  170.     {
  171.         Random = 1+ (rand()%20);
  172.  
  173.         if (getch() == VK_ESCAPE) break;
  174.  
  175.         itoa (Random,wyslanymsg,10);
  176.  
  177.         int wynik = sendto (Client,ciag,strlen(wyslanymsg),0,(SOCKADDR*)&Th.Zwrocserwer(), sizeof(Th.Zwrocserwer() ) );
  178.         if (wynik == INVALID_SOCKET)
  179.         {
  180.             cout <<"Blad wysylania nr:" << WSAGetLastError();
  181.             shutdown (Client,2);
  182.             closesocket (Client);
  183.             getch();
  184.             return 1;
  185.         }
  186.  
  187.         AddrSize = sizeof (Th.Zwrocklient());
  188.  
  189.         wynik = recvfrom (Client,buff2,buffLen,0,(SOCKADDR*)&Th.Zwrocklient(),&AddrSize);      
  190.         buff2[wynik] = 0;
  191.  
  192.         if (wynik == INVALID_SOCKET)
  193.         {
  194.             cout <<"Blad odbeirania nr" << WSAGetLastError();
  195.             shutdown (Client,2);
  196.             closesocket(Client);
  197.             getch();
  198.             return 0;
  199.         }
  200.  
  201.        
  202.         for (int i=0; i< adresyIP.size(); i++)
  203.         {
  204.             if (adresyIP[i].IP == Th.ZwrocHostent().h_name) sprawdzAdres = true;
  205.         }
  206.  
  207.         if (!sprawdzAdres)
  208.         {
  209.             tempadr.IP = Th.ZwrocHostent().h_name;
  210.             tempadr.suma = atoi (buff2);
  211.             adresyIP.push_back(tempadr);
  212.         }
  213.         else
  214.         {
  215.             for (int i = 0; i < adresyIP.size(); i++)
  216.                 {
  217.                     if (adresyIP[i].IP == Th.ZwrocHostent().h_name)
  218.                     {
  219.                         adresyIP[i].suma += atoi(buff2);
  220.                     }
  221.                 }
  222.          }
  223.          for (int i = 0; i < adresyIP.size(); i++)
  224.             {
  225.                 cout << adresyIP[i].IP << ":        " << adresyIP[i].suma << endl;
  226.             }
  227.     }
  228.  
  229.     shutdown (Client,2);
  230.     closesocket (Client);
  231.     cout << "Koniec programu" << endl;
  232.     getch();
  233.     return 0;
  234.  
  235. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment