资源简介
ics的proxy lab,实现一个代理服务器,已经经过测试,能够通过,绝对正确。
代码片段和文件信息
/*
* proxy.c - CS:APP Web proxy
*
*
* IMPORTANT: Give a high level description of your code here. You
* must also provide a header comment at the beginning of each
* function that describes what that function does.
*/
#include “csapp.h“
struct arg
{
int fd;
struct sockaddr_in sock;
};
FILE *log_file;
sem_t sem_log;
sem_t sem_dns;
/*
* Function prototypes
*/
int parse_uri(char *uri char *target_addr char *path int *port);
void format_log_entry(char *logstring struct sockaddr_in *sockaddr char *uri int size);
void *proxy(void *);
void Rio_writen_w(int fd void *usrbuf size_t n);
ssize_t Rio_readlineb_w(rio_t *rp void *usrbuf size_t maxlen);
int Open_clientfd_w(char *hostname int port);
/*
* main - Main routine for the proxy program
*/
int main(int argc char **argv)
{
Signal(SIGPIPE SIG_IGN);
int port = atoi(argv[1]);
sem_init(&sem_log 0 1);
sem_init(&sem_dns 0 1);
log_file = fopen(“./proxy.log“ “a“);
int connfd;
int listenfd = Open_listenfd(port);
struct sockaddr_in clientaddr;
int clientlen;
pthread_t tid;
struct arg g;
while (1)
{
clientlen= sizeof(clientaddr);
struct arg *g = (struct arg *)malloc(sizeof(struct arg));
connfd = Accept(listenfd (SA *)(&(g->sock)) &clientlen);
g->fd = connfd;
Pthread_create(&tid NULL proxy (void *)g);
}
exit(0);
}
/*
* Thread
*/
void *proxy(void *ptr)
{
Pthread_detach(pthread_self());
struct arg *g = (struct arg *)ptr;
int connfd = g->fd;
struct sockaddr_in sock;
memcpy(&sock &(g->sock) sizeof(struct sockaddr_in));
free(g);
char buf[MAXLINE] method[MAXLINE] uri[MAXLINE] version[MAXLINE]
hostname[MAXLINE] pathname[MAXLINE] content[MAXLINE];
int port server_fd content_len;
rio_t rio_client rio_server;
Rio_readinitb(&rio_client connfd);
Rio_readlineb_w(&rio_client content MAXLINE);
sscanf(content “%s %s %s“ method uri version);
parse_uri(uri hostname pathname &port);
//send content to server
int retval = 0;
server_fd = Open_clientfd_w(hostname port);
if (server_fd==0) return;
Rio_readinitb(&rio_server server_fd);
Rio_writen_w(server_fd content strlen(content));
while (Rio_readlineb_w(&rio_client content MAXLINE)>2)
{
if (strstr(content “Proxy-Connection“))
strcpy(content “Proxy-Connection: close\r\n“);
else if (strstr(content “Connection“))
strcpy(content “Connection: close\r\n“);
Rio_writen_w(server_fd content strlen(content));
}
Rio_writen_w(server_fd “\r\n“ 2);
// receive content from server
int found = 0;
while (Rio_readlineb_w(&rio_server buf MAXLINE)>2) {
char *index = strstr(buf “Content-Length“);
if (index) {
found = 1;
content_len = atoi(index+16);
}
Rio_writen_w(connfd buf strlen(buf));
- 上一篇:PID控制超详细教程(含软硬件上位机,很好
- 下一篇:反应釜cad图纸
相关资源
- BCC(FCS)校验 labview PLC
- SSDA模板匹配算法
- LabVIEW 的交通灯 程序
- 基于LabVIEW的秒表设计
- labview2017破解版地址
- Agilent34401A串口程控程序Labview
- 基于Log算子的边缘检测
- labview十六进制转二进制
- LabVIEW在模拟电子中的应用
- 基于labview的电子时钟显示拼出的数码
- labview贪吃蛇
- v2v信道仿真与特性研究
- LABVIEW 调用摄像头
- VMD实现函数
- 基于LabView振动信号采集的最小vi程序
- process monitor.vi 进程监视器
- elasticsearch-6.4.3-全套ela+源码+ik分词器
- ICS lab10 WebProxy
- 基于LabVIEW的QAM调制仿真
- 文件加密与解密
- 利用labview制作的简单最小二乘法求解
- Labview TCP/IP实例132272
- OpenHaptics_API_Reference 完整API翻译
- 波形图的横轴时间显示
- 基于LabVIEW的一些小程序
- 采用labview 实现水箱的PID控制,界面美
- 小数据量法计算Lyapunov
- LABVIEW串口调试助手源码
- 用中值滤波和均值滤波去除高斯白噪
- labview 温湿度界面
评论
共有 条评论