Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <fstream>
- #include <vector>
- #include <sstream>
- using namespace std;
- void error(const char* msg)
- {
- perror(msg);
- exit(1);
- }
- void client()
- {
- int sock, n;
- unsigned int length;
- struct sockaddr_in server, from, local;
- char* buffer = "Client message\n";
- char bufferFrom[256];
- bzero(bufferFrom, 256);
- sock = socket(AF_INET, SOCK_DGRAM, 0);
- if (sock < 0)
- error("Socket");
- local.sin_family = AF_INET;
- local.sin_addr.s_addr = inet_addr("10.0.0.21");
- local.sin_port = htons(25025);
- server.sin_family = AF_INET;
- server.sin_port = htons(25025);
- length = sizeof(struct sockaddr_in);
- int reuse = 1;
- if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0)
- perror("setsockopt(SO_REUSEADDR) failed");
- #ifdef SO_REUSEPORT
- if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const char*)&reuse, sizeof(reuse)) < 0)
- perror("setsockopt(SO_REUSEPORT) failed");
- #endif
- if (bind(sock, (struct sockaddr *)&local, length) < 0)
- perror("binding");
- // Get the IPs
- ifstream file;
- stringstream ss;
- vector<string> ips;
- file.exceptions( ifstream::failbit | ifstream::badbit );
- try
- {
- file.open("ips.txt");
- while (!file.eof())
- {
- string ip;
- file >> ip;
- ips.push_back(ip);
- }
- file.close();
- } catch(const ofstream::failure& e)
- {
- cerr << "Exception opening/reading file: " << e.what() << endl;
- }
- if (ips.empty())
- {
- cerr << "No ips" << endl;
- close(sock);
- return;
- }
- for (size_t i = 0; i < ips.size(); i++)
- {
- cout << i << ") Sending message to: " << ips[i] << endl;
- server.sin_addr.s_addr = inet_addr(ips[i].c_str());
- n = sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr *)&server, length);
- if (n < 0)
- {
- perror("Sendto");
- return;
- }
- }
- close(sock);
- }
- int main()
- {
- thread clientThread(client);
- clientThread.join();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement