-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDPTest.c
executable file
·76 lines (61 loc) · 2.52 KB
/
UDPTest.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include "logger.h"
#define MAXSTRINGLENGTH 1024
int main(int argc, char *argv[]) {
if (argc < 3 || argc > 4) {
log(FATAL, "Parameter(s) <Server Address/Name> <FQDN> [<Server Port>]");
exit(EXIT_FAILURE);
}
char *server = argv[1]; // First arg: server address/name
char *echoString = argv[2]; // Second arg: FQDN
size_t echoStringLen = strlen(echoString);
// Third arg server port
char * servPort = argv[3];
struct addrinfo addrCriteria; // Criteria for address match
memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
addrCriteria.ai_family = AF_UNSPEC; // Any address family
addrCriteria.ai_socktype = SOCK_DGRAM; // Only datagram sockets
addrCriteria.ai_protocol = IPPROTO_UDP; // Only UDP protocol
// Get address(es)
// TODO: recorrer todas las IPs devueltas por getaddrinfo, hasta que una responda
struct addrinfo *servAddr; // List of server addresses
int rtnVal = getaddrinfo(server, servPort, &addrCriteria, &servAddr);
if (rtnVal != 0) {
log(FATAL, "getaddrinfo() failed: %s", gai_strerror(rtnVal));
exit(EXIT_FAILURE);
}
// Create a datagram/UDP socket
int sock = socket(servAddr->ai_family, servAddr->ai_socktype,
servAddr->ai_protocol); // Socket descriptor for client
if (sock < 0) {
log(FATAL, "socket failed");
exit(EXIT_FAILURE);
}
// Send the string to the server
ssize_t numBytes = sendto(sock, echoString, echoStringLen, 0,
servAddr->ai_addr, servAddr->ai_addrlen);
if (numBytes < 0) {
log(FATAL, "sendto failed");
exit(EXIT_FAILURE);
}
// Receive a response
struct sockaddr_storage fromAddr; // Source address of server
// Set length of from address structure (in-out parameter)
socklen_t fromAddrLen = sizeof(fromAddr);
char buffer[MAXSTRINGLENGTH + 1]; // I/O buffer
numBytes = recvfrom(sock, buffer, MAXSTRINGLENGTH, 0,
(struct sockaddr *) &fromAddr, &fromAddrLen);
if (numBytes < 0) {
log(FATAL, "recvfrom() failed");
exit(EXIT_FAILURE);
}
buffer[numBytes] = '\0'; // Null-terminate received data
printf("Received: %s\n", buffer);
close(sock);
exit(0);
}