89e71e66073f83cca0bf9fb557b675290506a40b
8 #include <netinet/in.h>
9 #include <sys/socket.h>
11 #include <arpa/inet.h>
14 #define MAX_MSG_ACCEPT_BYTES 100
16 // look for an IPv4/6 host:port in an address structure
17 void *get_in_addr(struct sockaddr
*sa
)
19 if (sa
->sa_family
== AF_INET
) {
20 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
22 return &(((struct sockaddr_in6
*)sa
)->sin6_addr
);
25 int main(int argc
, char *argv
[])
28 struct addrinfo hints
, *servinfo
, *port
;
29 char s
[INET6_ADDRSTRLEN
];
31 char buf
[MAX_MSG_ACCEPT_BYTES
];
34 fprintf(stderr
, "usage: client hostname 'message'\n");
38 memset(&hints
, 0, sizeof hints
);
39 hints
.ai_family
= AF_UNSPEC
;
40 hints
.ai_socktype
= SOCK_STREAM
;
42 // look up local address information
43 if ((rv
= getaddrinfo(argv
[1], PORT
, &hints
, &servinfo
)) != 0) {
44 fprintf(stderr
, "getaddrinfo: %s\n", gai_strerror(rv
));
48 // loop over available interfaces and try to bind to port
49 for(port
= servinfo
; port
!= NULL
; port
= port
->ai_next
) {
50 if ((sockfd
= socket(port
->ai_family
, port
->ai_socktype
,
51 port
->ai_protocol
)) == -1) {
52 perror("client: socket");
55 if (connect(sockfd
, port
->ai_addr
, port
->ai_addrlen
) == -1) {
57 perror("client: connect");
64 fprintf(stderr
, "client: failed to connect\n");
68 if (send(sockfd
, argv
[2], strlen(argv
[2])+1, 0) == -1)