LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 940|回复: 2

求助:web服务器能在Linux上浏览而不能在windows上浏览(无论用IE还是FireFox),为什么?

[复制链接]
发表于 2006-4-3 10:31:47 | 显示全部楼层 |阅读模式
如标题。源程序如下:
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#define debug


#define addr_ip INADDR_ANY
#define backlog 20
#define num 2

extern int errno;

char s10[]="<html><head><title>My works</title></head><body><FORM ACTION=http://192.168.100.119 MATHOD=GET><p>Water level:<INPUT TYPE=TEXT NAME=level SIZE=30><p> temperature:<INPUT TYPE=TEXT NAME=temperature SIZE=30><p><INPUT TYPE=submit><INPUT TYPE=reset></FORM></body></html>\n";

char s20[]="<html><head><title>My works</title></head><body>resive message,",s21[]="</body></html>\n";

int lth1=sizeof(s10)+60,lth2=sizeof(s20)+sizeof(s21)+20+40*num;

/*error*/
report_error(char *s)                    
{
printf("receiver:error in %s,errno=%d\n",s,errno);
perror("error type");
exit(1);
}
writehead(FILE *f1,int lth)      
{
fprintf(f1,"HTTP/1.0 200 OK\n");
fprintf(f1,"Content-type:text/html\n");
fprintf(f1,"Date:Sun,01-Mar-2006 12:00:00 GMT\n");
fprintf(f1,"Server:ipserver-http 1.1\n");
fprintf(f1,"Expires:Sun,01-Oct-2006 12:00:00 GMT\n");
fprintf(f1,"Context-Length:%d\n",lth);
#ifdef debug
printf("flength=%d\n\n",lth);
#endif
fprintf(f1,"\n");
}


/*handle message and send*/
handlemsg(int fd)         
{
FILE *f;
char buf[160]="",num1[30]="",num2[30]="";
int i=0,j=0,k=0,flag1[2]={0},flag2[2]={0},flag=0;

/*open the newfd to resive message*/
f=fdopen(fd,"r+");      
if(!f)
{
  close(fd);
  report_error("fdopen");
}
printf("fdopen success...\n");
setbuf(f,0);

/*read message*/
if(!fgets(buf,150,f))        
{
  fclose(f);
  close(fd);
  report_error("fgets");
}
printf("fgets success...\n");
#ifdef debug
printf("buf=%s\n",buf);
#endif
do     
{
if(buf=='?')flag=1;  
if((flag==1)&&(buf =='=')==1)
flag1[j++]=i;
if((flag==1)&&(buf =='&')==1)
flag2[k++]=i;
if((flag==1)&&(buf ==' ')==1)
flag2[k++]=i;
i++;
}while(buf!='\n');
#ifdef debug
printf("flag=%d\n",flag);
#endif

/*no message*/
if(flag==0)           
{        
writehead(f,lth1);
fprintf(f,s10);
fflush(f);
fclose(f);
}

/*resive message*/
else      
{
j=0;
for(i=flag1[0]+1;i<flag2[0];i++)
num1[j++]=buf;
j=0;
for(i=flag1[1]+1;i<flag2[1];i++)
num2[j++]=buf;
writehead(f,lth2);
fprintf(f,s20);
fprintf(f,"level=%s,temperature=%s",num1,num2);
fprintf(f,s21);
fflush(f);
fclose(f);
}
}

main()
{
pid_t pid;
int sockfd,newfd;
struct sockaddr_in my_addr,their_addr;
struct hostent *hp;
int sin_size;
volatile int true=1;

/*socket*/
if((sockfd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
report_error("socket");
printf("socket success...\n");

/*socket set*/
if((setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(void *)&true,sizeof(true)))==-1)

report_error("socket set");

printf("socket set success...\n");

/*bind*/
my_addr.sin_family=AF_INET;
#ifdef port
my_addr.sin_port=htons(port);
#else
my_addr.sin_port=htons(IPPORT_USERRESERVED);
#endif
my_addr.sin_addr.s_addr=htonl(addr_ip);
printf("port=%d\n",my_addr.sin_port);
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(my_addr))==-1)
report_error("bind...\n");
printf("bind success...\n");

/*listen*/
if(listen(sockfd,backlog)==-1)
report_error("listen");
printf("listen success...\n");
while(1)
{
sin_size=sizeof(struct sockaddr_in);
printf("server is run...\n");

/*new cling*/
newfd=accept(sockfd,(struct sockaddr *)&their_addr,&sin_size);
if(newfd<0&&errno==EINTR)
continue;

/*a signal might interrupt our accept() call*/
else if(newfd<0)

/*something quite amiss -- kill the server*/
report_error("accept");
printf("accept success...\n");

/*vfork*/
if ((pid=vfork())<0)
report_error("vfork");
printf("vfork success...\n");

/*child*/
if (pid==0)
{
close(sockfd); /*close listen socket*/
#ifdef debug
printf("their_ip=%s\n",inet_ntoa(their_addr.sin_addr));
#endif
handlemsg(newfd);

/*close client socket*/
close(newfd);
exit(0);
}

/*father*/
else close(newfd); /*father close client socket*/
}
close(sockfd);
exit(0);
}
发表于 2006-4-3 16:35:26 | 显示全部楼层
you misuse the following code:

  1. /*read message*/
  2. if(!fgets(buf,150,f))
  3. {
  4.     fclose(f);
  5.     close(fd);
  6.     report_error("fgets");
  7. }
复制代码

fgets will return when it encounters '\n', so you have not get all the client's request string when you send the reply, what will happen then? when client's following lines of a request arrived, the server has closed the socket, then the client will think she(he) cannot find the page, to fix it, use "recv" function to get from network, just as follows:

  1. /*read message*/
  2. if(recv(f,buf,150,0)<=0)
  3. {
  4.     fclose(f);
  5.     close(fd);
  6.     report_error("fgets");
  7. }
复制代码


another problem is 150 is too small for the buffer, plz make it larger
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-4-5 11:45:28 | 显示全部楼层
Thanks,i will try it according to you said.
Thank you very much
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表