c - Linux C socket服务器printf问题

标签 c sockets printf

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

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

最佳答案

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

关于c - Linux C socket服务器printf问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4301522/

相关文章:

c - 如何创建这个Kakuro表?

C int数组在传递时缩小

java - 使用开放式套接字在 Tomcat 上进行并行部署?

java - 处理异常而不导致套接字服务器崩溃?

c++ - 如何仅打印完整路径名的文件部分?

c - char * 与 getchar 到 printf 的奇怪输出

C 双括号

c - 让子进程等待另一个for循环

java - 用 Java 构建 IRC 机器人

c - 打印无限值