以下程序的预期输出是:
received REQUEST
from client
received REPLY
from server
例如,可以在GNU / Linux上看到。但是,在Cygwin上,实际输出是这样的:
received REQUEST
from client
received
from server
从客户端的流而不是
"\n"
接收到空行("REPLY\n"
字符串)。问题源于
putc
和行缓冲的结合。在
server
函数中,如果将my_puts
替换为fprintf
,则会产生预期的输出。在
server
中,如果我们执行setvbuf
调用,该调用将流切换到行缓冲,则还会产生预期的输出。如果更改
my_puts
使其使用fprintf(f, "%c", ch)
而不是putc(ch, f)
输出每个字符,则将产生预期的输出。用
putc
替换fputc
没有区别。在服务器中,根据ISO C的要求在输入和输出之间添加文件定位操作(例如
fseek(cli_stream, 0, SEEK_CUR)
)(因此恰好在my_puts
调用之前)没有区别。这包含在下面的代码中。这个程序做错了什么会导致这个奇怪的问题吗?
码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
int my_puts(FILE *f, const char *str)
{
int ch;
while ((ch = *str++))
if (putc(ch, f) == EOF) {
perror("putc");
return EOF;
}
return 0;
}
int client(struct sockaddr_in *srv_addr)
{
int srv_fd = socket(AF_INET, SOCK_STREAM, 0);
FILE *srv_stream;
char buf[256];
if (srv_fd == -1) {
perror("socket");
return EXIT_FAILURE;
}
if (connect(srv_fd, (struct sockaddr *) srv_addr, sizeof *srv_addr) == -1) {
perror("connect");
close(srv_fd);
return EXIT_FAILURE;
}
if ((srv_stream = fdopen(srv_fd, "r+")) == 0) {
perror("fdopen");
close(srv_fd);
return EXIT_FAILURE;
}
setvbuf(srv_stream, (char *) NULL, _IOLBF, 0);
my_puts(srv_stream, "REQUEST\n");
fflush(srv_stream);
shutdown(srv_fd, SHUT_WR);
if (fgets(buf, sizeof buf, srv_stream) == 0) {
printf("short fread");
fclose(srv_stream);
return EXIT_FAILURE;
}
printf("received %s from server\n", buf);
return 0;
}
void server(int acc_fd)
{
struct sockaddr_in claddr;
socklen_t claddrlen = sizeof claddr;
int cli_fd = accept(acc_fd, (struct sockaddr *) &claddr, &claddrlen);
FILE *cli_stream = fdopen(cli_fd, "r+");;
char buf[256];
if (cli_fd == -1) {
perror("accept");
if (cli_stream)
fclose(cli_stream);
return;
}
if (cli_stream == 0) {
perror("fdopen");
close(cli_fd);
return;
}
setvbuf(cli_stream, (char *) NULL, _IOLBF, 0);
if (fgets(buf, sizeof buf, cli_stream) == 0) {
printf("short fread");
fclose(cli_stream);
return;
}
printf("received %s from client\n", buf);
fseek(cli_stream, 0, SEEK_CUR);
my_puts(cli_stream, "REPLY\n");
fflush(cli_stream);
fclose(cli_stream);
}
int main(void)
{
struct sockaddr_in saddr = { 0 };
int acc_fd = socket(AF_INET, SOCK_STREAM, 0);
pid_t child;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(12345);
saddr.sin_addr.s_addr = htons(INADDR_ANY);
if (acc_fd == -1) {
perror("socket");
return EXIT_FAILURE;
}
if (bind(acc_fd, (struct sockaddr *) &saddr, sizeof saddr) == -1) {
perror("bind");
return EXIT_FAILURE;
}
if (listen(acc_fd, 1) == -1) {
perror("listen");
return EXIT_FAILURE;
}
child = fork();
if (child == -1) {
perror("fork");
return EXIT_FAILURE;
} else if (child == 0) {
server(acc_fd);
_exit(0);
} else {
int status;
int result = client(&saddr);
kill(child, SIGKILL);
if (wait(&status) != child) {
printf("wait problem");
return EXIT_FAILURE;
}
return result;
}
return 0;
}
最佳答案
Cygwin git现在has the fix,从OpenBSD向后移植。
较小的repro测试用例为posted to the mailing list。
关于c - Cygwin:由于stdio putc +行缓冲导致流数据丢失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35928828/