-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_cli.cpp
More file actions
69 lines (57 loc) · 1.96 KB
/
echo_cli.cpp
File metadata and controls
69 lines (57 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "csapp.h"
char request[] = "GET / HTTP/1.1\r\nHost: www.blue.com\r\n\r\n";
char response[MAXLINE];
char buf[MAXLINE];
int main(int argc, char **argv)
{
int clientfd;
char *port;
char *host;
char crlf[] = "\r\n";
rio_t rio_client;
if (argc != 3) {
fprintf(stderr, "\nusage: %s <host> <port>\n", argv[0]);
exit(0);
}
host = argv[1];
port = argv[2];
clientfd = open_clientfd(host, port);
rio_readinitb(&rio_client, clientfd);
rio_writen(clientfd, request, strlen(request));
fputs(buf, stdout);
int n = rio_readlineb(&rio_client, buf, MAXLINE);
while (strcasecmp(buf, crlf)) //expression says false if buf[] == "\r\n". Else, while loop executes again.
{
if(n == 0) {
printf("\nrio_readnb has returned a zero. This means the remote peer closed the connection connection prematurely");
break;
}
else if(n < 0 && errno != EINTR) {
printf("\nSome error, because read() returned a negative value.");
break;
}
else if (n > 0) strcat(response, buf);
n = rio_readlineb(&rio_client, buf, MAXLINE);
}
if(n > 0) strcat(response, buf);
while (1)
{
n = rio_readnb( &(rio_client), buf, MAXLINE);
if(n == 0) break; // EOF
else if (n > 0) strcat(response, buf);
else if(errno != EINTR) {
printf("\nSome error, because read() returned a negative value");
break;
}
}
printf("\nresponse was :\n%s", response);
/* //below is the actual code for echo client. But, we are testing the http proxy now. So, currently using the 3 lines of code above.
while (fgets(buf, MAXLINE, stdin) != NULL) {
rio_writen(clientfd, buf, strlen(buf));
rio_readlineb(&rio_client, buf, MAXLINE);
fputs(buf, stdout);
}
*/
printf("\n");
close(clientfd);
}