分叉错误:资源暂时不可用

时间:2017-01-30 20:56:20

标签: c sockets server fork pid

我已经在基于客户端 - 服务器模型的项目中编写了这段代码,但在我的一个文件中我遇到了这个错误:fork error resource暂时不可用。 这是 代码

#include "LibreriaFunzioni.c"
int main (int argc , char *argv[])
{
    int list_fd,list_fd2,fd,fd2,SocketFiglioSede;
    int i;
    struct sockaddr_in s_addr,s_addr2,c_addr,c_addr2;
    char buffer [4096];
    socklen_t len,len2;
    pid_t pid_Admin,pid_Client,pid;
    int logging =1,N_Portate;
    PORTATA *Menu = NULL;
    ORDINE Ordine;
    ORDINE OrdineID;

    Menu = CaricaMenuPortate(&N_Portate);
 //------------------ADMIN--------------------------------------
    /* create socket ADMIN*/
    if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Socket creation error");
        exit(1);
    }

    /* initialize address */
    memset((void *)&s_addr, 0, sizeof(s_addr));   /* clear server address */
    s_addr.sin_family = AF_INET;                  /* address type is INET */
    s_addr.sin_port = htons(2700);                   /* echo port is 7 */
    s_addr.sin_addr.s_addr = htonl(INADDR_ANY);   /* connect from anywhere */

    /* bind socket */
    if (bind(list_fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) < 0) {
        perror("bind error");
        exit(1);
    }

    /* main body */
    if (listen(list_fd, 4096) < 0) {
        perror("listen error");
        exit(1);
    }
//-----------------------CLIENT-------------------------------------
    /* create socket CLIENT */
    if ( (list_fd2 = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Socket creation error");
        exit(1);
    }

    /* initialize address */
    memset((void *)&s_addr2, 0, sizeof(s_addr2));   /* clear server address */
    s_addr2.sin_family = AF_INET;                  /* address type is INET */
    s_addr2.sin_port = htons(2800);                   /* echo port is 7 */
    s_addr2.sin_addr.s_addr = htonl(INADDR_ANY);   /* connect from anywhere */

    /* bind socket */
    if (bind(list_fd2, (struct sockaddr *) &s_addr2, sizeof(s_addr2)) < 0) {
        perror("bind error");
        exit(1);
    }

    /* main body */
    if (listen(list_fd2, 4096) < 0) {
        perror("listen error");
        exit(1);
    }





    while(1){
      //---------GESTIONE ADMIN--------------------------------------------
        pid_Admin = fork();
        if(pid_Admin < 0) {
            perror (" fork error ");
            exit ( -1);
        }
        if(pid_Admin == 0)
        {
            len = sizeof(c_addr);
            if ((fd = accept(list_fd, (struct sockaddr *) &c_addr, &len)) < 0) {
                perror("accept error");
                exit(1);
            }

            close ( list_fd );
            if(logging)
            {
                inet_ntop(AF_INET, &c_addr.sin_addr, buffer, sizeof(buffer));
                printf("\nNUOVA SESSIONE con admin IP:PORTA => %s:%d su socket => %d\n", buffer, ntohs(c_addr.sin_port), fd);
                SpedisciTabella(fd, N_Portate, Menu);
            }

            close(fd);
            exit (0);
        }
     //-------------GESTIONE CLIENT----------------------------------------
        pid_Client = fork();
        if(pid_Client < 0) {
            perror (" fork error ");
            exit ( -1);
        }
        if(pid_Client == 0)
        {
            len2 = sizeof(c_addr2);
            if ((fd2 = accept(list_fd, (struct sockaddr *) &c_addr2, &len2)) < 0) {
                perror("accept error");
                exit(1);
            }

            close ( list_fd2 );
            if(logging)
            {
                inet_ntop(AF_INET, &c_addr.sin_addr, buffer, sizeof(buffer));
                printf("\nNUOVA SESSIONE con client IP:PORTA => %s:%d su socket => %d\n", buffer, ntohs(c_addr.sin_port), fd);
                SpedisciTabella(fd2, N_Portate, Menu);
            }
            pid = fork();
            if((pid<0))
            {perror ("Errore nella fork()"); exit (-1);}
            if(pid==0)
            {
                read(fd2, &Ordine, sizeof(Ordine));
                if(Ordine.sede == 1){
                    SocketFiglioSede = ConnettiAlServer("127.0.0.1", 2200);
                    FullWrite(SocketFiglioSede, &Ordine, sizeof(Ordine));
                    read(SocketFiglioSede,&Ordine, sizeof(Ordine));
                    printf("\nID %d",Ordine.ID);
                    printf("\nCOD %d",Ordine.SceltaP);
                    FullWrite(fd2, &Ordine, sizeof(Ordine));
                    printf("\n** (PADRE) Disconnetti client su socket ==> %d **\n", fd2);
                    close(SocketFiglioSede); /
                    exit(0);
                }// Chiudi processo

                if(Ordine.sede == 2){
                    SocketFiglioSede = ConnettiAlServer("127.0.0.1", 2300);
                    FullWrite(SocketFiglioSede, &Ordine, sizeof(Ordine));
                    read(SocketFiglioSede,&Ordine, sizeof(Ordine));
                    FullWrite(fd2, &Ordine, sizeof(Ordine));
                    close(SocketFiglioSede);
                    printf("\n** (PADRE) Disconnetti client su socket ==> %d **\n", fd2);
                    exit(0);
                }// Chiudi processo

            }
            close(fd2);
            exit (0);
        }
    }
    exit (0);
}

我尝试纠正代码,但我一直都失败了。 你能救我吗?

0 个答案:

没有答案