Linux C套接字服务器printf问题

发布于 2024-10-05 15:38:36 字数 5227 浏览 3 评论 0原文

我这里有一个简单的套接字服务器,在我看来应该是一个简单的问题。在套接字接受连接之前,我希望它打印其进程 ID。但在建立连接之前,无论它是什么,它都不会打印任何内容。起初我认为这是因为accept调用以某种方式阻塞了打印,所以我尝试在不同的地方添加它:

int fdflags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, fdflags | O_NONBLOCK);

但这除了使accept非阻塞之外对代码没有任何影响。所以我希望这里有人能够告诉我发生了什么事。否则服务器可以工作。我也会继续发布客户端代码。

服务器.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>

static void error(char *msg) {
    perror(msg);
    exit(1);
}

static void SIGCHLD_Handler(int sig) {
    waitpid(-1, NULL, WNOHANG);
}

int main(int argc, char *argv[]) {

    int num,sockfd, newsockfd, portno, clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    struct sigaction sigact;

    sigact.sa_handler = SIGCHLD_Handler;
    if (sigaction(SIGCHLD, &sigact, 0)) error("sighandle def");

    int n, childPid;
    if (argc < 2) {
        fprintf(stderr,"ERROR, no port provided\n");
        exit(1);
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0) error("ERROR opening socket");
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    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) error("ERROR on binding");
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    printf("I am the Knock Knock server and my pid number is %d\n", getpid());

    while (1) {
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0) else error("ERROR on accept");

        bzero(buffer,256);

        childPid = fork();
        if (childPid < 0) error ("ERROR on fork");
        else if (childPid == 0) {
            close(sockfd);

            while(1) {
                // read an int from the client that says how big the message will be
                n = read(newsockfd, &num, sizeof(int));
                // if client sends just Enter, then quit
                if(num==2) break;

                // read num bytes from client
                n = read(newsockfd,buffer,num);
                if (n < 0) error("ERROR reading from socket");

                // display the message from the client
                printf("Here is the message: %s\n",buffer);
                num=19;

                // Tell the client to expect 19 bytes
                n = write(newsockfd, &num, sizeof(int));
                // Send client 19 bytes
                n = write(newsockfd,"I got your message",num);
                if (n < 0) error("ERROR writing to socket");
            }

            exit(0);
        } else close(newsockfd);
    }
    return 0;
}

客户端.c:

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

void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[]) {
    int num, sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

    do{
       printf("Please enter the message: ");
       bzero(buffer,256);
       fgets(buffer,255,stdin);

       num = strlen(buffer)+1;
       // send server an int that says how long the coming message will be
       n = write(sockfd, &num, sizeof(int));
       // num=2 when user just presses Enter. No message = quit
       if(num==2) break;

       // send server the message (num bytes long)
       n = write(sockfd,buffer,num);
       if (n < 0) 
           error("ERROR writing to socket");
       bzero(buffer,256);

       // read how many bytes are coming from the server
       n = read(sockfd, &num, sizeof(int));
       // read num bytes from the server
       n = read(sockfd,buffer,num);
       if (n < 0) 
          error("ERROR reading from socket");
       // display the message from the server
       printf("%s\n",buffer);
    }while(1);

    return 0;
}

I have a simple socket server here, and have what seems to me should be a simple question. Before the socket accepts connections I wish to have it print its process id. But it won't print anything no matter what it is, until there has been a connection. At first I thought this was because the accept call was blocking the print somehow so I tried adding this in various places:

int fdflags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, fdflags | O_NONBLOCK);

But this had no effect on the code other than making the accept non blocking. So I was hoping someone here might be able to tell me what is going on. The server otherwise works. I'll go ahead and post the client code too.

server.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>

static void error(char *msg) {
    perror(msg);
    exit(1);
}

static void SIGCHLD_Handler(int sig) {
    waitpid(-1, NULL, WNOHANG);
}

int main(int argc, char *argv[]) {

    int num,sockfd, newsockfd, portno, clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    struct sigaction sigact;

    sigact.sa_handler = SIGCHLD_Handler;
    if (sigaction(SIGCHLD, &sigact, 0)) error("sighandle def");

    int n, childPid;
    if (argc < 2) {
        fprintf(stderr,"ERROR, no port provided\n");
        exit(1);
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0) error("ERROR opening socket");
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    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) error("ERROR on binding");
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    printf("I am the Knock Knock server and my pid number is %d\n", getpid());

    while (1) {
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0) else error("ERROR on accept");

        bzero(buffer,256);

        childPid = fork();
        if (childPid < 0) error ("ERROR on fork");
        else if (childPid == 0) {
            close(sockfd);

            while(1) {
                // read an int from the client that says how big the message will be
                n = read(newsockfd, &num, sizeof(int));
                // if client sends just Enter, then quit
                if(num==2) break;

                // read num bytes from client
                n = read(newsockfd,buffer,num);
                if (n < 0) error("ERROR reading from socket");

                // display the message from the client
                printf("Here is the message: %s\n",buffer);
                num=19;

                // Tell the client to expect 19 bytes
                n = write(newsockfd, &num, sizeof(int));
                // Send client 19 bytes
                n = write(newsockfd,"I got your message",num);
                if (n < 0) error("ERROR writing to socket");
            }

            exit(0);
        } else close(newsockfd);
    }
    return 0;
}

client.c:

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

void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[]) {
    int num, sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

    do{
       printf("Please enter the message: ");
       bzero(buffer,256);
       fgets(buffer,255,stdin);

       num = strlen(buffer)+1;
       // send server an int that says how long the coming message will be
       n = write(sockfd, &num, sizeof(int));
       // num=2 when user just presses Enter. No message = quit
       if(num==2) break;

       // send server the message (num bytes long)
       n = write(sockfd,buffer,num);
       if (n < 0) 
           error("ERROR writing to socket");
       bzero(buffer,256);

       // read how many bytes are coming from the server
       n = read(sockfd, &num, sizeof(int));
       // read num bytes from the server
       n = read(sockfd,buffer,num);
       if (n < 0) 
          error("ERROR reading from socket");
       // display the message from the server
       printf("%s\n",buffer);
    }while(1);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回梦 2024-10-12 15:38:36

添加对 fflush(stdout); 的调用 调用 printf 之后,或使用 setvbuf(stdout, NULL, _IOLBF, 0);stdout 设置为行缓冲。

Add a call to fflush(stdout); after the call to printf, or use setvbuf(stdout, NULL, _IOLBF, 0); to set stdout to line-buffered.

反差帅 2024-10-12 15:38:36

您是否尝试过一些:

pid_t pid = getpid();

并尝试使用 gdb 打印 pid 值?
尽管如此,你的 pid 应该会打印出来。如果你尝试怎么办:

printf("pid = %ld\n", getpid());

Did you try some:

pid_t pid = getpid();

and try to use gdb to print pid value?
Nevertheless, your pid should print. What if you try :

printf("pid = %ld\n", getpid());
淡水深流 2024-10-12 15:38:36

accept() 会阻塞,但它不会干扰 printf(),并且使其成为非阻塞不会有帮助,并且会破坏程序的正确工作,除非你真的知道你在用非阻塞做什么网络代码。这一定是缓冲问题:fflush(stout) 应该修复它。

accept() blocks, but it doesn't interfere with printf(), and making it non-blocking won't help that, and will destroy the correct working of your program unless you really know what you're doing with non-blocking network code. This must be a buffering problem: fflush(stout) should fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文