#include #include "socketfun.h" #include #include /* This thread reads from the socket, and writes to standard output. */ void *thread(void *arg) { char buf[1000]; FILE *fin; fin = (FILE *) arg; while (fgets(buf, 1000, fin) != NULL) { fputs(buf, stdout); fflush(stdout); } exit(0); return NULL; } /* This thread reads from standard input, and writes to the socket. */ int main(int argc, char **argv) { int fd; FILE *fin, *fout; char buf[1000]; pthread_t tid; if (argc != 3) { fprintf(stderr, "usage: telnet host port\n"); exit(1); } fd = request_connection(argv[1], atoi(argv[2])); fin = fdopen(fd, "r"); fout = fdopen(fd, "w"); if (pthread_create(&tid, NULL, thread, (void *) fin) != 0) exit(1); while (fgets(buf, 1000, stdin) != NULL) { fputs(buf, fout); fflush(fout); } return 0; }