LinuxSir.cn,穿越时空的Linuxsir!

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

求助:web服务器在本机上能用浏览造访,但不能在别的机子上收到数据,为什么?

[复制链接]
发表于 2006-3-20 17:36:56 | 显示全部楼层 |阅读模式
在自己机子上(Linux系统)用浏览器造访时与网页一起出来的还有一个警告框内容如下:
The document contains no data

在别的机子上(XP系统)用浏览器造访时,服务器反映正常(所设打印正常),但就是”无法显示网页“


请诸位大大解惑一二,在此多谢了。


服务器源程序:
#include <sys/types.h>

#include <sys/socket.h>

#include <sys/stat.h>

#include <netinet/in.h>

#include <string.h>

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <netdb.h>

#include <arpa/inet.h>

#include <fcntl.h>

#include <dirent.h>

#include <ctype.h>



#define port 80

#define addr_ip INADDR_ANY

#define backlog 20

#define name "lineminwangne.com"

#define debug


extern int errno;



/*error*/

report_error(char *s)

{

printf("receiver:error in %s,errno=%d\n",s,errno);

perror("error type");

exit(1);

}

handlemsg(int fd)        /*handle message and send*/
{
FILE *f;
char buf[125]="",num1[10]="",num2[10]="";
int i=0,j=0,k=0,flag1[2]={0},flag2[2]={0},flag=0;
f=fdopen(fd,"r+");      /*open the newfd to resive message*/
if(!f)
{
  close(fd);
  report_error("fdopen");
}
printf("fdopen success...\n");
setbuf(f,0);
if(!fgets(buf,115,f))        /*read message*/
{
  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
if(flag==0)           /*no message*/
{        
fprintf(f,"HTTP/1.0 200 OK\n");
fprintf(f,"Data:Sun,01-Oct-2005 12:00:00 GMT\n");
fprintf(f,"Content-type:text/html\n");
fprintf(f,"Expires:0\n");
fprintf(f,"\n");
fprintf(f,"<html><head><title>My works</title></head><body><FORM ACTION=http://210.40.41.1 MATHOD=GET><p>Water level:<INPUT TYPE=TEXT NAME=level SIZE=10><p> temperature:<INPUT TYPE=TEXT NAME=temperature SIZE=10><p><INPUT TYPE=submit><INPUT TYPE=reset></FORM></body></html>\n");
fflush(f);
fclose(f);
}
else       /*resive message*/
{
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;
fprintf(f,"HTTP/1.0 200 OK\n");
fprintf(f,"Data:Sun,01-Oct-2005 12:00:00 GMT\n");
fprintf(f,"Content-type:text/html\n");
fprintf(f,"Expires:0\n");
fprintf(f,"\n");
fprintf(f,"<html><head><title>My works</title></head><body>resive message,num1=%s,num2=%s</body></html>\n",num1,num2);
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");

/*fork*/

if ((pid=fork())<0)

report_error("fork");

printf("fork 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(newfd); /*close client socket*/

exit(0);

}

/*father*/

else close(newfd); /*father close client socket*/

}

close(sockfd);

exit(0);

}
发表于 2006-3-22 10:40:29 | 显示全部楼层
当把数据传送给客户的时候,改一下:

  1. fprintf(f,"HTTP/1.0 200 OK\n"
  2. "Server: Resin/2.1.14\n"
  3. "Content-type: text/html\n"
  4. "Expires:0\n"
  5. "Date: Wed, 22 Mar 2006 02:24:45 GMT\n"
  6. "\n"
  7. "<html><head><title>My works</title></head><body><FORM ACTION=http://210.40.41.1 MATHOD=GET><p>Water level:<INPUT TYPE=TEXT NAME=level SIZE=10><p> temperature:<INPUT TYPE=TEXT NAME=temperature SIZE=10><p><INPUT TYPE=submit><INPUT TYPE=reset></FORM></body></html>\n");
复制代码

这些东西应该是一次性送出去的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-22 11:46:25 | 显示全部楼层

5555555555斑竹大大,我照你的意见改了之后还是不行啊

错误现象:
别的机子在其浏览器上输入210.40.41.1时服务器(本机)显示:

accept success...
Detaching after fork from child process 3555.
fork success...
server is run...
fork success...
their_ip=210.40.41.2
fdopen success...
fgets success...
buf=GET / HTTP/1.1
flag=0

但在那个机子上的浏览器上则无法找到网页


自我浏览时弹出的警告:
The document contains no data.


源程序:见附件

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2006-3-22 12:12:15 | 显示全部楼层
那就奇怪了,我把你的 works1.txt 编译了一下,跑在一台fc3机器上,然后在另一台fc3和win机器上作了连接,都没有问题啊。 那你把 "\n"  都换成 "\r\n" 试试呢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-22 12:36:42 | 显示全部楼层
斑竹大大,我又去调试了,还是不行,是不是要设置、修改什么东东呢?
我的linux的网卡:静态210.40.41.1/255.255.255.0
主机地址:210.40.41.1
对了,我用的是红帽linux的X窗口,这是不是有影响呢?
回复 支持 反对

使用道具 举报

发表于 2006-3-22 13:21:42 | 显示全部楼层
难道是rpwt?
先指出几个可能的错误吧:
1. Date 写成了 Data
2. 我不知道 Context-Length 头是不是必须,但是从我抓的www服务器的包的情况看,都有这个字段
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-22 13:57:33 | 显示全部楼层
Post by rickxbx
难道是rpwt?
先指出几个可能的错误吧:
1. Date 写成了 Data
2. 我不知道 Context-Length 头是不是必须,但是从我抓的www服务器的包的情况看,都有这个字段



斑竹大大:
    首先,rpwt是什么意思?我只是个菜鸟,连入门都谈不上,麻烦解释一下;
    其次,我那个Data没有写错,就是数据的意思,不知道你在机子上运行的时候显现的是什么,怎么你运行好使而我就不行?难不成是破程序认人?
    最后,我这个动态网页长度随时要变都不知道怎么写长度,而且我这里有一例(书:《嵌入式linux的设计与应用》)就没有写Context-Length 头。

    我都晕了,老板下个星期就要检查这玩意弄到arm7开发板上的效果,我现在连pc机上的都没有搞定。哭555555555555
回复 支持 反对

使用道具 举报

发表于 2006-3-24 14:09:29 | 显示全部楼层
rpwt 即 人品问题

Data 怎么会没错呢? 明明是Date啊
你把http的rfc找出来看看吧,看看有什么不妥的地方,可能有些地方你没注意到,但是是有些浏览器必须的,在win下,用firefox和ie得到的结果就可能不一样

因为论文压力比较大,我不可能给你慢慢的去看rfc了,sorry
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-24 22:57:50 | 显示全部楼层
多谢斑竹大大指点,去看了http的rfc后才知道Context-Length 头至少在http1.1上是必须的。
而且我程序中的date确实错了。
我明白了,这就去调试。在此感激不尽,让你费心了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-25 14:33:14 | 显示全部楼层

555555555555还是不行

别的机子在其ie浏览器上输入210.40.41.1时服务器(本机)显示:

accept success...
Detaching after fork from child process 3555.
fork success...
server is run...
fork success...
their_ip=210.40.41.2
fdopen success...
fgets success...
buf=GET / HTTP/1.1

flag=0


但在那个机子上的浏览器上则无法找到网页


自我浏览(用的是firefox)时弹出的警告:
The document contains no data.

为什么就不能用ie浏览器呢?


源代码经过修改以后如附件

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

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

本版积分规则

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