Linux C套接字服务器printf问题

Byr*_*sco 1 c sockets printf

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

int fdflags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, fdflags | O_NONBLOCK);
Run Code Online (Sandbox Code Playgroud)

但除了使接受非阻塞之外,这对代码没有影响.所以我希望这里的某个人能告诉我发生了什么.否则服务器工作.我会继续发布客户端代码.

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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

mar*_*k4o 6

在调用fflush(stdout);之后添加一个调用printf,或者用于setvbuf(stdout, NULL, _IOLBF, 0);设置stdout为行缓冲.