Hyluss

udp broadcast dzialajace witek

Apr 4th, 2017
585
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 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 <string.h>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. class wyjatek
  15. {
  16.     DWORD dwError;
  17. public:
  18.     wyjatek(int iResult)
  19.     {
  20.         printf ("Winsock - Blad nr %d",iResult);       
  21.     }
  22.  
  23.     wyjatek (hostent *rH)
  24.     {
  25.             dwError = WSAGetLastError();
  26.             if (dwError == WSAHOST_NOT_FOUND)
  27.             {
  28.                 printf ("host not found");
  29.             }
  30.             else if (dwError = WSANO_DATA)
  31.             {
  32.                 printf ("no data record");
  33.             }
  34.             else
  35.             {
  36.                 printf ("failed %d",dwError);
  37.             }
  38.     }
  39. };
  40.  
  41. class TInicjalizacja
  42. {
  43.     int iResult;
  44.     WSAData wsaData;
  45.     int dwError;
  46. public:
  47.     TInicjalizacja()
  48.     {
  49.         iResult = WSAStartup (MAKEWORD (2,2), &wsaData);
  50.         if (iResult != 0)
  51.         {
  52.            
  53.             throw wyjatek(iResult);
  54.         }
  55.         else
  56.         {
  57.         //  printf ("Winosck jest oki\n");
  58.         }
  59.     }
  60.     ~TInicjalizacja()
  61.     {
  62.         WSACleanup();
  63.     }
  64.  
  65. };
  66. /*
  67. class THost
  68. {
  69.     int iResult;
  70.     DWORD dwError;
  71.     hostent *rH;
  72.     char hostname[100];
  73.     sockaddr_in serwer;
  74.     sockaddr_in klient;
  75. public:
  76.     THost (const char *nazwa)
  77.     {
  78.         strcpy (hostname,nazwa);
  79.         rH = gethostbyname(hostname);
  80.         if (rH == NULL) throw wyjatek (rH);    
  81.         else
  82.         {
  83.             serwer.sin_family = AF_INET;
  84.             serwer.sin_port = htons (5000);
  85.             serwer.sin_addr.s_addr = (INADDR_BROADCAST);
  86.  
  87.             //
  88.             klient.sin_family = AF_INET;
  89.             klient.sin_port = htons (300);
  90.             klient.sin_addr.s_addr = htonl (INADDR_ANY);
  91.         }
  92.     }
  93.  
  94.     hostent ZwrocHostent()
  95.     {
  96.         return *rH;
  97.     }
  98.  
  99.     sockaddr_in Zwrocserwer()
  100.     {
  101.         return serwer;
  102.     }
  103.    
  104.     sockaddr_in Zwrocklient()
  105.     {
  106.         return klient;
  107.     }
  108.  
  109. };
  110. */
  111.  
  112. struct Adresowa
  113. {
  114.     char adres[16];
  115.     int liczba;
  116. };
  117.  
  118. struct Odb
  119. {
  120.     int liczba;
  121.     char znak[32];
  122. };
  123.  
  124. int Sprawdz (int czas, SOCKET gniazdo)
  125. {
  126.     timeval timer = {czas/1000,(czas%1000)*1000};
  127.     fd_set set;
  128.     FD_ZERO (&set);
  129.     FD_SET (gniazdo,&set);
  130.     int wyjscie = select (FD_SETSIZE,&set,NULL,NULL,&timer);
  131.     return wyjscie;
  132. }
  133.  
  134. int _tmain(int argc, _TCHAR* argv[])
  135. {
  136.     vector<Adresowa>dane;
  137.    
  138.     //srand(time(NULL));
  139.    
  140.  
  141.     TInicjalizacja TW;
  142.    
  143. /////////////////////////////////////////////////////////////
  144.     SOCKET gniazdo;
  145.     gniazdo = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  146.     if (gniazdo == INVALID_SOCKET)
  147.     {
  148.         cout << "Nie mozna stworzyc socket" << endl;
  149.         getch();
  150.         return 1;
  151.     }
  152.  
  153.     bool optVal = true;
  154.     if ( (setsockopt (gniazdo,SOL_SOCKET,SO_BROADCAST,(char*)&optVal,sizeof(bool))) == SOCKET_ERROR)
  155.     {
  156.         cout << "blad setsockopt" << endl;
  157.         getch();
  158.         return 1;
  159.     }
  160.  
  161.     //tworzenie RECV
  162.  
  163.     sockaddr_in pobierz;
  164.     pobierz.sin_family = AF_INET;
  165.     pobierz.sin_addr.s_addr = INADDR_ANY;
  166.     pobierz.sin_port = htons(5000);
  167.  
  168.     // tworzenie SENDTO
  169.     sockaddr_in wyslij;
  170.     wyslij.sin_family = AF_INET;
  171.     wyslij.sin_port = htons (5000);
  172.     wyslij.sin_addr.s_addr = INADDR_BROADCAST;
  173.  
  174.     //BINDOWANIE
  175.  
  176.     if ((bind(gniazdo,(SOCKADDR*)&pobierz,sizeof (pobierz))) == SOCKET_ERROR)
  177.     {
  178.         cout << "blad";
  179.         getch();
  180.         return 1;
  181.     }
  182.  
  183.     while(true)
  184.     {
  185.         int x = _kbhit();
  186.         if(x!=0)
  187.         {
  188.             if (getch() == VK_ESCAPE) break;
  189.  
  190.             int zmienna = (rand()%20)+20;
  191.            
  192.             if ((sendto (gniazdo,(char*)&zmienna,sizeof(int),0,(SOCKADDR*)&wyslij,sizeof(wyslij))) == SOCKET_ERROR)
  193.             {
  194.                 cout << "blad";
  195.                 getch();
  196.                 return 1;
  197.             }
  198.  
  199.             cout << "Wyslalem zmienna: " << zmienna << endl;
  200.         }
  201.         else
  202.         {
  203.             if (Sprawdz (0,gniazdo) > 0)
  204.             {
  205.                 char odebrane[64];
  206.                 memset (odebrane,0,sizeof(odebrane));
  207.                 int rozm = sizeof(pobierz);
  208.                 if ((recvfrom (gniazdo,odebrane,sizeof(odebrane),0,(SOCKADDR*)&pobierz,&rozm)) == SOCKET_ERROR)
  209.                 {
  210.                     cout << "blad";
  211.                     getch();
  212.                     return 1;
  213.                 }
  214.                 bool flaga = true;
  215.  
  216.                 for (int i=0; i<dane.size(); i++)
  217.                 {
  218.                     if (strcmp (inet_ntoa (pobierz.sin_addr),dane[i].adres) == 0)
  219.                     {
  220.                         dane[i].liczba += (((Odb*)odebrane)->liczba);
  221.                         flaga = false;
  222.                     }
  223.  
  224.                     printf ("%s::%i \n", dane[i].adres,dane[i].liczba);
  225.                 }
  226.                 if(flaga)
  227.                 {
  228.                     Adresowa obiekt;
  229.                     strcpy (obiekt.adres,inet_ntoa(pobierz.sin_addr));
  230.                     obiekt.liczba = (((Odb*)odebrane)->liczba);
  231.                     dane.push_back(obiekt);
  232.                     for (int i=0; i<dane.size(); i++)
  233.                     {
  234.                         printf ("%s:%i \n",dane[i].adres,dane[i].liczba);
  235.                     }
  236.                 }
  237.             }
  238.         }
  239.  
  240.  
  241.     }
  242.  
  243.  
  244.  
  245.     cout << "Koniec programu" << endl;
  246.     getch();
  247.     return 0;
  248.  
  249. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment