#include #include #include #include // unix standard (for close) #include #include #include // program for connecting to a time service port (the daytime service) // supported by: time.nist.gov for instance // see complete list at: http://tf.nist.gov/tf-cgi/servers.cgi# // // int getaddrinfo(const char *hostname, const char *servname, // const struct addrinfo *hints, struct addrinfo **res); // // struct addrinfo { // int ai_flags; /* input flags */ // int ai_family; /* protocol family for socket */ // int ai_socktype; /* socket type */ // int ai_protocol; /* protocol for socket */ // socklen_t ai_addrlen; /* length of socket-address */ // struct sockaddr *ai_addr; /* socket-address for socket */ // char *ai_canonname; /* canonical name for service location */ // struct addrinfo *ai_next; /* pointer to next in list */ // }; // // ---- // #include // // ssize_t send(int socket, const void *buffer, size_t length, int flags); // // ---- // #include // // int socket(int domain, int type, int protocol); int main(int argc, char *argv[]) { char *host; struct hostent *hostinfo; struct servent *servinfo; int fd ; // The socket descripter int status; struct sockaddr_in add_info; // The struct that getaddrinfo() fills up with data. // set up the host as the localhost if (argc == 1) { host = (char *)"localhost"; } else { host = argv[1]; } printf("Go to host: %s\n", host); // get hostinfo // // struct hosten { // tchar *h_name; /* official name f host */ // char **h_aliases; /* alias list */ // int h_addrtype; /* host address type */ // int h_length; /* length of address */ // char **h_addr_list; /* list of addresses */ // } hostinfo = gethostbyname(host); if (hostinfo==NULL) { printf("ERROR(gethostbyname): failed to find host %s\n", host); exit(1); } printf("Got host info for: %s\n", hostinfo->h_name); // // structse rvent { // char *s_name; /* official service name */ // char **s_aliases; /* alias list */ // int s_port; /* port number */ // char *s_proto; /* protocol to use */ // } servinfo = getservbyname("daytime", "tcp"); if (servinfo==NULL) { perror("ERROR(getservbyname): no daytime tcp service.\n"); exit(1); } printf("Got %s service port: %d\n", servinfo->s_name, servinfo->s_port); // getaddrinfo returns 0 on success, or some other value when an error occured. // (translated into human readable text by the gai_gai_strerror function). // if (status != 0) std::cout << "getaddrinfo error" << gai_strerror(status) ; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd==-1) { printf("ERROR(socket): socket call failed.\n"); perror("xxx"); } else { printf("made socket\n"); } add_info.sin_family = AF_INET; add_info.sin_port = servinfo->s_port; add_info.sin_addr = *(struct in_addr *)*hostinfo->h_addr_list; // connect printf("Try to connect to fd: %d\n", fd); status = connect(fd, (const struct sockaddr *)&add_info, sizeof(add_info)); if (status) { printf("ERROR(connect): %d\n", status); perror("connect"); } else { printf("connected\n"); } int bytes; int buffsize = 65536; char buff[buffsize]; bytes = read(fd, buff, buffsize-1); buff[bytes] = '\0'; printf("Read %d bytes from %s: %s\n", bytes, hostinfo->h_name, buff); close(fd); return 0; }