创建无需任何外部执行即可连续监听的守护程序文件

时间:2013-09-12 19:50:47

标签: c++ sockets daemon

我有deamon过程。哪个用作服务器套接字。为了让它听,我手动在终端中执行它:./daemon

现在当我想在ftp服务器上保留它时,我无法以这种方式执行。所以我希望它继续听取聋人的意见。

我在goolge上看到如何创造它。它说具有两个fork()的子进程和父进程将作为守护进程工作。但我无法弄清楚应该在哪里使用哪个进程ID。这是我的代码,有人请指导:

using namespace std;

void *SocketHandler(void *);



int main(int argv, char **argc)

{

    int host_port = 1103;

    char buf[20];

    int k;

    struct sockaddr_in my_addr;

    int hsock;

    int *p_int;

    int err;

    socklen_t addr_size = 0;

    int *csock;

    sockaddr_in sadr;

    pthread_t thread_id = 0;



    hsock = socket(AF_INET, SOCK_STREAM, 0);

    if (hsock == -1) {

    printf("Error initializing socket %dn", errno);

    goto FINISH;

    }



    p_int = (int *) malloc(sizeof(int));

    *p_int = 1;



    if ((setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char *) p_int, sizeof(int)) == -1) || (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char *) p_int, sizeof(int)) == -1)) {

    printf("Error setting options %dn", errno);

    free(p_int);

    goto FINISH;

        }

    free(p_int);



    my_addr.sin_family = AF_INET;

    my_addr.sin_port = htons(host_port);



    memset(&(my_addr.sin_zero), 0, 8);

    my_addr.sin_addr.s_addr = INADDR_ANY;



    if (bind(hsock, (sockaddr *) & my_addr, sizeof(my_addr)) == -1) {

    fprintf(stderr, "Error binding to socket, make sure nothing else is listening on this port %dn", errno);

    goto FINISH;

    }

    if (listen(hsock, 10) == -1) {

    fprintf(stderr, "Error listening %dn", errno);

    goto FINISH;

    }

    //Now lets do the server stuff



    addr_size = sizeof(sockaddr_in);



    while (true) {

    printf("waiting for a connectionn\n");

    csock = (int *) malloc(sizeof(int));

    if ((*csock = accept(hsock, (sockaddr *) & sadr, &addr_size)) != -1) {

        printf("---------------------nReceived connection from %s\n", inet_ntoa(sadr.sin_addr));

        pthread_create(&thread_id, 0, &SocketHandler, (void *) csock);

        pthread_detach(thread_id);

    } else {

        fprintf(stderr, "Error accepting %dn", errno);

    }

    }



  FINISH:

    ;

}



std::pair < int, std::string > mytransform(const std::pair < std::string, int >p)

{

    return std::pair < int, std::string > (p.second, p.first);

}



void *SocketHandler(void *lp)

{

    int ar[10];

    int result=0;

    int *csock = (int *) lp;

    char buf[20];

    int k;

    char *skp;

    char *str;

    char *str2;



        std::stringstream ss;

    std::multimap < int, std::string, std::greater < int >>dst;

        std::multimap < int, std::string >::iterator rec;

        std::map < std::string, int >src;

        std::map < int, std::vector < std::string > >three_highest; 

        std::vector < std::string > writable;

    std::string item;

    std::ostringstream bfr; 

    std::string result_string;

    std::istringstream iss;

    std::ostringstream oss;

  //    std::multimap < int, std::string >::iterator it; 





    std::vector<std::string> most;

    int max_count = 0;

    int tmp=0;



    int pcount = 0, ncount = 0;



    char buffer[1024];

    int buffer_len = 1024;

    int bytecount;



    int i = 0,t=0,q=0;

    int j = 0;

    char *ch[50] = { 0 };   /* stores references to 50 words. */

    char *ch2[50] = { 0 };  

    char *excluded_string[50] = { 0 };



        char *word = strtok(buffer, " ");

        char *word2 = strtok(buffer, " ");



        char *portstring1=(char *)malloc(sizeof(buffer));

        char *portstring2=(char *)malloc(sizeof(buffer));



    memset(buffer, 0, buffer_len);

    if ((bytecount = recv(*csock, buffer, buffer_len, 0)) == -1) {

    fprintf(stderr, "Error receiving data %d \n", errno);

    goto FINISH;

    }

    printf("Received bytes %d \nReceived string  %s \n ", bytecount, buffer);

    word = strtok(buffer, " ");



    while ((NULL != word) && (50 > i)) {

    ch[i] = strdup(word);

    excluded_string[j]=strdup(word);

    word = strtok(NULL, " ");

    skp = BoyerMoore_skip(ch[i], strlen(ch[i]) );

    if(skp != NULL)

    {

        i++;

        continue;

     }

    printf("exclueded : %s  and %s size %d \n",excluded_string[j],ch[i],sizeof(excluded_string));

    bfr << excluded_string[j] << " ";

    result_string = bfr.str();

    j++;    

    //  std::cout << "string is :" << r1;



    }

        std::cout << "string is :" << result_string << "\n";



            ss<<result_string;



        while (std::getline(ss, item, ' ')) {

            writable.push_back(item);

        }



        for (std::vector < std::string >::iterator it = writable.begin(); it != writable.end(); it++)

            ++src[*it];



        std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()), mytransform);



    rec=dst.begin();   

    for (auto it = dst.begin(); it != dst.end(); ++it)

        std::cout << it->second << ":" << it->first << std::endl;



    while (three_highest.size() < 3 && rec != dst.end()) {

        three_highest[rec->first].push_back(rec->second);

        rec++;

       }



    //std::cout << "\nthree_highest:\n";

    i=0;

    for (std::map < int, std::vector < std::string > >::iterator hit = three_highest.begin(); hit != three_highest.end(); ++hit) {

    //std::cout << hit->first << ":";



    for (std::vector < std::string >::iterator vit = (*hit).second.begin(); vit != (*hit).second.end(); vit++) {

        std::cout << hit->first << ":";

        std::cout << *vit << "\n";

        ar[i]= hit-> first;

        printf(" ar : %d \n",ar[i]);

        i++;

        oss << hit->first << " " << *vit << "\n";

    }

    }

    printf( "i is  :%d \n",i);



    if ((bytecount = send(*csock, (char *)ar, i *sizeof(int), 0)) == -1) { // Here we cant send lenth-1. It consider exact

    fprintf(stderr, "Error sending data %d\n", errno);

    goto FINISH;

    }



  FINISH:

    free(csock);

    return 0;

}

1 个答案:

答案 0 :(得分:0)

你应该从一个更简单的问题开始。在这种情况下,您似乎遇到了创建守护进程的问题。 This是一个很好的教程,对我来说非常有用。