免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 862 | 回复: 1

请教deamon如何退出 [复制链接]

论坛徽章:
0
发表于 2005-12-06 15:35 |显示全部楼层
下面是我们老师要我们做的题:
题目:创建和运行daemon,通过client端程序给daemon发送用户输入的英文字符,daemon排序后输出。

基本要求:
1 注意daemon应该注意和遵循的原则。
1 非英文字母忽略掉,输入'?'表示结束本client输入,client程序退出。
2 通讯方法不限。
3 client结束后,排序结果输出到屏幕,daemon退出。
output ex:
acghlopsxz
以下是我的程序:
//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 2323
#define LENGTH 128

int main(int argc, char *argv[])
{
        int sockfd;
        //int numbytes;
        char buf[LENGTH];
        char space[LENGTH];
        bzero (buf,sizeof(buf));
        bzero (space, sizeof(space));
        struct hostent *he;
        struct sockaddr_in their_addr;
        int iNum = 0;
        int jNum = 0;
        if (3 != argc)
        {
                printf("parameters should three,error!\n";
                exit(1);
        }
        he = gethostbyname(argv[1]);
        if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0)))
        {
                perror("socket";
                exit(1);
        }

        their_addr.sin_family = AF_INET;
        their_addr.sin_port = htons(PORT);
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        bzero(&(their_addr.sin_zero), ;
        if (-1 == connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)))
        {
                perror("connect";
                exit(1);
        }
        strcpy(space, argv[2]);
                while(('\0' != space[iNum]) && ('?' != space[iNum]))
        {
                if(((65 <= space[iNum]) && (90 >= space[iNum])) || ((97 <= space[iNum]) && (122>= space[iNum])))
                {
                        buf[jNum] = space[iNum];
                        jNum++;
                }
                iNum++;
        }
        buf[jNum] = '\0';
                if(-1 == send(sockfd, buf, strlen(buf), 0))
        {
                perror("send";
                exit(1);
        }
        close(sockfd);
        return 0;
}       

//daemon
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define PORT 2323
#define LENGTH 128
int init_daemon(void)
{
        int pid;
        pid = fork();
        if(0 > pid)
        {
                printf("Fork failure!\n";
                return -1;
        }
        else if(0 < pid)
        {
//                printf("arent process during call the daemon!\n";
                exit(0);
        }
       
        setsid();
        chdir("/";
        umask(0);
        close(0);
        close(1);
        close(2);
        int myfd = open("/dev/tty1", O_WRONLY|O_APPEND);
        if(myfd < 0)
        {
                printf("open another temi error!\n";
                exit(0);
        }
        dup2(myfd, 1);       
}

int main(void)
{
        init_daemon();
        int sockfd;
        int new_fd;
        int pid;
        int numbytes;
        int iNum;
        int jNum;
        char temp;
        char buff[LENGTH];
        struct sockaddr_in my_addr;
        struct sockaddr_in their_addr;
        int sin_size;       
        bzero(buff, sizeof(buff));

        if(-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0)))
        {
                perror("socket";
                exit(1);
        }
        my_addr.sin_family = AF_INET;
        my_addr.sin_port = htons(PORT);
        my_addr.sin_addr.s_addr = INADDR_ANY;
        bzero(&(my_addr.sin_zero), ;
        if (-1 == bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)))
        {
                perror("bind";
                exit(1);
        }
       
        if (-1 == listen(sockfd, 5))
        {
                perror("listen");
                exit(1);
        }
        puts("Listening service start:");
        while(1)
        {
                sin_size = sizeof(struct sockaddr_in);
                if(-1 == (new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)))
                {
                        perror("accept");
                        exit(1);
                }
                pid = fork();
                if (0 == pid)
                {
                        if (-1 == (numbytes = recv(new_fd, buff, LENGTH, 0)))
                        {
                                perror("recv");
                                exit(1);
                        }
       
                        for (iNum = 1; iNum < strlen(buff); iNum++)
                        {
                                for(jNum = 0; jNum < strlen(buff) - iNum; jNum++)
                                {
                                        if( buff[jNum] > buff[jNum + 1])
                                        {
                                                temp = buff[jNum];
                                                buff[jNum] = buff[jNum + 1];
                                                buff[jNum + 1] = temp;
                                        }
                                }
       
                        }
                        printf("the result is: %s\n", buff);
                        close (new_fd);
                        exit(0);
                }
                close (new_fd);
        }
        close(sockfd);
        return 0;
}

但运行结果没能退出daemon,而且也在client端输出,请各位指点,谢谢!

论坛徽章:
0
发表于 2005-12-06 18:40 |显示全部楼层
自己先顶一下,望各位高手不吝赐教
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP