-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNAADS_receiver.cpp
69 lines (60 loc) · 2.04 KB
/
NAADS_receiver.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by Siwei He on 2024-08-18.
//
#include "NAADS_receiver.h"
#include <iostream>
#include <unistd.h> // For close()
#include "NAAD_alert_message.h"
NAADS_RECEIVER::NAADS_RECEIVER(const char* hostname, int port) {
this->hostname = hostname;
this->port = port;
this->sock = socket(AF_INET, SOCK_STREAM, 0);
if (this->sock < 0) {
std::cerr << "Error creating socket!" << std::endl;
return;
}
this->host = gethostbyname(hostname);
if (this->host == nullptr) {
std::cerr << "Error resolving hostname!" << std::endl;
close();
return;
}
memset(&this->server_addr, 0, sizeof(this->server_addr));
this->server_addr.sin_family = AF_INET;
this->server_addr.sin_port = htons(port);
memcpy(&this->server_addr.sin_addr.s_addr, this->host->h_addr, this->host->h_length);
}
NAADS_RECEIVER::~NAADS_RECEIVER() {
close();
}
void NAADS_RECEIVER::connect() {
if (::connect(this->sock, reinterpret_cast<struct sockaddr *>(&this->server_addr), sizeof(this->server_addr)) < 0) {
std::cerr << "Error connecting to server!" << std::endl;
close();
return;
}
std::cout << "Connected to " << this->hostname << " on port " << this->port << std::endl;
}
void NAADS_RECEIVER::receive() const {
NAAD_ALERT_MESSAGE alert_message;
char buffer[4096];
while (true) {
if (const ssize_t bytes_received = ::recv(this->sock, buffer, sizeof(buffer) - 1, 0); bytes_received > 0) {
buffer[bytes_received] = '\0'; // Null-terminate the response
alert_message.append(buffer);
if (alert_message.try_parse_alert()) {
alert_message.print();
alert_message.clear();
}
} else if (bytes_received == 0) {
std::cout << "Connection closed by peer." << std::endl;
break;
} else {
std::cerr << "Error receiving response!" << std::endl;
break;
}
}
}
void NAADS_RECEIVER::close() const {
::close(this->sock);
}