-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathraven-server.cpp
More file actions
61 lines (49 loc) · 1.26 KB
/
raven-server.cpp
File metadata and controls
61 lines (49 loc) · 1.26 KB
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
#include "raven/raven.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char** argv)
{
if (argc != 3) {
cout << "Usage: " << argv[0] << " <ip-address> <port-number>" << endl;
cout << endl;
return -1;
}
int fd = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(argv[1]);
sin.sin_port = htons(atoi(argv[2]));
if (bind(fd, (struct sockaddr*)&sin, sizeof(sin))) {
cout << "Cannot bind: " << errno << ", " << strerror(errno) << endl;
close(fd);
return -1;
}
char buffer[65536];
while (true) {
int n = recv(fd, buffer, sizeof(buffer), 0);
if (n < 0) {
cout << "=== error: " << errno << ", " << strerror(errno) << endl;
} else {
char* eoleol = (char*)memmem(buffer, n, "\n\n", 2);
if (eoleol) {
raven::Message msg;
std::string encoded(eoleol + 2, buffer + n);
(*eoleol) = '\0';
if (raven::decode(encoded, msg)) {
cout << "=== received: " << buffer << endl;
raven::to_json_stream(msg, cout);
} else {
cout << "=== error: cannot decode message" << endl;
}
}
}
}
return 0;
}