免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1629 | 回复: 3
打印 上一主题 下一主题

[C] socket守护进程不能正确发送数据 大侠们帮帮忙 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-02 21:17 |只看该作者 |倒序浏览
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 编辑 ]

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
2 [报告]
发表于 2009-05-03 08:00 |只看该作者
if(!fork())
这个地方看看为什么不能fork(),errno多少

论坛徽章:
0
3 [报告]
发表于 2009-05-03 09:46 |只看该作者

回复 #1 upczhaokai 的帖子

xinglp 兄  你说的非常有道理,本来我以为变为守护进程后,所有终端就没有用了,于是将accept后的//{perror("accept";continue;}注释掉了,为了向看下errno的值我本来想先将值写进一个文件里,就将注释取消掉了,接收数据也就正常了;再次感谢,不知道怎么给分,我的分好像也不够
另:再问个问题,转变为守护进程后,perror是不是就能使用了,errno是个整型的变量么??

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
4 [报告]
发表于 2009-05-03 12:14 |只看该作者

回复 #3 upczhaokai 的帖子

我也没有注意你说的这个问题, 尝试用一下daemon(),它会把stdin stdout stderr 定向到 /dev/null,
perror就是输出到stderr
errno可以看做一个整形变量
可以使用strerror(errno)获取出错信息的描述.
服务端程序一般都是把出错信息写入日志
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP