#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<unistd.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<pthread.h>
#include<fcntl.h>
#define HOME_PATH "/home/wangpeng/mydir/http浏览器网络编程2018.04.03/v3_访问html网页"
int create_socket();
void send_200ok(int c,int size);
void* thread_fun(void*arg);
void send_404(int c);
void *thread_fun1(void*arg);
int main()
{
int sockfd=create_socket();
assert(sockfd!=-1);
while(1)
{
struct sockaddr_in caddr;
int len=sizeof(caddr);
int c=accept(sockfd,(struct sockaddr*)&caddr,&len);
if(c<0)
{
continue;
}
printf("accept c=%d\n",c);
pthread_t id;
pthread_create(&id,NULL,thread_fun,(void*)c);
}
}
int create_socket()
{
int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
return -1;
}
struct sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(80);//root
saddr.sin_addr.s_addr=inet_addr("192.168.174.128");
int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
if(res<0)
{
return -1;
}
listen(sockfd,5);
return sockfd;
}
void *thread_fun(void*arg)
{
int c=(int)arg;
char buff[512]={0};
int n=recv(c,buff,511,0);//接收http请求报文
if(n<=0)
{
pthread_exit(NULL);
}
printf("read: %s\n",buff);
//send(c,"hello",5,0);//发送http应答报文
//send_200ok(c,5);
//send(c,"hello",5,0);
char*ptr=NULL;
char*s=strtok_r(buff," ",&ptr);
if(s==NULL)
{
close(c);
pthread_exit(NULL);
}
printf("Method:%s\n",s);
s=strtok_r(NULL," ",&ptr);
if(s==NULL)
{
close(c);
pthread_exit(NULL);
}
if(strcmp(s,"/")==0)
{
s="/index.html";
char path[256]={HOME_PATH};
strcat(path,s);
printf("flie path:%s\n",path);
int fd=open(path,O_RDONLY);
if(fd==-1)
{
send_404(c);
close(c);
pthread_exit(NULL);
}
int size=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
//sendfile(c,fd,NULL,size);
//close(fd);
send_200ok(c,size);
char readbuff[256]={0};
int num=0;
while((num=read(fd,readbuff,256))>0)//读多少发多少
{
send(c,readbuff,num,0);
}
close(fd);
pthread_t id1;
pthread_create(&id1,NULL,thread_fun1,(void*)c);
pthread_join(id1,(void**)0);
}
//close(c);
}
void send_200ok(int c,int size)
{
char http_head[512]={"HTTP/1.1 200 ok\r\n"};
strcat(http_head,"Server:myhttp\r\n");
sprintf(http_head+strlen(http_head),"content-length:%d\r\n",size);
strcat(http_head,"\r\n");//空行
printf("send:\n%s\n",http_head);
send(c,http_head,strlen(http_head),0);
}
void send_404(int c)
{
char http_head[512]={"HTTP/1.1 404 Not Found\r\n"};
strcat(http_head,"Server:myhttp\r\n");
strcat(http_head,"Conten-length:10\r\n");
strcat(http_head,"\r\n");
strcat(http_head,"Not Found!");
printf("send:\n%s\n",http_head);
send(c,http_head,strlen(http_head),0);
}
void *thread_fun1(void*arg)
{
int c=(int)arg;
char buff[512]={0};
int n=recv(c,buff,511,0);//接收http请求报文
if(n<=0)
{
pthread_exit(NULL);
}
printf("pthread1 read:%s\n",buff);
char*s="/a.jpg";
char path[256]={HOME_PATH};
strcat(path,s);
printf("图片flie path:%s\n",path);
int fd=open(path,O_RDONLY);
if(fd==-1)
{
send_404(c);
close(c);
pthread_exit(NULL);
}
int size=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
send_200ok(c,size);
char readbuff[256]={0};
int num=0;
while((num=read(fd,readbuff,256))>0)//读多少发多少
{
send(c,readbuff,num,0);
}
close(fd);
close(c);
}
评论0