#include #include #include "socketfun.h" int inout(int in, int out) { char s[1000]; int i; i = 0; while(read(in, s+i, 1) != 0) { if (s[i] == '\n') { write(out, s, i+1); return i+1; } i++; } return 0; } main(int argc, char **argv) { char *hn, *un; int port, sock, fd; int i; char s[1000]; if (argc != 4) { fprintf(stderr, "usage: alternate hostname port s|c\n"); exit(1); } hn = argv[1]; port = atoi(argv[2]); if (port < 5000) { fprintf(stderr, "usage: alternate hostname port\n"); fprintf(stderr, " port must be > 5000\n"); exit(1); } un = getenv("USER"); if (argv[3][0] == 's') { sock = serve_socket(hn, port); fd = accept_connection(sock); } else { fd = request_connection(hn, port); } printf("Connection established. Client should start talking\n", un); if (argv[3][0] == 's') { if (inout(fd, 1) == 0) exit(0); } while(1) { if (inout(0, fd) == 0) exit(0); if (inout(fd, 1) == 0) exit(0); } }