|
|
在自己机子上(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);
} |
|