|
|
如标题。源程序如下:
#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);
} |
|