#include "csapp.h" #define MAX 20 char buf[MAX]; int main() { /* Server opens a listening socket. * Server listens for incoming connection * requests from clients on that socket */ rio_t rio; int n, listenfd, tofromClient; struct sockaddr clientaddr; int clientlen = sizeof(clientaddr); listenfd = Open_listenfd(5555); while(1) { tofromClient = Accept(listenfd, &clientaddr, &clientlen); /* Unix I/O * n = read(tofromClient, buf, MAX); */ /* n = Rio_readn(tofromClient, buf, MAX); Rio_writen(1, buf, n); fflush(stdout); */ Rio_readinitb(&rio, tofromClient); n = Rio_readlineb(&rio, buf, MAX); Rio_writen(1, buf, n); Close(tofromClient); } Close(listenfd); return 0; }