// this is the client. // compile with CC client.cpp -o client // the server must already be running before you run the client // and you must know the server's port number, e.g. 21000 // if the client and server are on the same computer, just say // client 21000 // otherwise, say something like // client rabbit.eng.miami.edu 21000 // (the client must be told where the server is) // the only useful things in this file are the "connection" and // "event" objects, and of course the sample main() #include #include #include #include #include #include #include #include #include #include #include #include #include #include extern int errno; string is(int n) { char s[20]; sprintf(s, "%d", n); return s; } const string timeout = "timeout"; const string disconnection = "disconnection"; const string keyboard = "keyboard"; const string server = "server"; struct event { string what; string data; event(string a, string c) { what=a; data=c; } }; struct connection { int port; string hostname, ip; int main_socket; connection(string hostname, int port); event wait(double maxtime=-1); void send(string s); void disconnect(); }; connection::connection(string hn, int p) { hostname=hn; port=p; hostent * record = gethostbyname(hostname.c_str()); if (record==NULL) { herror("gethostbyname failed"); exit(1); } in_addr * addressptr = (in_addr *) record->h_addr; ip=inet_ntoa(* addressptr); main_socket = socket(AF_INET, SOCK_STREAM, 0); if (main_socket<0) { perror("socket creation"); exit(1); } sockaddr_in server_info; server_info.sin_len = sizeof(server_info); server_info.sin_family = AF_INET; server_info.sin_addr = * addressptr; server_info.sin_port = htons(port); int r = connect(main_socket, (sockaddr *) &server_info, sizeof(server_info)); if (r<0) { perror("connect"); exit(1); } } void connection::send(string s) { write(main_socket, s.c_str(), s.length()); } event connection::wait(double maxtime) { while (1) { fd_set fds; FD_ZERO(&fds); FD_SET(main_socket, &fds); FD_SET(0, &fds); timeval maxwait; maxwait.tv_sec=(int)maxtime; maxwait.tv_usec=(int)((maxtime-(int)maxtime)*1000000); int r3; if (maxtime<0) r3 = select(main_socket+1, &fds, NULL, NULL, NULL); else r3 = select(main_socket+1, &fds, NULL, NULL, &maxwait); if (r3<0) { perror("select"); sleep(2); continue; } else if (r3==0) return event(timeout, timeout); else if (FD_ISSET(main_socket, &fds)) { char s[1000]; int n=read(main_socket, s, 999); if (n<=0) { close(main_socket); return event(disconnection, ""); } else { s[n]=0; return event(server, s); } } else if (FD_ISSET(0, &fds)) { char s[1000]; int n=read(0, s, 999); while (n>0 && s[n-1]<' ') n-=1; s[n]=0; return event(keyboard, s); } } } void connection::disconnect() { if (main_socket>=0) close(main_socket); main_socket=-1; } void main(int argc, char *argv[]) { char * server = "127.0.0.1"; int port; if (argc>=3) { server=argv[1]; port=atol(argv[2]); } else port=atol(argv[1]); connection con(server, port); while (true) { event e = con.wait(); if (e.what==disconnection) { cout << "I've been disconnected\n"; exit(1); } else if (e.what==server) cout << "received from the server: " << e.data << "\n"; else if (e.what==keyboard) con.send(e.data); else cout << e.what << " '" << e.data << "'\n"; } }