
CS485G Spring 2015 49
9. Sample client for ”echo”
1 int client(int argc, char
**
argv) {
2 int toserverfd, port;
3 char
*
host, buf[MAXLINE];
4 rio_t rio;
5 if (argc != 3) {
6 fprintf(stderr, "Usage: %s host port\n", argv[0]);
7 exit(1);
8 }
9 host = argv[1]; port = atoi(argv[2]);
10 toserverfd = open_clientfd(host, port);
11 Rio_readinitb(&rio, toserverfd);
12 printf("type: "); fflush(stdout);
13 while (Fgets(buf, MAXLINE, stdin) != NULL) {
14 Rio_writen(toserverfd, buf, strlen(buf));
15 Rio_readlineb(&rio, buf, MAXLINE);
16 printf("echo: ");
17 Fputs(buf, stdout);
18 printf("type: "); fflush(stdout);
19 }
20 Close(toserverfd);
21 exit(0);
22 } // client
Comentarios a estos manuales