forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.c
32 lines (24 loc) · 787 Bytes
/
test2.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
#include "unp.h"
int
main(int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE + 1];
socklen_t salen;
struct sockaddr *sa, *sabind;
if (argc != 3)
err_quit("usage: test2 <hostname/IPaddress> <service/port#>");
sockfd = Udp_client(argv[1], argv[2], (void **) &sa, &salen);
/* Same as daytimeudpcli1, but we explicitly bind the wildcard */
sabind = Malloc(salen);
bzero(sabind, salen);
sabind->sa_family = sa->sa_family;
Bind(sockfd, sabind, salen);
printf("bound %s\n", Sock_ntop(sabind, salen));
printf("sending to %s\n", Sock_ntop_host(sa, salen));
Sendto(sockfd, "", 1, 0, sa, salen); /* send 1-byte datagram */
n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = 0; /* null terminate */
Fputs(recvline, stdout);
exit(0);
}