资源简介
可以拿到满分的网络代理,采用读者优先的方式避免竞争,避免了pipe broken造成的服务器中断。
代码片段和文件信息
#include
#include “csapp.h“
#include
//the const variable below are requested in the reference
static const char *user_agent_hdr = “User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.3) Gecko/20120305 Firefox/10.0.3\r\n“;
static const char *connect_hdr = “Connection: close\r\n“;
static const char *proxy_hdr = “Proxy-Connection: close\r\n“;
static const char *connection_key = “Connection“;
static const char *user_agent_key= “User-Agent“;
static const char *proxy_connection_key = “Proxy-Connection“;
static const char *host_key = “Host“;
//the micro below show some string type and cache size
#define EOF_TYPE 1
#define HOST_TYPE 2
#define OTHER_TYPE 3
#define MAX_CACHE_SIZE 1049000
#define MAX_object_SIZE 102400
#define CACHE_NUM 10
int getType(char *buf);
void doit(int connfd);
void parseUri(char uri[]char hostname[]char path[]char port[]);
void build_http
(char *server_httpchar *hostnamechar*pathchar* portrio_t *clientrio);
void* thread(void *vargp);
void initCache();
void PreRead(int index);
void afterRead(int index);
void preWrite(int index);
void afterWrite(int index);
int findCache(char *url);
void updateLRU(int index);
int findSuitCache();
void writeCacheContent(char *urlchar* buf);
//the struct of a cacheLine for a file
typedef struct
{
char content[MAX_object_SIZE];
char url[MAXLINE];
int time;
int isEmpty;
int readCount;
int writeCount;
sem_t mutex;
sem_t w;
}cacheunit;
//the cache struct consist of ten cacheLine
typedef struct
{
cacheunit cacheUnit[CACHE_NUM];
}Cache;
Cache cache;
//record the overall time to update the cacheline time
int allTime=0;
//the flag is used to void compete of “allTime“
sem_t flag;
/*
the main function to connect the client and it is same to
the code in the page 672 of textbook
*/
int main(int argcchar **argv)
{
int listenfd*connfd;
char hostname[MAXLINE]port[MAXLINE];
socklen_t clientlen;
struct sockaddr_storage clientaddr;
pthread_t tid;
Signal(SIGPIPE SIG_IGN);
Sem_init(&flag01);
// the cmd parameters fail
if (argc!=2){
fprintf(stderr “usage: %s \n“argv[0]);
exit(1);
}
initCache();
listenfd= open_listenfd(argv[1]);
while (1)
{
clientlen=sizeof(clientaddr);
connfd=malloc(sizeof(int));
*connfd=Accept(listenfd(SA*)&clientaddr&clientlen);
Getnameinfo((SA*)&clientaddrclientlenhostnameMAXLINE
portMAXLINE0);
printf(“Accepted connection from (%s %s)\n“hostnameport);
Pthread_create(&tidNULLthread(void*)connfd);
}
}
//create a new thread to run for servering the client
void *thread(void *vargo)
{
int connfd = *((int *)vargo);
Pthread_detach(pthread_self());
free(vargo);
doit(connfd);
Close(connfd);
return NULL;
}
//the main process to get the cmdparse the cmdfind the cache
//build the headerconnect to the server and return the content
//to th
相关资源
- labview2014模块和破解文件
- Labview2012全模块
- SFP光模块收发模块标准英文版.pdf
- 简单二阶互联系统的非线性动力学分
- Laboratory investigation of the effects of str
- Labwindows/CVI下Excel打开、读写操作
- labview process monitor控件
- 基于LabVIEW的并行数据采集系统设计
- 基于LabVIEW平台和网络的计算机远程在
- labview ASCII_十六进制_十进制转换
- labview大型应用程序开发流程与技巧
- labview获取子窗口句柄FindwindowsEX(AP
- EXCEL整体写入和读取labview 文件
- 基于LabVIEW的带式输送机托辊圆度误差
- 基于ZigBee无线传感器网络的井下环境
- MPPT算法.pdf
- 立象Argox Label Dr 200打印机驱动 官方版
- 非超对称杂散弦理论的Calabi-Yau压缩
- Facile hydrothermal synthesis of Tb2(MoO4)
- The HI catalytic decomposition in the lab-scal
- Labview生成dll文件的方法
- keithley2002 example
- labview语音识别程序
- 手写数字识别-模板匹配法
- Stock_Watson_动态因子分析模型
- labview人脸识别283682
- 果蝇优化算法优化支持向量回归程序
- 自己做的一个简单GUI扑克纸牌识别-
- multi output SVR
- 台湾林教授的支持向量机libsvm
评论
共有 条评论