变量返回Null?

时间:2016-02-12 23:02:25

标签: c sockets while-loop fork

我正在尝试通过根据发送的第一个字符串将多个内容打印到同一个文件来练习我的套接字编程。我发现我的char指针在运行后仍然无效,因此不再存储首次发送的名称。这是怎么回事?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>

char* doprocessing (char *fnptr) {
    fnptr = "filename.txt";
    return fnptr;
}

int main( int argc, char *argv[] ) {
    int sockfd, newsockfd, portno, clilen;
    struct sockaddr_in serv_addr, cli_addr;
    int count=0, pid;
    char *fnptr = NULL;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0) {
        perror("ERROR opening socket");
        exit(1);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = 5001;

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
        perror("ERROR on binding");
        exit(1);
    }

    listen(sockfd,5);
    clilen = sizeof(cli_addr);

    while(1){

        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

        if (newsockfd < 0) {
            perror("ERROR on accept");
            exit(1);
        }

        pid = fork();

        if (pid < 0) {
            perror("ERROR on fork");
            exit(1);
        }

        if (pid == 0) {
            close(sockfd);
            printf("Pid is 0\n");
            fnptr = doprocessing(fnptr);
            printf("Fnptr is %s\n", fnptr);
            exit(0);
        }

        else {
            printf("Fnptr in else is %s\n", fnptr);
            close(newsockfd);
        }
        printf("fnptr outside of else is %s\n", fnptr);

    }

}

输出

Fnptr in else is (null) //Expected
fnptr outside of else is (null) //Expected
Pid is 0 //Start of processing
Fnptr is filename.txt //Goal
Fnptr in else is (null) //Where'd it go?
fnptr outside of else is (null) //Not good :(

1 个答案:

答案 0 :(得分:1)

可疑声音,但工作代码!使用此处的指南:TLDP Man Page - Creating Pipes in C,我能够实现管道指针以便跟踪它。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>

char* doprocessing (char *fnptr) {
    fnptr = "filename.txt";
    return fnptr;
}

int main( int argc, char *argv[] ) {
    int sockfd, newsockfd, portno, clilen;
    struct sockaddr_in serv_addr, cli_addr;
    int count=0, pid, fd[2], nbytes;
    char *fnptr = NULL, readbuffer[80];

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0) {
        perror("ERROR opening socket");
        exit(1);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = 5002;

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
        perror("ERROR on binding");
        exit(1);
    }

    listen(sockfd,5);
    clilen = sizeof(cli_addr);

    pipe(fd);

    while(1){

        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

        if (newsockfd < 0) {
            perror("ERROR on accept");
            exit(1);
        }

        pid = fork();

        if (pid < 0) {
            perror("ERROR on fork");
            exit(1);
        }

        if (pid == 0) {
            close(sockfd);
            printf("Pid is 0\n");
            fnptr = doprocessing(fnptr);
            printf("Fnptr is %s\n", fnptr);
            close(fd[0]);
            write(fd[1], fnptr, (strlen(fnptr)+1));
            exit(0);
        }

        else {
            close(fd[1]);
            nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
            fnptr = readbuffer;
            printf("Fnptr in else is %s\n", fnptr);
            close(newsockfd);
        }
        printf("fnptr outside of else is %s\n", fnptr);

    }

}