需要一些涉及C中分叉的帮助

时间:2015-08-15 09:02:25

标签: c

你好,所以我一直在研究这个问题已经有一段时间了,无法解决这个问题。 我正在尝试使用特定的端口号建立与客户端的连接。由于某些原因,如果我的While循环包含在代码中,则子进程正在退出。当我取出while循环时,一切都按预期执行。有什么指导吗?

这是我的代码

> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

int main(int argc, char *argv[]){
    if (argc < 2) {
        printf("Please enter a port");
    } else {
        printf("test2");
        int socketfd;
        pid_t pid, sid;

        //Fork
        pid = fork();
        if (pid < 0) {
            perror("Fork failed");
            exit(1);
        }

        if (pid > 0) {
            printf("exiting");
            exit(0);

        } 

        if (pid == 0) {
            printf("Hi");
            sid = setsid();
            if (sid < 0){
                    printf("failure");
            }
            //Start the Daemon
            printf("Test");
            socketfd = socket(AF_INET, SOCK_STREAM, 0);
            if(socketfd < 0){
                printf("socket error");
            }
            int portNumber = atoi(argv[1]); //Port number entered in by user.   
            struct sockaddr_in server;
            struct sockaddr_in clientAddress;
            bzero((char *) &server, sizeof(server)); // Clear buffer for server address
            server.sin_addr.s_addr = INADDR_ANY; 
            server.sin_port = htons(portNumber); 
            server.sin_family = AF_INET; 
            socklen_t clientLength;
            printf("Test3");    
            if (bind(socketfd, (struct sockaddr *) &server, sizeof(server)) == -1)
            {
                perror("bind call failed");
            }
            printf("Test5");
            listen(socketfd, 5);
            clientLength = sizeof(clientAddress);
            printf("Test6");
            while(1) {
                int client_socketfd;
                int x; 
                client_socketfd = accept(socketfd, (struct sockaddr *) &clientAddress, &clientLength);
                if(client_socketfd == -1){
                    perror("ERROR: \n");
                }
                FILE * pFile;
                char receivedFile[1000];
                char rawData[1000];
                bzero(receivedFile, 1000);
                if (read(client_socketfd,receivedFile,1000) == -1){
                    perror("Read Failure");
                }
                printf("Received File %s",receivedFile); 
                pFile = fopen(receivedFile, "r");
                if(pFile == NULL){
                    perror("Cannot open file"); 
                }
                if(fgets(rawData,1000,pFile) != NULL) {
                    int fileLength = strlen(rawData);
                    for(x = 0; x < fileLength; x++){
                        printf("%s\n",rawData[x]);
                    }
                } else {
                    perror("Nothing in the file!\n");
                }
            }   


        }


    }
    return 0;
}

以下是编译并运行上述代码时发生的情况:

Compile and Run

这是我将问题提供给while循环的测试。

 > #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>

    int main(int argc, char *argv[]){
        if (argc < 2) {
            printf("Please enter a port");
        } else {
            printf("test2");
            int socketfd;
            pid_t pid, sid;

            //Fork
            pid = fork();
            if (pid < 0) {
                perror("Fork failed");
                exit(1);
            }

            if (pid > 0) {
                printf("exiting");
                exit(0);

            } 

            if (pid == 0) {
                printf("Hi");
                sid = setsid();
                if (sid < 0){
                        printf("failure");
                }
                //Start the Daemon
                printf("Test");
                socketfd = socket(AF_INET, SOCK_STREAM, 0);
                if(socketfd < 0){
                    printf("socket error");
                }
                int portNumber = atoi(argv[1]); //Port number entered in by user.   
                struct sockaddr_in server;
                struct sockaddr_in clientAddress;
                bzero((char *) &server, sizeof(server)); // Clear buffer for server address
                server.sin_addr.s_addr = INADDR_ANY; 
                server.sin_port = htons(portNumber); 
                server.sin_family = AF_INET; 
                socklen_t clientLength;
                printf("Test3");    
                if (bind(socketfd, (struct sockaddr *) &server, sizeof(server)) == -1)
                {
                    perror("bind call failed");
                }
                printf("Test5");
                listen(socketfd, 5);
                clientLength = sizeof(clientAddress);
                printf("Test6");

            }


        }
        return 0;
    }

此代码成功进入孩子并打印出所有“测试”

0 个答案:

没有答案