- 论坛徽章:
- 0
|
sevrer的源代码
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <ctype.h>
#include <signal.h>
#include <netdb.h>
#define backlog 10
#define myport 8000
#define maxsize 100
#define Maxfile 65535
void dameon(int socket_fd)
{
int pc,fd;//demeon process ID
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
pc=fork();//first step
if(pc<0)
{
printf("error fork\n" ;
exit(1);
}
else if(pc>0) exit(0);
openlog("simpleserv",LOG_PERROR,LOG_DAEMON);
setsid();//second step
if(pc=fork())
exit(0);
else if (pc<0)exit(1);
chdir("/" ;//third step
umask(0);//fourth step
/*for(fd=0;fd<Maxfile;fd++)
if(fd!=socket_fd)
close(fd);//fifth step*/
//signal(SIGCLD,(sigfunc *)reap_status);
signal(SIGCHLD,SIG_IGN);
}
void reap_status()
{
int pid;
union wait status;
while((pid=wait3(&status,WNOHANG,NULL))>0);
}
int main()
{
int sock_fd,new_fd,fd,nd;
/*listen on sock*/
// char *buf="test";
struct sockaddr_in my_addr;
/*information of server address*/
struct sockaddr_in their_addr;
/*information of client address*/
int sin_size,len;
int sen;/*data which will be sent to the net*/
// len=strlen(buf);
dameon(sock_fd);
if((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
{perror("socket" ;exit(1);}
my_addr.sin_family=AF_INET;//fill the struct of server
my_addr.sin_port=htons(myport);//fill the struct of server with intnet byte order
my_addr.sin_addr.s_addr=INADDR_ANY;/*fill the s_addr with the IP of machine which runs the client*/
bzero(&(my_addr.sin_zero), ;
if(bind(sock_fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{perror("bind" ;exit(1);}
if(listen(sock_fd,backlog)==-1)
{perror("listen" ;exit(1);}
while(1)
{
/*if((fd=open("/home/test/application/123.log",O_CREAT|O_WRONLY|O_APPEND,0600))<0)
{perror("open" ;exit(1);}
write(fd,buf,len+1);
close(fd);
sleep(10);*/
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sock_fd,(struct sockaddr *)&their_addr,&sin_size))==-1)
//{perror("accept" ;continue;}
//printf("server got connection from%\n",inet_ntoa(their_addr.sin_addr));
if(!fork())
{
sen=10;
for(sen=0;sen<1000;sen++)
{
nd=htons(sen);
write(new_fd,&sen,sizeof(sen));
}
close(new_fd);
}
}
// while(waitpid(-1,NULL,W));
return 0;
}
client 源代码
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define port 8000 //port number which is monitered by server
#define maxdatasize 100 //maximum bytes which can be received
int main(int argc,char *argv[])
{
int sockfd,numbytes;//specify the socket
char buf[maxdatasize];
struct hostent *he;
struct sockaddr_in their_addr;//information of server
if(argc!=2)
{
/*if there is no argument,display useage and quit*/
fprintf(stderr,"usage:client hosname\n" ;
exit(1);
}
if((he=gethostbyname(argv[1]))==NULL)
{
/*acquire the information of server:*/
herror("gethostbyname" ;
exit(1);
//display the error and quit
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket" ;
exit(1);
}
their_addr.sin_family=AF_INET;//host bytes order
their_addr.sin_port=htons(port);//intnet bytes order
their_addr.sin_addr=*((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), ;//make the rest of the space in the struct zero
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1)
{
/*if function connect() fails,display the error and quit*/
perror("connect");
exit(1);
}
/*
if((numbytes=recv(sockfd,buf,maxdatasize,0))==-1)
{
/*if function recv() fails,display the error and quit*/
/* perror("recv");
exit(1);
}*/
// buf[numbytes]='\0';
read(sockfd,&numbytes,sizeof(numbytes));
printf("receive:%d",numbytes);
close(sockfd);
return 0;
}
自从使用守护进程后,一直无法实现正确发送数据 ,不能执行fork函数不知道为什么,大家帮帮忙 看看哪出错了 小弟不胜感激
[ 本帖最后由 upczhaokai 于 2009-5-2 21:21 编辑 ] |
|