本文整理汇总了C++中Rio_readinitb函数的典型用法代码示例。如果您正苦于以下问题:C++ Rio_readinitb函数的具体用法?C++ Rio_readinitb怎么用?C++ Rio_readinitb使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Rio_readinitb函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: doit
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);
}
}
示例2: doit
/*
* 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 */
}
示例3: check_clients
void check_clients(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_readinitb(&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->clienfd[i] = -1;
}
}
}
}
示例4: keyboard
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;
}
示例5: main
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);
printf("type:"); fflush(stdout);
while (Fgets(buf, MAXLINE, stdin) != NULL) {
Rio_writen(clientfd, buf, strlen(buf));
Rio_readlineb(&rio, buf, MAXLINE);
printf("echo:");
Fputs(buf, stdout);
printf("type:"); fflush(stdout);
}
Close(clientfd);
exit(0);
}
示例6: process_request
/* Read a request from a connection socket and parses its information into a
* req_t struct.
* Initally based on csapp echo() p.911
* Allocates memory for request hdrs
* returns -1 on failure, 1 if the content is local, and 0 if the content is
* remote
*/
int process_request(int fd, req_t* req){
size_t n;
char buf[MAXLINE];
rio_t rio;
req->domain = NULL;
req->path = NULL;
req->hdrs = NULL;
//Parse domain and path information
Rio_readinitb(&rio, fd);
if((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
if(parse_req(buf, req) == -1)
return -1;
}
else return -1;
//parse header information
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
if(strcmp(buf, "\r\n") == 0)
break;
if(req->hdrs != NULL){
n = strlen(req->hdrs) + strlen(buf) + 1;
req->hdrs = Realloc(req->hdrs,strlen(req->hdrs)+ n);
strcat(req->hdrs, handle_hdr(buf));
} else {
req->hdrs = Malloc(n+1);
strcpy(req->hdrs, handle_hdr(buf));
}
}
return 0;
}
示例7: receive_message
void receive_message(){
char buf[MAXLINE];
char header[MAXLINE];
char buf1[MAXLINE];
char buf2[MAXLINE];
char buf3[MAXLINE];
rio_t client;
Rio_readinitb(&client, clientfd);
while(1) {
//rio_readp(clientfd,buf,MAXLINE);
Rio_readlineb(&client,buf,MAXLINE);
if (strcmp(buf,"error\r\n")==0){
printf("The file you want to download doesn't exist!\n");
close(clientfd);
exit(1);
}
if (strcmp(buf, "finish\r\n") == 0){
break;
}
sscanf(buf, "%s %s %s %s", header, buf1, buf2, buf3);
if (strcmp(header, "total") == 0){
total_num = atoi(buf1);
user_list = malloc(sizeof(user_list)*total_num);
}
if (strcmp(header,"user")==0){
int i = atoi(buf1);
user_list[i-1].IP = strdup(buf2);
user_list[i-1].port = atoi(buf3);
}
//printf("buf is %s\n",buf);
memset(buf, 0, strlen(buf));
}
close(clientfd);
}
示例8: response_read
Response response_read(int fd) {
rio_t rio;
char buf[MAXLINE];
Rio_readinitb(&rio, fd);
ssize_t r = Rio_readlineb(&rio, buf, MAXLINE);
if (r == 0) {
printf("Received empty response\n");
return NULL;
}
Response response = malloc(sizeof(struct Response));
sscanf(buf, "%s %d %s", response->version, &response->status_code, response->status_name);
header_read(&rio, buf, &response->header);
HeaderEntry* content_length_entry = header_find_entry(&response->header, "Content-Length");
int content_length = content_length_entry == NULL
? 0
: atoi(content_length_entry->value);
response->content_length = content_length;
response->content = malloc(response->content_length * sizeof(char));
char *content = response->content;
while (content_length > 0) {
r = Rio_readlineb(&rio, content, (size_t) content_length);
if (r > 0) {
content += r;
content_length -= r;
}
}
return response;
}
示例9: clientPrint
/*
* Read the HTTP response and print it out
*/
void clientPrint(int fd)
{
rio_t rio;
char buf[MAXBUF];
int length = 0;
int n;
Rio_readinitb(&rio, fd);
/* Read and display the HTTP Header */
n = Rio_readlineb(&rio, buf, MAXBUF);
while (strcmp(buf, "\r\n") && (n > 0)) {
printf("Header: %s", buf);
n = Rio_readlineb(&rio, buf, MAXBUF);
/* If you want to look for certain HTTP tags... */
if (sscanf(buf, "Content-Length: %d ", &length) == 1) {
printf("Length = %d\n", length);
}
}
/* Read and display the HTTP Body */
n = Rio_readlineb(&rio, buf, MAXBUF);
while (n > 0) {
printf("%s", buf);
n = Rio_readlineb(&rio, buf, MAXBUF);
}
}
示例10: main
int main(int argc, char** argv) {
int clientfd;
char* hostname = argv[1];
char* filename = argv[2];
int port = atoi(argv[3]);
char data[60];
strcpy(data, "GET /");
strcat(data, filename);
strcat(data, " HTTP/1.1\r\nHost: ");
strcat(data, hostname);
strcat(data, "\r\n\r\n");
printf("Constructed Data: %s\n", data);
char buffer[MAXLINE]; // Using MAXLINE from csapp
bzero(buffer, MAXLINE);
buffer[0] = 'x';
rio_t rio;
clientfd = Open_clientfd(hostname, port);
Rio_readinitb(&rio, clientfd);
rio_writen(clientfd, data, strlen(data));
while (strlen(buffer)>0) {
Rio_readlineb (&rio, buffer, MAXLINE);
printf("%s\n", buffer);
}
close(clientfd);
exit(0);
}
示例11: getInfo
void getInfo(int fd, rio_t client, int id){
char buf1[MAXLINE];
int jobID=id;
int numBytes;
char * saveptr=NULL;
Rio_readinitb( &client,fd);
while(true){
numBytes = Rio_readlineb(&client, buf1, MAXLINE);
id1 = strtok_r(buf1, " \r\n",&saveptr);
int fileStartID=atoi(id1);
id2 = strtok_r(buf1, " \r\n",&saveptr);
int fileEndID=atoi(id2);
numBytes = Rio_readlineb(c, buf, MAXLINE);
if(numBytes<0){
Close(fd);
return ans;
}
buf[numBytes]=0;
seperate(buf, &ans.pre);
fd=Open_clientfd(ipAddress, atoi(port));
}
示例12: main
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);
}
示例13: main
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;
}
示例14: getSize
int getSize(int fd) {
//stats.req_arrival = id.req_arrival;
int is_static;
struct stat sbuf;
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
char filename[MAXLINE], cgiargs[MAXLINE];
rio_t rio;
//stats.size = -1;
Rio_readinitb(&rio, fd);
Rio_readlineb(&rio, buf, MAXLINE);
sscanf(buf, "%s %s %s", method, uri, version);
//close(fd);
printf("%s %s %s\n", method, uri, version);
if (strcasecmp(method, "GET")) {
requestError(fd, method, "501", "Not Implemented",
"CS537 Server does not implement this method");
return 0;
}
requestReadhdrs(&rio);
is_static = requestParseURI(uri, filename, cgiargs);
if (stat(filename, &sbuf) < 0) {
requestError(fd, filename, "404", "Not found", "CS537 Server could not find this file");
return 0;
}
return sbuf.st_size;
}
示例15: echoClient
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);
}