2 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT license.
9 #include <netinet/in.h>
13 #include <sys/socket.h>
17 extern const char* __progname
;
21 main(int argc
, char** argv
)
24 printf("usage: %s <host> [port]\n"
25 "Connects to the host (default port 21, ftp), and calls\n"
26 "getpeername() to see if it works correctly with a short\n"
27 "buffer.\n", __progname
);
31 struct hostent
* host
= gethostbyname(argv
[1]);
33 perror("gethostbyname");
41 int socket
= ::socket(AF_INET
, SOCK_STREAM
, 0);
48 memset(&address
, 0, sizeof(sockaddr_in
));
50 address
.sin_family
= AF_INET
;
51 address
.sin_port
= htons(port
);
52 address
.sin_addr
= *((struct in_addr
*)host
->h_addr
);
54 socklen_t length
= 14;
56 int result
= getpeername(socket
, &buffer
, &length
);
57 printf("getpeername() before connect(): %d (%s), length: %d\n", result
,
58 strerror(errno
), length
);
60 if (connect(socket
, (sockaddr
*)&address
, sizeof(sockaddr_in
)) < 0) {
67 result
= getpeername(socket
, &buffer
, &length
);
68 printf("getpeername() after connect(): %d (%s), length: %d\n", result
,
69 strerror(errno
), length
);