8 #include <netinet/in.h>
9 #include <sys/socket.h>
11 #include <arpa/inet.h>
13 #define PORT "3490" // the port client will be connecting to
15 #define MAXDATASIZE 100 // max number of bytes we can get at once
17 // get sockaddr, IPv4 or IPv6:
18 void *get_in_addr(struct sockaddr
*sa
)
20 if (sa
->sa_family
== AF_INET
) {
21 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
24 return &(((struct sockaddr_in6
*)sa
)->sin6_addr
);
27 int main(int argc
, char *argv
[])
30 char buf
[MAXDATASIZE
];
31 struct addrinfo hints
, *servinfo
, *p
;
33 char s
[INET6_ADDRSTRLEN
];
36 fprintf(stderr
,"usage: client hostname 'message'\n");
40 memset(&hints
, 0, sizeof hints
);
41 hints
.ai_family
= AF_UNSPEC
;
42 hints
.ai_socktype
= SOCK_STREAM
;
44 if ((rv
= getaddrinfo(argv
[1], PORT
, &hints
, &servinfo
)) != 0) {
45 fprintf(stderr
, "getaddrinfo: %s\n", gai_strerror(rv
));
49 // loop through all the results and connect to the first we can
50 for(p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
51 if ((sockfd
= socket(p
->ai_family
, p
->ai_socktype
,
52 p
->ai_protocol
)) == -1) {
53 perror("client: socket");
57 if (connect(sockfd
, p
->ai_addr
, p
->ai_addrlen
) == -1) {
59 perror("client: connect");
67 fprintf(stderr
, "client: failed to connect\n");
71 if (send(sockfd
, argv
[2], strlen(argv
[2])+1, 0) == -1)