- 论坛徽章:
- 0
|
3.下面借楼主的话题求教一个问题,也是模拟一个WEB服务器,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // bzero
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int get_index_name(char *name, char *file)
{
struct stat buf;
mode_t isdir = 16895;
FILE *fp;
char a;
char name_dir[256] = "\0";
char name_temp[256] = "\0";
char index_path[256] = "\0";
int i=0,j=0,k=0;
char defult[4][64] = {{"index.html"},{"index.htm"},{"default.htm"},{"default.html"}};
while((name[i] != '\n') && (name[i] != '\r')){
i++;
j++;
}//read the first line
for (i=0; i<j; i++)
{
if(*(name+i) == '/')
{
a=*(name+i+1);
if((int)(a) == 32)
{
for (j=0; j<4; j++)
{
//strcpy(index_path, "/mnt/hgfs/share/http/" ;
strcpy(index_path, "./" ;
strcat(index_path, &defult[j-1][64]);
if ((fp = fopen(index_path, "r" ) != NULL)
{
// strcpy(file, &defult[j-1][64]);
strcpy(name, index_path);
return 0;
}
}
return -1;
}
else
break;
}
}
for (k=0; k<(j-i); k++){
if (name[k+i+1] != ' ')
{
name_temp[k] = name[k+i+1];
name_temp[k+1] = '\0';
}
else
{
//strcpy(name_dir, "/mnt/hgfs/share/http/" ;
strcpy(name_dir, "./" ;
strcat(name_dir, name_temp);
strcpy(file, name_temp);
// printf("file=%s\n",file);
stat(name_dir, &buf);
if (buf.st_mode == isdir)
{
if (name_temp[strlen(name_temp)-1] == '/'){
for (j=0; j<4; j++)
{
strcpy(index_path, name_dir);
strcat(index_path, &defult[j-1][64]);
if ((fp = fopen(index_path, "r" ) != NULL)
{
strcpy(name, index_path);
return 0;
}
}
return -1;
}
else
return -301;
}
// strcpy(file, name_temp);
//strcpy(name, "/mnt/hgfs/share/http/" ;
strcpy(name, "./" ;
strcat(name, name_temp);
return 0;
}
}
return -1;
}
int main()
{
int fd;
int strLen;
char recvbuf[1024] = "\0";
char sendbuf[51200] = "\0";
int sockfd;
int ret;
struct sockaddr_in servAddr;
unsigned short port = 8000;
char index_name[1024] = "\0";
char ok_info[] = "HTTP/1.1 200 OK\r\nConnection: Keep-alive\r\n\r\n";
// char err_info[] = "HTTP/1.1 404 Not Found\r\n\r\n<HTML><BODY>File Not Found!</BODY></HTML>";
char move_info[256] = "HTTP/1.1 301 Moved Permanently\r\nLocation:";
char file[256] = "\0";
char cliIP[500];
struct sockaddr_in cliAddr;
int cliAddrLen = sizeof(cliAddr);
int connfd;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if ( sockfd<0 )
{
perror("socket err!" ;
return -1;
}
bzero(&servAddr, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(port);
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockfd, (struct sockaddr*)&servAddr, sizeof(struct sockaddr)) != 0)
{
close(sockfd);
perror("binding err!" ;
exit(1);
}
if(listen(sockfd, 1) != 0)
{
close(sockfd);
perror("listen err!");
exit(1);
}
printf("waiting client...\n");
//while (1){
//memset(&cliaddr,0,sizeof(struct sockaddr));
//len=sizeof(struct sockaddr_in);
//if( ( child_socket=accept(sock,(struct sockaddr *)&cliaddr,&len) ) < 0 )
connfd = accept(sockfd, (struct sockaddr*)&cliAddr, &cliAddrLen);
if(connfd < 0)
{
close(sockfd);
perror("accept err!");
exit(1);
}
while (1){
inet_ntop(AF_INET, &cliAddr.sin_addr.s_addr, cliIP, sizeof(cliAddr));
printf("client ip = %s\n", cliIP);
bzero(recvbuf, 1024);
//read(connfd, recvbuf, 204 ;
if(read(connfd, recvbuf, 204 < 0)
{
close(sockfd);
perror("read err!");
exit(1);
}
printf("recvbuf:%s\n",recvbuf);
if (strncmp(recvbuf, "GET ", 4) < 0){
strcpy(recvbuf,"<HTML><BODY>Unknow Type!<BODY><HTML>");
write(connfd, recvbuf, strlen(recvbuf));
close(connfd);
continue;
}
strcpy(index_name, recvbuf);
if ((ret = get_index_name(index_name, file)) < 0)
{
if (ret == -301){
strcat(move_info, file);
strcat(move_info, "/\r\n\r\n");
printf("%s\n",move_info);
write(connfd, move_info, strlen(move_info));
strcpy(recvbuf,"<HTML><BODY>Not Found! 301 ERROR! <br /><br /> lease check your address!</BODY></HTML>");
write(connfd, recvbuf, strlen(recvbuf));
close(connfd);
continue;
}
strcpy(recvbuf,"<HTML><BODY>Not Found! 404 ERROR! <br /><br /> lease check your address!</BODY></HTML>");
write(connfd, recvbuf, strlen(recvbuf));
close(connfd);
continue;
}
printf("文件名:%s\n",index_name);
if ((fd = open(index_name,O_RDONLY)) < 0){
strcpy(recvbuf,"<HTML><BODY>Not Found! 404 ERROR!<br /><br /> lease check your address!</BODY></HTML>");
write(connfd, recvbuf, strlen(recvbuf));
close(connfd);
continue;
}
printf("===write===\n");
write(connfd, ok_info, strlen(ok_info));
printf("ok_info:%s\n",ok_info);
while((strLen = read(fd, sendbuf, 51200)) > 0){
write(connfd, sendbuf, strLen);
bzero(sendbuf, strLen);
printf("-------------------------------------\n");
}
//close(connfd);
}
close(connfd);
return 0;
}
本人找这段代码是为了测试长连接,用IE,但每到第二次请求都会出错退出,在read时出现"connection reset by peer".
如果将accept那段和关闭close(connfd);包含在while循环之内一切正常,可这时就成了短连接。
各位大侠,能不能帮忙解惑?方便的话QQ:448750215,或EMAIL:jzduan@126.com |
|