Advertisement
peterzig

[PWŚS] UDP Broadcast

Apr 4th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #pragma comment(lib, "Ws2_32.lib")
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <windows.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <string>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. int main() {
  14.     WSADATA wsa;  WSAStartup(MAKEWORD(2, 2), &wsa);
  15.     SOCKET sockout = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);  
  16.     SOCKET sockin = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);  
  17.     SOCKADDR_IN broadcast, receive;  
  18.     broadcast.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  19.     broadcast.sin_family = receive.sin_family = AF_INET;  
  20.     broadcast.sin_port = receive.sin_port = htons(5001);  
  21.     receive.sin_addr.s_addr = htonl(INADDR_ANY);  
  22.     bind(sockout, (SOCKADDR*)&broadcast, sizeof(broadcast));
  23.     bind(sockin, (SOCKADDR*)&receive, sizeof(receive));    
  24.     int one = 1;  
  25.     setsockopt(sockout, SOL_SOCKET, SO_BROADCAST, (char*)&one, sizeof(one));
  26.  
  27.     char buffer[200] = "";  
  28.     char random[61];    
  29.     int length, wynik;  
  30.     int inSize = sizeof(receive);  
  31.     fd_set set;  
  32.     timeval timeout;  
  33.     bool exists;
  34.  
  35.     vector<string> hosty;
  36.  
  37.     while (1) {
  38.         if (_kbhit()) { if (_getch() == VK_ESCAPE) {
  39.             sendto(sockout, "KONIEC", strlen("KONIEC"), 0, (SOCKADDR*)&broadcast, sizeof(broadcast));    
  40.             break;
  41.         }
  42.         else {
  43.             srand(clock());    
  44.             length = 20 + rand() % 41;    
  45.             for (int i = 0; i < length; ++i) {
  46.                 srand(clock() + i*i);      
  47.                 random[i] = 32 + rand() % 95;
  48.             }    
  49.             random[length] = '\0';    
  50.             strcpy(buffer, "Piotr Zaporowski ");    
  51.             strcat(buffer, random);    
  52.             sendto(sockout, buffer, strlen(buffer), 0, (SOCKADDR*)&broadcast, sizeof(broadcast));
  53.             }
  54.         }
  55.         else {
  56.             timeout.tv_sec = 0;    
  57.             timeout.tv_usec = 100 * 1000;    
  58.             FD_ZERO(&set);    
  59.             FD_SET(sockin, &set);
  60.             wynik = select(FD_SETSIZE, &set, NULL, NULL, &timeout);  
  61.             if (wynik > 0) {
  62.                 memset(buffer, 0, sizeof(buffer));    
  63.                 recvfrom(sockin, buffer, sizeof(buffer), 0, (SOCKADDR*)&receive, &inSize);    
  64.                 if (!strcmp(buffer, "KONIEC")) {
  65.                     for (int i = hosty.size() - 1; i >= 0; --i) {
  66.                         if (hosty[i] == inet_ntoa(receive.sin_addr)) {
  67.                             hosty.erase(hosty.begin() + i);        break;
  68.                         }
  69.                     }
  70.                 } else {
  71.                     exists = false;      
  72.                     for (int i = 0; i < hosty.size(); ++i) {
  73.                         if (hosty[i] == inet_ntoa(receive.sin_addr)) {
  74.                             exists = true;        
  75.                             break;
  76.                         }
  77.                     }      
  78.                     if (!exists) hosty.push_back(inet_ntoa(receive.sin_addr));          
  79.                     for (int i = 0; i < hosty.size(); ++i) {
  80.                         printf("%s", hosty[i].c_str());      
  81.                         if (i == hosty.size() - 1) printf("\n");      
  82.                         else printf(", "); }      
  83.                     printf("%s: %s\n\n", inet_ntoa(receive.sin_addr), buffer);
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.     shutdown(sockin, 2);  
  90.     shutdown(sockout, 2);  
  91.     closesocket(sockin);
  92.     closesocket(sockout);
  93.  
  94.     WSACleanup();
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement