#include #include #include #include #include #include #include #include #include #include #include #define BS 1024 int main(int argc, char *argv[]) { int sock, quit = 0; unsigned long hostname; char buff[BS]; struct sockaddr_in inaddr; struct hostent *gethost; fd_set readfds; if (argc < 3) { printf("usage: %s [hostname] [port]\n", argv[0]); return 0; } if ((gethost = gethostbyname(argv[1])) == NULL) { herror("gethostbyname"); exit(1); } hostname = inet_addr(inet_ntoa(*(struct in_addr *)gethost->h_addr)); inaddr.sin_family = AF_INET; inaddr.sin_addr.s_addr = hostname; inaddr.sin_port = htons(atoi(argv[2])); memset(&inaddr.sin_zero, 0, 8); memset(buff, 0, sizeof(buff)); sock = socket(AF_INET, SOCK_STREAM, 0); if (connect(sock, (struct sockaddr *)&inaddr, sizeof(inaddr)) == -1) { perror("connect"); exit(1); } while (!quit) { FD_ZERO(&readfds); FD_SET(0, &readfds); FD_SET(sock, &readfds); select(sock+1, &readfds, NULL, NULL, NULL); if (FD_ISSET(sock, &readfds)) { memset(buff, 0, BS); recv(sock, buff, BS, 0); write(1, buff, strlen(buff)); } if (FD_ISSET(0, &readfds)) { memset(buff, 0, BS); read(0, buff, BS); send(sock, buff, BS, 0); } } return 0; }