【问题标题】:Linux C socket server printf problemLinux C 套接字服务器 printf 问题
【发布时间】:2010-11-29 06:58:08
【问题描述】:

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

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

但这对代码没有影响,除了使接受非阻塞。所以我希望这里有人能告诉我发生了什么。服务器以其他方式工作。我也会继续发布客户端代码。

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;
}

【问题讨论】:

  • 如果你在听之前先 printf,你有同样的问题吗?

标签: c sockets printf


【解决方案1】:

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

【讨论】:

  • 您已经使用setvbuf() 调用抑制了任何缓冲区;对于大多数目的,它是无缓冲的,而不是行缓冲的。但我同意基本诊断。它确实有点取决于如何调用守护程序(服务器)。但是,如果标准输出被发送到文件而不是连接到终端,那么您是正确的。
  • 或等价于setlinebuf(stdout)
【解决方案2】:

你试过了吗:

pid_t pid = getpid();

并尝试使用 gdb 打印 pid 值? 不过,您的 pid 应该打印。如果你尝试怎么办:

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

【讨论】:

  • %ld 几乎肯定是pid_t 的错误格式。我从来没有见过一个平台在默认促销之后除了int(有时之前是short)。
  • @R。 from man 2 wait: printf("Child PID is %ld\n", (long) getpid());
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    相关资源
    最近更新 更多