






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







/* wait for a connection from an echo client / conn = await_contact((appnum) atoi(argv[1])); if (conn < 0) exit(1); / iterate, echoing all data received until end of file */ while((len = recv(conn, buff, BUFFSIZE, 0)) > 0) (void) send(conn, buff, len, 0); send_eof(conn); return 0;
// convert the arguments to binary format comp and appnum comp = cname_to_comp(argv[1]); if (comp == -1) exit(1); if (argc == 3) app = (appnum) atoi(argv[2]); else if ((app = appname_to_appnum("echo")) == -1) exit(1); /* form a connection with the echoserver */ Note that some C++ programmers prefer to test for equality using a construct such as if ( –1 = = comp) because (comp = – 1) always evaluates to TRUE.
/* read and print same no. of bytes from echo server */ expect = len; for (received = 0; received < expect;) { len = recv(conn, buff, (expect - received) < BUFFSIZE? (expect - received) : BUFFSIZE, 0); if (len < 0) { send_eof(conn); return 1; } (void) write(STDOUT_FILENO, buff, len); received += len; } (void) printf("\n"); (void) printf(INPUT_PROMPT); (void) fflush(stdout); } For the rest of the code, see the textbook.
len = recv(conn, buff, (expect - received) < BUFFSIZE? (expect - received) : BUFFSIZE, 0);
(expect - received) < BUFFSIZE? (expect - received) : BUFFSIZE