本设计设计的web服务器,根据服务机上指定的网页为默认网页,存储于本地,从浏览器中输入WEB服务器的地址,服务器响应,并发送默认网页给浏览器,由于默认网页中包含图片的内容,当浏览器读到该照片内容时,再向服务器发送照片请求,服务器响应请求,把浏览器所需要的内容打开,发送给浏览器。
本设计牵涉到 http协议,需根据协议信息头,分辨浏览器发送的请求内容,再做答复。答复中,也需要遵守http协议,先发送协议头,以便浏览器辨认识别。本文中采用了sscanf截取浏览器发送的http协议头中的文件信息。
-
#include <sys/types.h>
-
#include <sys/socket.h> // 包含套接字函数库
-
#include <stdio.h>
-
#include <netinet/in.h> // 包含AF_INET相关结构
-
#include <arpa/inet.h> // 包含AF_INET相关操作的函数
-
#include <unistd.h>
-
#include<string.h>
-
#include<stdlib.h>
-
#include<fcntl.h>
-
#include<sys/stat.h>
-
#include<dirent.h>
-
-
-
#define PORT 80
-
#define FILENAME "my.html"
-
#define SIZE 100
-
-
int main()
-
{
-
-
-
-
int server_sockfd,client_sockfd;
-
int server_len,client_len;
-
-
struct sockaddr_in server_sockaddr,client_sockaddr;
-
-
server_sockfd = socket(AF_INET,SOCK_STREAM, 0); // 定义套接字类型
-
-
server_sockaddr.sin_family=AF_INET;
-
server_sockaddr.sin_port=htons(PORT);
-
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
-
-
server_len=sizeof(server_sockaddr);
-
-
//允许重复使用本地地址和套接字绑定
-
int j=1;
-
setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&j,sizeof(j));
-
-
//绑定端口
-
if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,server_len)==-1)
-
{
-
perror("bind:");
-
exit(1);
-
}
-
-
//监听端口
-
if(listen(server_sockfd,5)==-1)
-
{
-
perror("listen:");
-
exit(1);
-
}
-
printf("Listening...\n");
-
-
-
unsigned char data_buf[2048];
-
int i;
-
-
struct stat statbuf;
-
-
-
while(1)
-
{
-
if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_sockaddr,&client_len))==-1)
-
{
-
perror("accept error:");
-
exit(1);
-
}
-
-
printf("%s linked\n",inet_ntoa(client_sockaddr.sin_addr));
-
-
// printf("Send Msg...\n");
-
-
-
bzero(data_buf,sizeof(data_buf));
-
i=recv(client_sockfd,data_buf,sizeof(data_buf),0);
-
-
printf("*********(%d)bytes recv******\n",i);
-
printf("data_buf=%s\n",data_buf);
-
printf("+++++++++++++++++++++++++++++\n");
-
-
if(0==strncmp(data_buf,"GET / HTTP",10)) //发送网页文件
-
{
-
FILE *fp;
-
fp=fopen("my.html","r");
-
lstat("my.html",&statbuf);
-
-
bzero(data_buf,sizeof(data_buf));
-
sprintf(data_buf,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %ld\r\n\r\n",statbuf.st_size);
-
-
int file_length;
-
do
-
{
-
file_length=fread(data_buf,sizeof(char),2048,fp);
-
-
i=send(client_sockfd,data_buf,strlen(data_buf),0);
-
// printf("send html bytes:i=%d\n",i);
-
bzero(data_buf,sizeof(data_buf));
-
}while(file_length>0);
-
-
-
fclose(fp);
-
-
-
}
-
-
else //根据浏览器的请求发送文件
-
{
-
char fileinfo[SIZE]; //获取请求的文件路径和文件名
-
bzero(fileinfo,100);
-
char buff[SIZE];
-
bzero(buff,sizeof(buff));
-
-
sscanf(data_buf,"%*[^/]/%[^ ]",buff);
-
printf("file name buff=%s\n",buff);
-
-
char a[2]={'/'};
-
//fileinfo[SIZE]={'/'};
-
strcat(fileinfo,a);
-
strcat(fileinfo,buff);
-
-
printf("fileinfo=%s\n",fileinfo);
-
-
//if(0==strncmp(data_buf,"GET /home/",10)) //发送图片
-
//{
-
printf("Send jpg...\n");
-
FILE *fp;
-
fp=fopen(fileinfo,"r");
-
if(fp==NULL)
-
{
-
perror("fopen failed:");
-
}
-
lstat(fileinfo,&statbuf); //获取文件属性
-
-
bzero(data_buf,sizeof(data_buf));
-
sprintf(data_buf,"HTTP/1.1 200 OK\r\nContent-Type: image/jpg\r\nContent-Length: %ld\r\n\r\n",statbuf.st_size);
-
-
printf("statbuf.st_size=%ld\n",statbuf.st_size);
-
-
-
int file_block_length;
-
-
do
-
{
-
file_block_length=fread(data_buf,1,2048,fp);
-
printf("file_block_length=%d\n",file_block_length);
-
-
-
// printf("data_buf=%s\n",data_buf);
-
i=send(client_sockfd,data_buf,2048,0);
-
-
printf("send byte i=%d\n",i);
-
}while(file_block_length>0);
-
-
printf("send over \n");
-
fclose(fp);
-
-
}
-
-
close(client_sockfd);
-
}
-
-
}
阅读(824) | 评论(0) | 转发(2) |