forked from kukulski/BeagleBoneUDPLED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UDPSender.cpp
97 lines (78 loc) · 2.06 KB
/
UDPSender.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
// UDPSender.cpp
// TabletServer
//
// Created by Timothy Kukulski on 10/22/12.
//
//
#include "UDPSender.h"
#include <netdb.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
UDPSender::UDPSender(const char *host, int port) {
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;// AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = NI_NUMERICSERV;
char portString[128];
sprintf(portString,"%d",port);
if ((rv = getaddrinfo(host, portString, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
}
// loop through all the results and make a socket
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("talker: socket");
continue;
}
break;
}
}
//UDPSender::~UDPSender() {
// freeaddrinfo(servinfo);
// close(sockfd);
//}
void UDPSender::send(void *data, size_t count) {
size_t numbytes;
if ((numbytes = sendto(sockfd, data,count, 0,
p->ai_addr, p->ai_addrlen)) == -1) {
perror("talker: sendto");
exit(1);
}
}
void error(const char *msg)
{
perror(msg);
exit(0);
}
UDPListener::UDPListener(int port) {
sock = socket(AF_INET, SOCK_DGRAM,0);
if (sock < 0) error("Opening socket");
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(port);
if (bind(sock,(struct sockaddr *)&server,sizeof(server))<0)
error("binding");
}
//UDPListener::~UDPListener() {
// close(sock);
//}
size_t UDPListener::listen(void *data, size_t maxcount) {
socklen_t fromlen = sizeof(from);
return recvfrom(sock,data,maxcount,0,(struct sockaddr *)&from,&fromlen);
}
void UDPListener::setNonblocking() {
int flags;
flags = fcntl(sock,F_GETFL,0);
assert(flags != -1);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
}