/* Helper function that writes a simple html error page to a file descriptor * for 404 error: not found */ void not_found(int fd){ Rio_writen(fd,"<html>\r\n", 8); Rio_writen(fd,"<body>\r\n", 8); Rio_writen(fd,"404: not found", 14); Rio_writen(fd,"</body>\r\n", 9); Rio_writen(fd,"</html>\r\n", 9); }
void requestError( int fd, char *cause, char *errnum, char *shortmsg, char *longmsg ) { char buf[MAXLINE], body[MAXBUF]; printf("Request ERROR\n"); /* Create the body of the error message */ sprintf(body, "<html><title>CS537 Error</title>"); sprintf(body, "%s<body bgcolor=""fffff"">\r\n", body); sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg); sprintf(body, "%s<p>%s: %s\r\n", body, longmsg, cause); sprintf(body, "%s<hr>CS537 Web Server\r\n", body); /* Write out the header information for this response */ sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg); Rio_writen(fd, buf, strlen(buf)); printf("%s", buf); sprintf(buf, "Content-Type: text/html\r\n"); Rio_writen(fd, buf, strlen(buf)); printf("%s", buf); sprintf(buf, "Content-Length: %lu\r\n\r\n", strlen(body)); Rio_writen(fd, buf, strlen(buf)); /* Write out the content */ Rio_writen(fd, body, strlen(body)); printf("%s", body); }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; int n; srcp = malloc(filesize); /* Send response headers to client */ get_filetype(filename, filetype); //line:netp:servestatic:getfiletype sprintf(buf, "HTTP/1.0 200 OK\r\n"); //line:netp:servestatic:beginserve sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); //line:netp:servestatic:endserve /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); //line:netp:servestatic:open //srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);//line:netp:servestatic:mmap while ((n = rio_readn(srcfd, srcp, filesize + 1)) != 0) { Rio_writen(fd, srcp, filesize); } Close(srcfd); //line:netp:servestatic:close free(srcp); // Rio_writen(fd, srcp, filesize); //line:netp:servestatic:write // Munmap(srcp, filesize); //line:netp:servestatic:munmap }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); if(ishttps) { SSL_write(ssl, buf, strlen(buf)); SSL_write(ssl, srcp, filesize); } else { Rio_writen(fd, buf, strlen(buf)); Rio_writen(fd, srcp, filesize); } Munmap(srcp, filesize); }
/* * serve_static - serve static content */ void serve_static(int connfd, char* filename, struct stat filestat) { int srcfd; char *srcp; char filetype[MAXLINE], body[MAXBUF]; /* get file type */ if (strstr(filename, ".html")) strcpy(filetype, "text/html"); else if (strstr(filename, ".gif")) strcpy(filetype, "text/gif"); else if (strstr(filename, ".jpg")) strcpy(filetype, "text/jpg"); else strcpy(filetype, "text/plain"); /* send response headers to client */ sprintf(body, "HTTP/1.0 200 OK\r\n"); sprintf(body, "%sServer: Tiny Web Server\r\n", body); sprintf(body, "%sContent-length: %d\r\n", body, (int)filestat.st_size); sprintf(body, "%sContent-type: %s\r\n\r\n", body, filetype); Rio_writen(connfd, body, strlen(body)); /* send response body to client */ srcfd = Open(filename, O_RDONLY, 0); /* map the file into a chunk of virtual memory */ srcp = Mmap(NULL, filestat.st_size, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); Rio_writen(connfd, srcp, filestat.st_size); Munmap(srcp, filestat.st_size); }
/* * clienterror - returns an error message to the client */ void clienterror(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg) { char buf[MAXLINE], body[MAXBUF]; /* Build HTTP response, like * HTTP/1.0 404 Not Found * Content-type: text/html * Content-length: xxx * * Tiny couldn't find file. * Server: 'The Tiny Web Server' */ /* Build the HTTP response body */ sprintf(body, "<html><title>Tiny Error</title>"); sprintf(body, "%s<body bgcolor=""ffffff"">\r\n", body); sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg); sprintf(body, "%s<p>%s: %s\r\n", body, longmsg, cause); sprintf(body, "%s<hr><em>The Tiny Web server</em>\r\n", body); /* Print the HTTP response */ sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg); Rio_writen(fd, buf, strlen(buf)); sprintf(buf, "Content-type: text/html\r\n"); Rio_writen(fd, buf, strlen(buf)); sprintf(buf, "Content-length: %d\r\n\r\n", (int)strlen(body)); Rio_writen(fd, buf, strlen(buf)); Rio_writen(fd, body, strlen(body)); }
/* $begin serve_static */ void serve_static(int fd, char *filename, int filesize, int is_head) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; /* Send response headers to client */ get_filetype(filename, filetype); // line:netp:servestatic:getfiletype sprintf(buf, "HTTP/1.0 200 OK\r\n"); // line:netp:servestatic:beginserve sprintf(buf, "%sServer: Tiny Web Server\r\n", buf); // char c; // c = getchar(); // printf("%c\n", c);/*to ignore the EPIPE error!!! */ sprintf(buf, "%sContent-length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // line:netp:servestatic:endserve if (is_head) { return; // the request is head } /* Send response body to client */ srcfd = Open(filename, O_RDONLY, 0); // line:netp:servestatic:open srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); // Close(srcfd); // Rio_writen(fd, srcp, filesize); // Munmap(srcp, filesize); // }
void requestServeStatic(int fd, char *filename, int filesize) { int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; requestGetFiletype(filename, filetype); srcfd = Open(filename, O_RDONLY, 0); // Rather than call read() to read the file into memory, // which would require that we allocate a buffer, we memory-map the file srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); // put together response sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%sServer: CS537 Web Server\r\n", buf); sprintf(buf, "%sContent-Length: %d\r\n", buf, filesize); sprintf(buf, "%sContent-Type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // Writes out to the client socket the memory-mapped file Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
void remove_channel(int fd, const char *channel, char *sendbuf) { node_t *prev, *node; printf("Removing Channel %s\n", channel); for (prev = NULL, node = LocalIRCList.ChannelList.header; node != NULL; prev = node, node = node->next) { printf("Existing Channel: %s", node->data); if (strcasecmp(node->data, channel) == 0) { printf("...Found!\n"); break; } printf("\n"); } if (node != NULL) { if (prev == NULL) LocalIRCList.ChannelList.header = node->next; else prev->next = node->next; LocalIRCList.ChannelList.Count--; printf("Removed Channel: %s\n", node->data); free(node->data); free(node); // BroadcastSelf(); sprintf(sendbuf, "OK\r\n"); Rio_writen(fd, sendbuf, strlen(sendbuf)); return; } printf("Cannot Find Channel\n"); sprintf(sendbuf, "OK\r\n"); Rio_writen(fd, sendbuf, strlen(sendbuf)); return; }
void request_write(Request request, int fd) { char buf[MAXLINE]; sprintf(buf, "%s %s %s\r\n", request->method, request->uri, request->version); Rio_writen(fd, buf, strlen(buf)); header_write(request->header, buf, fd); Rio_writen(fd, "\r\n", 2); }
void * keyboard(void * vargp) { char buf[MAXLINE]; rio_t rio; fprintf(stderr,"fprintf 7\n"); int clientfd = *((int*) vargp); fprintf(stderr,"fprintf 8\n"); Rio_readinitb(&rio, clientfd); while(Fgets(buf, MAXLINE, stdin) != NULL) { fprintf(stderr,"fprintf 9\n"); if(active) Rio_writen(clientfd, buf, strlen(buf)); else break; } if(active) { fprintf(stderr,"fprintf 10\n"); strcpy(buf, "$q\n"); Rio_writen(clientfd, buf, strlen(buf)); } return NULL; }
/* Helper function that writes a simple html error page to a file descriptor * for 400 error: bad request */ void bad_request(int fd){ Rio_writen(fd,"<html>\r\n", 8); Rio_writen(fd,"<body>\r\n", 8); Rio_writen(fd,"400: bad request", 16); Rio_writen(fd,"</body>\r\n", 9); Rio_writen(fd,"</html>\r\n", 9); }
/* * Sends the necessary headers and data to the client * */ void sendResponse(int fd, char *data, char *type, char *version, int size) { char buffer[MAXLINE]; if (strcmp(version, "HTTP/1.1") == 0) { sprintf(buffer, "%s 100 Continue\r\n", version); } else { sprintf(buffer, "%s 200 OK\r\n", version); } Rio_writen(fd, buffer, strlen(buffer)); printf("\n%s", buffer); sprintf(buffer, "Content-length: %d\r\n", size); Rio_writen(fd, buffer, strlen(buffer)); printf("%s", buffer); sprintf(buffer, "Content-type: %s\r\n\r\n", type); Rio_writen(fd, buffer, strlen(buffer)); printf("%s", buffer); Rio_writen(fd, data, size); printf("%s", data); }
/* $begin clienterror */ void clienterror(int fd, char *cause, char *errnum, char *shortmsg, char *longmsg) { char buf[MAXLINE], body[MAXBUF]; /* Build the HTTP response body */ sprintf(body, "<html><title>Tiny Error</title>"); sprintf(body, "%s<body bgcolor=""ffffff"">\r\n", body); sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg); sprintf(body, "%s<p>%s: %s\r\n", body, longmsg, cause); sprintf(body, "%s<hr><em>The Tiny Web server</em>\r\n", body); /* Print the HTTP response */ sprintf(buf, "HTTP/1.0 %s %s\r\n", errnum, shortmsg); sprintf(buf, "%sContent-type: text/html\r\n",buf); sprintf(buf, "%sContent-length: %d\r\n\r\n",buf,(int)strlen(body)); printf("...................\n"); printf("%s\n",buf); printf("%s\n",body); printf("...................\n"); if(ishttps) { SSL_write(ssl,buf,strlen(buf)); SSL_write(ssl,body,strlen(body)); } else { Rio_writen(fd, buf, strlen(buf)); Rio_writen(fd, body, strlen(body)); } }
void requestServeStatic(request_t request, char *filename, int filesize, thread_t * thread) { int fd = request.connfd; int srcfd; char *srcp, filetype[MAXLINE], buf[MAXBUF]; char tmp = 0; int i; requestGetFiletype(filename, filetype); srcfd = Open(filename, O_RDONLY, 0); double time_start_read = get_time(); // Rather than call read() to read the file into memory, // which would require that we allocate a buffer, we memory-map the file srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0); Close(srcfd); // The following code is only needed to help you time the "read" given // that the file is memory-mapped. // This code ensures that the memory-mapped file is brought into memory // from disk. // When you time this, you will see that the first time a client //requests a file, the read is much slower than subsequent requests. for (i = 0; i < filesize; i++) { tmp += *(srcp + i); } double time_end_read = get_time(); request.Stat_req_read = time_end_read - time_start_read; double time_start_write = get_time(); request.Stat_req_complete = time_start_write - request.Stat_req_arrival; sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%s Server: CS537 Web Server\r\n", buf); // CS537: Your statistics go here -- fill in the 0's with something useful! sprintf(buf, "%s Stat-req-arrival: %d\r\n", buf, request.Stat_req_arrival); sprintf(buf, "%s Stat-req-dispatch: %d\r\n", buf, request.Stat_req_dispatch); sprintf(buf, "%s Stat-req-read: %d\r\n", buf, request.Stat_req_read); sprintf(buf, "%s Stat-req-complete: %d\r\n", buf, request.Stat_req_complete); sprintf(buf, "%s Stat-req-age: %d\r\n", buf, request.Stat_req_age); sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, thread->Stat_thread_id); sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, thread->Stat_thread_count); sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, thread->Stat_thread_static); sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, thread->Stat_thread_dynamic); sprintf(buf, "%s Content-Length: %d\r\n", buf, filesize); sprintf(buf, "%s Content-Type: %s\r\n\r\n", buf, filetype); Rio_writen(fd, buf, strlen(buf)); // Writes out to the client socket the memory-mapped file Rio_writen(fd, srcp, filesize); Munmap(srcp, filesize); }
void response_write(Response response, int fd) { char buf[MAXLINE]; sprintf(buf, "%s %d %s\r\n", response->version, response->status_code, response->status_name); Rio_writen(fd, buf, strlen(buf)); header_write(response->header, buf, fd); Rio_writen(fd, "\r\n", 2); Rio_writen(fd, response->content, (size_t) response->content_length); }
/* * Send an HTTP request for the specified file */ void clientSend(int fd, char *filename) { char buf[MAXLINE]; char hostname[MAXLINE]; Gethostname(hostname, MAXLINE); /* Form and send the HTTP request */ sprintf(buf, "GET %s HTTP/1.1\n", filename); sprintf(buf, "%shost: %s\n\r\n", buf, hostname); Rio_writen(fd, buf, strlen(buf)); Rio_writen(1, buf, strlen(buf)); }
/* * forward_to_client - forward without write to cache * * used for non GET methods; * * return -1 on error * return 0 on success */ int forward_to_client(int to_client_fd, int to_server_fd) { rio_t rio_server; char buf[MAXLINE]; unsigned int length = 0, size = 0; Rio_readinitb(&rio_server, to_server_fd); // forward status line if (Rio_readlineb(&rio_server, buf, MAXLINE) == -1) { return -1; } if (Rio_writen(to_client_fd, buf, strlen(buf)) == -1) { return -1; } // forward response headers while (strcmp(buf, "\r\n") != 0 && strlen(buf) > 0) { if (Rio_readlineb(&rio_server, buf, MAXLINE) == -1) { return -1; } get_size(buf, &size); if (Rio_writen(to_client_fd, buf, strlen(buf)) == -1) { return -1; } } // forward response body if (size > 0) { while (size > MAXLINE) { if ((length = Rio_readnb(&rio_server, buf, MAXLINE)) == -1) { return -1; } if (Rio_writen(to_client_fd, buf, length) == -1) { return -1; } size -= MAXLINE; } if (size > 0) { if ((length = Rio_readnb(&rio_server, buf, size)) == -1) { return -1; } if (Rio_writen(to_client_fd, buf, length) == -1) { return -1; } } } else { while ((length = Rio_readnb(&rio_server, buf, MAXLINE)) > 0) { if (Rio_writen(to_client_fd, buf, length) == -1) { return -1; } } } return 0; }
void doit(int fd) { int clientfd; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE]; char host[MAXLINE], port[MAXLINE]; char filename[MAXLINE]; char cache_buf[MAX_OBJECT_SIZE]; size_t buflen; struct cache *cache; int cache_size = 0, cache_flag = 1; rio_t c_rio, s_rio; /* Read request line and headers */ Rio_readinitb(&c_rio, fd); if (!Rio_readlineb(&c_rio, buf, MAXLINE)) return; printf("%s", buf); sscanf(buf, "%s %s", method, uri); if (strcasecmp(method, "GET")) { printf("this proxy can handle only \"GET\"\n"); return; } construct_requesthdrs(&c_rio, buf, filename, uri, host, port); if (find_cache(&cache, uri) == 1) { Rio_writen(fd, cache->data, strlen(cache->data)); } else { clientfd = Open_clientfd(host, port); if (clientfd < 0) { printf("there is no such server\n"); return; } Rio_readinitb(&s_rio, clientfd); Rio_writen(clientfd, buf, strlen(buf)); while ((buflen = Rio_readlineb(&s_rio, buf, MAXLINE)) != 0) { Rio_writen(fd, buf, buflen); if (cache_size + buflen > MAX_OBJECT_SIZE) { cache_flag = 0; } else { memcpy(cache_buf + cache_size, buf, buflen); cache_size += buflen; } } if (cache_flag == 1) create_cache(cache_buf, uri); Close(clientfd); } }
/* * doit - handle one HTTP request/response transaction */ void doit(int fd) { int is_static; struct stat sbuf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], cgiargs[MAXLINE]; rio_t rio_c, rio_h; /* Ren's Local Vars */ char host[MAXLINE], url[MAXLINE], request[MAXLINE], header[MAXLINE]; int hostfd; /* Ren's Local Vars END */ /* Read request line and headers */ Rio_readinitb(&rio_c, fd); Rio_readlineb(&rio_c, buf, MAXLINE); sscanf(buf, "%s %s %s", method, uri, version); if (strcasecmp(method, "GET")) { clienterror(fd, method, "501", "Not Implemented", "Tiny does not implement this method"); return; } printf("STUFF FROM THE CLIENT:\n"); printf("%s\n",buf); read_requesthdrs(&rio_c); /* Ren's code */ parseURL(buf, host, uri, version); /* parse url for hostname and uri */ hostfd = Open_clientfd(host, PORT); /* connect to host as client */ Rio_readinitb(&rio_h, hostfd); /* set up host file discriptor */ /* generate and send request to host*/ genrequest(request, method, uri); genheader(host, header); strcat(request, header); printf("%s\n",request); Rio_writen(hostfd, request, strlen(request)); /* stream information from server to client */ printf("STUFF FROM THE SERVER:\n"); while(Rio_readlineb(&rio_h, buf, MAXLINE)){ printf("%s\n",buf); Rio_writen(fd, buf, MAXLINE); } printf("stream ended\n"); /* Ren's code */ }
/* * serve_dynamic- run a CGI program on behalf of the client */ void serve_dynamic(int fd, char *filename, char *cgiargs) { char buf[MAXLINE], *emptylist[] = {NULL}; sprintf(buf, "HTTP/1.0 200 OK\r\n"); Rio_writen(fd, buf, strlen(buf)); sprintf(buf, "Server: Tiny Web Server\r\n"); Rio_writen(fd, buf, strlen(buf)); if (Fork() == 0) { setenv("QUERY_STRING", cgiargs, 1); Dup2(fd, STDOUT_FILENO); Execve(filename, emptylist, environ); } Wait(NULL); }
/* $begin serve_dynamic */ void serve_dynamic(int fd, char *filename, char *cgiargs) { char buf[MAXLINE]; /* Return first part of HTTP response */ sprintf(buf, "HTTP/1.0 200 OK\r\n"); Rio_writen(fd, buf, strlen(buf)); sprintf(buf, "Server: Tiny Web Server\r\n"); Rio_writen(fd, buf, strlen(buf)); Dup2(fd, STDOUT_FILENO); /* Redirect stdout to client */ //line:netp:servedynamic:dup2 int result = functionstub(cgiargs); printf("%d", result); }
int echoClient(int argc, char** argv) { int clientfd, port; char* host, buf[MAXLINE]; rio_t rio; if (argc != 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(0); } host = argv[1]; port = atoi(argv[2]); clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)); // get response from server Rio_readlineb(&rio, buf, MAXLINE); //Fputs(buf, stdout); } Close(clientfd); exit(0); }
int main(int argc, char *argv[]) { int clientfd, port; char *host, buf[MAXLINE]; rio_t rio; /* if(argc!= 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(1); } */ //host = argv[1]; //port = atoi(argv[2]); host ="127.0.0.1"; port=8080; clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while(Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)); Fputs(buf, stdout); } close(clientfd); exit(0); }
void check_client(pool *p) { int i, connfd, n; char buf[MAXLINE]; rio_t rio; for (i = 0; (i <= p->maxi) && (p->nready > 0); i++) { connfd = p->clientfd[i]; rio = p->clientrio[i]; /* If the descriptor is ready, echo a text line from it */ if ((connfd > 0) && (FD_ISSET(connfd, &p->ready_set))) { p->nready--; if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { byte_cnt += n; printf("Server received %d (%d total) bytes on fd %d\n", n, byte_cnt, connfd); Rio_writen(connfd, buf, n); } /* EOF detected, remove descriptor from pool */ else { Close(connfd); FD_CLR(connfd, &p->read_set); p->clientfd[i] = -1; } } } }
void requestServeDynamic(int fd, char *filename, char *cgiargs, request* req) { char buf[MAXLINE], *emptylist[] = {NULL}; /* The server does only a little bit of the header. */ /* The CGI script has to finish writing out the header. */ sprintf(buf, "HTTP/1.0 200 OK\r\n"); sprintf(buf, "%s Server: Tiny Web Server\r\n", buf); /* CS537: Your statistics go here -- fill in the 0's with something * useful! */ sprintf(buf, "%s Stat-req-arrival: %f\r\n", buf, toMS(req->req_arrival)); sprintf(buf, "%s Stat-req-dispatch: %f\r\n", buf, toMS(req->req_dispatch)); sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, req->t_stats.thread_id); sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, req->t_stats.thread_count); sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, req->t_stats.thread_static); sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, req->t_stats.thread_dynamic); Rio_writen(fd, buf, strlen(buf)); if(Fork() == 0) { /* Child process */ Setenv("QUERY_STRING", cgiargs, 1); /* When the CGI process writes to stdout, it will instead go to * the socket */ Dup2(fd, STDOUT_FILENO); Execve(filename, emptylist, environ); } Wait(NULL); }
int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAXLINE]; rio_t rio; if (argc != 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(0); } host = argv[1]; port = atoi(argv[2]); clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) != NULL) { printf("client get: %s\n",buf); Rio_writen(clientfd, buf, strlen(buf)); printf("client buf: %s\n",buf); Rio_readlineb(&rio, buf, MAXLINE); Fputs(buf, stdout); } // printf("outside\n"); Close(clientfd); //line:netp:echoclient:close exit(0); }
int main(int argc, char **argv) { int n; rio_t rio; char buf[MAXLINE]; if (argc > 2) { fprintf(stderr, "usage: %s [filename]\n", argv[0]); return -1; } /* 这里将标准输入的文件描述符,重定位到输入文件的描述符 */ if (argc == 2) { char *filename = argv[1]; int fd = Open(filename, O_RDONLY, 0); int ret = dup2(fd, STDIN_FILENO); /* 将标准输入重定位到 fd */ if (ret < 0) { perror("dup2"); return -1; } } /* 没有输入文件名,将标准输入拷贝到标准输出 */ Rio_readinitb(&rio, STDIN_FILENO); while ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) Rio_writen(STDOUT_FILENO, buf, n); return 0; }
void *routeClientToFICS(void *args) { pthread_detach(pthread_self()); route *R = (route *) args; printf("INFO: route_to was called\n"); printf("INFO: fromfd is %d\n", R->fromfd); printf("INFO: tofd is %d\n", R->tofd); fflush(stdout); // Put the client bytes into the logs // FILE *client_logs; // client_logs = fopen("./logs.txt","wb"); char buf[MAXLINE]; int numread; while ((numread = recv(R->fromfd, buf, 1, 0)) > 0) { // To turn on DEBUG mode, uncomment the following line //putchar(buf[0]); Rio_writen(R->tofd, buf, numread); // fputc(buf[0], client_logs); } // fclose(client_logs); printf("INFO: exiting route_to...\n"); return NULL; }
int main(int argc, char **argv) { if(!(argc >=2) ) //If commandline args are not provided { printf("No file provided\n"); return -1; } //Use csapp Open to get fd number (creates files) int fd = Open(argv[1],O_CREAT | O_WRONLY,0); int n; //Number of bytes rio_t rio; //RIO type that contains file info char buf[MAXLINE]; //Character buffer with max size defined by csapp //Associate a descriptor with a read buffer and reset buffer //the POSIX <unistd.h> definition is STDIN_FILENO Rio_readinitb(&rio,STDIN_FILENO); while((n = rio_readnb(&rio, buf, MAXLINE)) != 0) //While it has read more than zero bytes { Rio_writen(fd, buf, n);//Write n bytes from buf } Close(fd); exit(0); }