歡迎您光臨本站 註冊首頁

用fork創建子進程 實現持久連接 問題。我在現在等。急

←手機掃碼閱讀     火星人 @ 2014-03-22 , reply:0

伺服器端的 全部代碼。連接時候總是顯示 等待。。。
是哪裡不對了。運行時沒有錯誤。但是就是打不開網頁。
是waitpid()的問題嗎?還是fork()用錯了?

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define LINESIZE 1024
#define MAXPENDING 20
#define MAXDIRLEN 256

void *service_thread(void *arg);
void do_HTTPservice(int clnt_sock);
void send_file(int clnt_sock,char *file_name);
void send_error400(int clnt_sock);
void send_error404(int clnt_sock);
void exit_error(char *error_message);
void zombie_collector(int cpid);
int read_line(int clnt_sock,char *line);

int *csock;
char base_dir[]="/home/class10/acyang/acyang/http/";

int main(int argc,char **argv)
{
int cpid;
int serv_sock;
int clnt_sock;

struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
struct sigaction sig_handler;

int caddr_len;
unsigned short serv_port;
pthread_t tid;

if(argc!=2){
printf("Usage: %s\n",argv[0]);
exit(0);
}

serv_port=atoi(argv[1]);

if((serv_sock=socket(AF_INET,SOCK_STREAM,0))<0)
exit_error("socket() failed");

memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(serv_port);

if(bind(serv_sock,(struct sockaddr *)&serv_addr,sizeof(serv_addr))==-1)
exit_error("bind() error");
if(listen(serv_sock,MAXPENDING)==-1)
exit_error("listen() error");

//sig_handler.sa_handler=zombie_collector;
sig_handler.sa_flags-SA_RESTART;


while(1){
caddr_len=sizeof(clnt_addr);
if((clnt_sock=accept(serv_sock,(struct sockaddr *)
&clnt_addr,&caddr_len))<0)
exit_error("accept() failed");
clnt_sock=accept(serv_sock,(struct sockaddr *)&clnt_addr,&caddr_len);

if((cpid=fork())<0)
exit_error("fork() failed");
if(cpid==0)
{
if((csock=(int *)malloc(sizeof(csock)))==NULL)
exit_error("malloc() error");

close(serv_sock);
*csock=clnt_sock;
if((pthread_create(&tid,NULL,service_thread,(void *)csock))!=0)
exit_error("pthread_create() failed");
exit(0);
}
close(clnt_sock);
}
return 0;
}
void zombie_collector(int cpid){
int zid;
while((zid=waitpid(cpid,NULL,0))>0);
if(zid==-1&&errno!=ECHILD)
exit_error("waitpid() failed");
}


void *service_thread(void *arg)
{
int csock;

pthread_detach(pthread_self());
csock=*(int *)arg;
free(arg);

do_HTTPservice(csock);
close(csock);
}

void do_HTTPservice(int clnt_sock)
{
int line_len;
char req_line[LINESIZE];
char method[5];
char file_name[128];
char path_name[MAXDIRLEN];

while(line_len=read_line(clnt_sock,req_line)){
if(strstr(req_line,"HTTP")){
strcpy(method,strtok(req_line," "));
strcpy(file_name,strtok(NULL," "));
}
}
if(strcmp(method,"GET")!=0){
send_error400(clnt_sock);
}
strcpy(path_name,base_dir);
if(file_name[0]=='/')
strcat(path_name,file_name+1);
else
strcat(path_name,file_name);

send_file(clnt_sock,path_name);
}


int read_line(int sock,char *line)
{
char buf[LINESIZE];
int len,pos;
char ch;

pos=0;

while(1){
ch='\0';
len=recv(sock,&ch,1,0);

if(len<0)
exit_error("recv() error in read_line()\n");

if(len==0){
strcpy(line,buf);
return pos;
}
buf[pos++]=ch;
if((pos>=1)&&(buf[pos-1]=='\n')){
if((pos>=2)&&(buf[pos-2]=='\r')){
buf[pos-2]='\0';
pos-=2;
break;
}
else{
buf[pos-1]='\0';
pos-=1;
break;
}
}
}
strcpy(line,buf);
return(pos);
}


void send_file(int clnt_sock,char *path_name)
{
FILE * fp;
char status[]="HTTP/1.1 200 OK\r\n";
char server[]="server: My HTTP Server \r\n";
char conn[]="Connection: Close \r\n";
char clength[]="Content-length:1024 \r\n";
char ctype[]="Content-type: text/html\r\n\r\n";
char buf[LINESIZE];
if((fp=fopen(path_name,"r"))==NULL){
send_error404(clnt_sock);
return;
}
send(clnt_sock,status,sizeof(status),0);
send(clnt_sock,server,sizeof(server),0);
send(clnt_sock,conn,sizeof(conn),0);
send(clnt_sock,clength,sizeof(clength),0);
send(clnt_sock,ctype,sizeof(ctype),0);

while(fgets(buf,LINESIZE,fp)!=NULL)
{
strtok(buf,"\n");
send(clnt_sock,buf,strlen(buf),0);
}
fclose(fp);
}


void send_error400(int clnt_sock)
{
char protocol[]="http/1.1 400 Bad request\r\n";
char server[]="server: My HTTP Server \r\n";
char conn[]="Connection: Close \r\n";
char clength[]="Content-length:1024 \r\n";
char ctype[]="Content-type: text/html\r\n\r\n";
char message[]=" 400:\
Bad Request Method!
";
send(clnt_sock,protocol,sizeof(protocol),0);
send(clnt_sock,server,sizeof(server),0);
send(clnt_sock,conn,sizeof(conn),0);
send(clnt_sock,clength,sizeof(clength),0);
send(clnt_sock,ctype,sizeof(ctype),0);
send(clnt_sock,message,sizeof(message),0);
}

void send_error404(int clnt_sock)
{
char protocol[]="http/1.1 400 Not Found\r\n";
char server[]="server: My HTTP Server \r\n";
char conn[]="Connection: Close \r\n";
char clength[]="Content-length:1024 \r\n";
char ctype[]="Content-type: text/html\r\n\r\n";
char message[]=" 404 FIle:\
Not found!
";

send(clnt_sock,protocol,sizeof(protocol),0);
send(clnt_sock,server,sizeof(server),0);
send(clnt_sock,conn,sizeof(conn),0);
send(clnt_sock,clength,sizeof(clength),0);
send(clnt_sock,ctype,sizeof(ctype),0);
send(clnt_sock,message,sizeof(message),0);
}
void exit_error(char *error_message)
{
perror(error_message);
exit(1);
}

[火星人 ] 用fork創建子進程 實現持久連接 問題。我在現在等。急已經有280次圍觀

http://coctec.com/docs/linux/show-post-103101.html