16 #include <arpa/inet.h>
17 #include <netinet/in.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
39 int broadcastEnable = 1;
42 if((udp->
sd_send = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) ==
s_ERROR) {
43 printf(
"udp_init_broadcast: could not open a send socket.\n");
48 if(setsockopt(udp->
sd_send, SOL_SOCKET, SO_BROADCAST, &broadcastEnable,
sizeof(broadcastEnable)) ==
s_ERROR) {
49 printf(
"udp_init_broadcast: could not enable broadcast.\n");
59 printf(
"udp_init_broadcast: inet_pton failed.\n");
75 if((udp->
sd_recv = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) ==
s_ERROR) {
76 printf(
"udp_init_receive: could not open a receive socket.\n");
84 udp->
sock_recv.sin_addr.s_addr = htonl(INADDR_ANY);
88 printf(
"udp_init_receive: could not bind a socket.\n");
143 printf(
"udp_close: could not close UDP sockets.\n");
164 printf(
"udp_broadcast: could not send UDP packet.\n");
193 if((n = select(udp->
sd_recv + 1, &infds, NULL, NULL, &tv)))
196 if((*len = recvfrom( udp->
sd_recv,
203 printf(
"udp_receive: could not receive UDP packet.\n");
int udp_receive(udp_t *udp, char *packet, int *len)
int udp_close(udp_t *udp)
struct sockaddr_in sock_recv
struct sockaddr_in sock_send
int udp_init_broadcast(udp_t *udp)
struct sockaddr_in sock_conn
int udp_broadcast(udp_t *udp, char *packet, int len)
udp_t * udp_open(char *broadcast_ip, int port, int packet_size)
int udp_init_receive(udp_t *udp)