阻止UDP套接字C.

时间:2017-05-19 19:42:24

标签: c sockets udp

有人可以解释为什么例如在调用ConnectionSocket()函数之前这个printf没有被执行吗?

我知道套接字在默认情况下是阻塞的,所以它等待它直到它收到一些东西。但是那个时候的功能还没有被执行,为什么不打印"测试"首先在屏幕上?

On what day of the month were you born? 13
What is the name of the month in which you were born? October
During what year were you born? 1943
You were born on October, 131943. You're mighty old!

1 个答案:

答案 0 :(得分:1)

printf()将要写入的字符串存储在缓冲区中,如果缓冲区已满,或者插入了“\ n”或者调用fflush(),或者标准函数调用fflush()或者如果到达程序结束。

我用write()编写了一个直接在屏幕上打印的例子。

#include <stdio.h>
#include <unistd.h>

int     main(void)
{
    printf("1");                    // stored in a buffer but not printed on screen
    write(STDOUT_FILENO, "2", 1);   // directly printed on screen (2)
    printf("3");                    // stored in a buffer but not printed on screen
    write(STDOUT_FILENO, "4", 1);   // directly printed on screen (4)
    fflush(stdout);                 // flushing stdout, 1 and 3 are printed
    write(STDOUT_FILENO, "5", 1);   // directly printed on screen (5)
    printf("6\n");                  // stored in a buffer then the buffer is printed on the screen because of '\n' (6\n)
    printf("7");                    // stored in a buffer but not printed on screen

    return (0);                     // end of the program, 7 is printed
}

输出为241356 \ n7

相关问题