2 #include <sys/socket.h>
5 #include <netinet/in.h>
15 usage(const char *cmd
)
17 fprintf(stderr
, "%s -4 ip4 -p port\n", cmd
);
22 main(int argc
, char *argv
[])
24 struct sockaddr_in in
;
28 memset(&in
, 0, sizeof(in
));
29 in
.sin_family
= AF_INET
;
31 while ((opt
= getopt(argc
, argv
, "4:p:")) != -1) {
34 if (inet_pton(AF_INET
, optarg
, &in
.sin_addr
) <= 0)
39 in
.sin_port
= strtol(optarg
, NULL
, 10);
40 in
.sin_port
= htons(in
.sin_port
);
48 if (in
.sin_addr
.s_addr
== INADDR_ANY
|| in
.sin_port
== 0)
51 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
53 err(2, "socket failed");
55 if (connect(s
, (const struct sockaddr
*)&in
, sizeof(in
)) < 0)
56 err(2, "connect failed");
58 n
= write(s
, buf
, sizeof(buf
));
60 err(2, "write failed");
61 else if (n
!= (int)sizeof(buf
))
62 errx(2, "written truncated data %d", n
);
64 n
= read(s
, buf
, sizeof(buf
));
66 err(2, "read failed");
67 printf("read %d, done\n", n
);