-
Notifications
You must be signed in to change notification settings - Fork 1
/
neighbor.cpp
41 lines (37 loc) · 992 Bytes
/
neighbor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdio>
#include <cstring>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include "neighbor.hpp"
using namespace std;
void Neighbors::broadcast(string message)
{
cout << "廣播 " << message << endl;
for (const Neighbor &neighbor : neighbors) {
int fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd == -1) {
printf("socket() error\n");
continue;
}
struct sockaddr_in info;
memset(&info, 0, sizeof(info));
info.sin_family = PF_INET;
info.sin_addr.s_addr = inet_addr(neighbor.ip.c_str());
info.sin_port = htons(neighbor.port);
if (connect(fd, (struct sockaddr *)&info, sizeof(info)) == -1) {
printf("\033[0;32;31m連線至 %s:%d 錯誤\033[m\n", neighbor.ip.c_str(), neighbor.port);
} else {
send(fd, message.c_str(), message.length(), 0);
}
close(fd);
}
}
void Neighbors::addNeighbor(Neighbor neighbor)
{
neighbors.push_back(neighbor);
}