Advertisement
Cieslin

WinSock_UDP_Broadcast

Apr 17th, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. // Klient.cpp : Defines the entry point for the console application.
  2. // 2018-04-13-udp-broadcast-kilent.cpp : Defines the entry point for the console application.
  3. #include "stdafx.h"
  4. #include <winsock.h>
  5. #include <conio.h>
  6. #include <locale.h>
  7.  
  8. #pragma comment(lib, "ws2_32")
  9.  
  10. int main() {
  11.     setlocale(LC_ALL, "Polish");
  12.     WSAData ws;
  13.     if (WSAStartup(MAKEWORD(1, 0), &ws) != 0)
  14.     {
  15.         printf("Błąd inicjalizacji biblioteki WinSock.\n");
  16.         _getch();
  17.         return 0;
  18.     }
  19.     printf("Biblioteka WinSock utworzona pomyslnie.\n");
  20.     SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  21.     if (sock == INVALID_SOCKET)
  22.     {
  23.         printf("Błąd utworzenia gniazda.\n");
  24.         _getch();
  25.         WSACleanup();
  26.         return 0;
  27.     }
  28.     printf("Gniazdo utworzone pomyślnie.\n");
  29.     sockaddr_in broadcast;
  30.     broadcast.sin_addr.s_addr = INADDR_BROADCAST;
  31.     broadcast.sin_port = htons(7602);
  32.     broadcast.sin_family = AF_INET;
  33.     int one = 1;
  34.     setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&one, sizeof(one));
  35.  
  36.     sockaddr_in address;
  37.     int addrSize = sizeof(address);
  38.  
  39.     sockaddr_in serverUnicast;
  40.     serverUnicast.sin_addr.s_addr = inet_addr("192.168.0.13");
  41.     serverUnicast.sin_port = htons(7602);
  42.     serverUnicast.sin_family = AF_INET;
  43.  
  44.     timeval time = { 0, 100000 };
  45.     fd_set set;
  46.  
  47.     char recBuff[128];
  48.     memset(recBuff, 0, sizeof(recBuff));
  49.  
  50.     char buff[128];
  51.     memset(buff, 0, sizeof(buff));
  52.     buff[0] = 0x30;
  53.     buff[1] = 0x1D;
  54.     strcat(buff, "Ulubiony język programowania?");
  55.     buff[31] = 0x4;
  56.     strcat(buff, "Java");
  57.     buff[36] = 0x3;
  58.     strcat(buff, "C++");
  59.     buff[40] = 0x6;
  60.     strcat(buff, "Python");
  61.     buff[47] = 0x2;
  62.     strcat(buff, "C#");
  63.     sendto(sock, buff, sizeof(buff), 0, (SOCKADDR*)&broadcast, sizeof(broadcast));
  64.  
  65.     while (true)
  66.     {
  67.         FD_ZERO(&set);
  68.         FD_SET(sock, &set);
  69.         if (select(0, &set, 0, 0, &time) > 0)
  70.         {
  71.             if (FD_ISSET(sock, &set))
  72.             {
  73.                 recvfrom(sock, recBuff, sizeof(recBuff), 0, (SOCKADDR*)&address, &addrSize);
  74.             }
  75.         }
  76.         if (_kbhit())
  77.         {
  78.             char c = _getch();
  79.             if (c == 27)
  80.                 break;
  81.             if (c == 32)
  82.             {
  83.                 memset(buff, 0, sizeof(buff));
  84.                 buff[0] = 0x30;
  85.                 buff[1] = 0x1D;
  86.                 strcat(buff, "Ulubiony język programowania?");
  87.                 buff[31] = 0x4;
  88.                 strcat(buff, "Java");
  89.                 buff[36] = 0x3;
  90.                 strcat(buff, "C++");
  91.                 buff[40] = 0x6;
  92.                 strcat(buff, "Python");
  93.                 buff[47] = 0x2;
  94.                 strcat(buff, "C#");
  95.                 sendto(sock, buff, sizeof(buff), 0, (SOCKADDR*)&broadcast, sizeof(broadcast));
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement