Advertisement
benp87

Sockets

Jan 15th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <netdb.h>
  11. #include <arpa/inet.h>
  12. #include <fstream>
  13. #include <vector>
  14. #include <sstream>
  15.  
  16. using namespace std;
  17.  
  18. void error(const char* msg)
  19. {
  20.     perror(msg);
  21.     exit(1);
  22. }
  23.  
  24. void client()
  25. {
  26.     int sock, n;
  27.     unsigned int length;
  28.     struct sockaddr_in server, from, local;
  29.     char* buffer = "Client message\n";
  30.     char bufferFrom[256];
  31.  
  32.     bzero(bufferFrom, 256);
  33.  
  34.     sock = socket(AF_INET, SOCK_DGRAM, 0);
  35.     if (sock < 0)
  36.         error("Socket");
  37.  
  38.     local.sin_family = AF_INET;
  39.     local.sin_addr.s_addr = inet_addr("10.0.0.21");
  40.     local.sin_port = htons(25025);
  41.  
  42.     server.sin_family = AF_INET;
  43.     server.sin_port = htons(25025);
  44.  
  45.     length = sizeof(struct sockaddr_in);
  46.  
  47.     int reuse = 1;
  48.     if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
  49.         perror("setsockopt(SO_REUSEADDR) failed");
  50.  
  51.     #ifdef SO_REUSEPORT
  52.         if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0)
  53.             perror("setsockopt(SO_REUSEPORT) failed");
  54.     #endif
  55.  
  56.     if (bind(sock, (struct sockaddr *)&local, length) < 0)
  57.         perror("binding");
  58.  
  59.  
  60.     // Get the IPs
  61.     ifstream file;
  62.     stringstream ss;
  63.     vector<string> ips;
  64.     file.exceptions( ifstream::failbit | ifstream::badbit );
  65.     try
  66.     {
  67.         file.open("ips.txt");
  68.         while (!file.eof())
  69.         {
  70.             string ip;
  71.             file >> ip;
  72.             ips.push_back(ip);
  73.         }
  74.         file.close();
  75.     } catch(const ofstream::failure& e)
  76.     {
  77.         cerr << "Exception opening/reading file: " << e.what() << endl;
  78.     }
  79.  
  80.     if (ips.empty())
  81.     {
  82.         cerr << "No ips" << endl;
  83.         close(sock);
  84.         return;
  85.     }
  86.  
  87.     for (size_t i = 0; i < ips.size(); i++)
  88.     {
  89.         cout << i << ") Sending message to: " << ips[i] << endl;
  90.         server.sin_addr.s_addr = inet_addr(ips[i].c_str());
  91.         n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr *)&server, length);
  92.         if (n < 0)
  93.         {
  94.             perror("Sendto");
  95.             return;
  96.         }
  97.     }
  98.     close(sock);
  99. }
  100.  
  101. int main()
  102. {
  103.     thread clientThread(client);
  104.  
  105.     clientThread.join();
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement