以下程序的预期输出是:
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
没有任何区别。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;
}
答案 0 :(得分:0)
Cygwin git now has the fix,从OpenBSD向后移植。
较小的repro测试案例是posted to the mailing list。