计划行为:
服务器:
客户端:
================================的问题 ======= ============================
程序确实进行通信,但有时呼叫READ将无法捕获来自其他程序的WRITE呼叫。
=============================================== =========================
服务器代码:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define SERVER_FIFO_FILE "/lab7_server_fifo"
#define CLIENT_FIFO_FILE "/lab7_client_fifo"
#define PERM 0666
int main(int argc, char** argv)
{
int s_read = 0;
int s_write = 0;
int exit = 0;
int listening = 1;
int writing = 0;
char* w_buffer = NULL;
size_t w_buffer_len = 0;
char r_buffer[BUFSIZ];
char* home = getenv("HOME");
char SERVER_FIFO[100];
char CLIENT_FIFO[100];
strcpy(SERVER_FIFO,home);
strcat(SERVER_FIFO, SERVER_FIFO_FILE);
strcpy(CLIENT_FIFO,home);
strcat(CLIENT_FIFO, CLIENT_FIFO_FILE);
mkfifo(SERVER_FIFO, PERM);
mkfifo(CLIENT_FIFO, PERM);
s_read = open(SERVER_FIFO, O_RDONLY | O_NONBLOCK);
s_write = open(CLIENT_FIFO, O_WRONLY | O_NONBLOCK);
while (!exit)
{
if (writing)
{
printf(">>>");
getline(&w_buffer,&w_buffer_len,stdin);
if (strcmp("exit",w_buffer)==0)
{
exit = 1;
}
else
{
write(s_write,w_buffer,w_buffer_len);
writing = 0;
listening = 1;
}
}
else
{
if (listening)
{
printf("listening...\n");
listening = 0;
}
read(s_read, r_buffer, BUFSIZ);
if (strlen(r_buffer) != 0)
{
if (strcmp(r_buffer,"exit") == 0)
{
exit = 1;
}
else
{
printf("%s",r_buffer);
writing = 1;
}
}
}
memset(r_buffer, 0, sizeof(r_buffer));
}
close(s_read);
close(s_write);
remove(SERVER_FIFO);
remove(CLIENT_FIFO);
return 0;
}
客户代码:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define SERVER_FIFO_FILE "/lab7_server_fifo"
#define CLIENT_FIFO_FILE "/lab7_client_fifo"
int main(int argc, char** argv)
{
int c_read = 0;
int c_write = 0;
int exit = 0;
int listening = 0;
int writing = 1;
char* w_buffer = NULL;
size_t w_buffer_len = 0;
char r_buffer[BUFSIZ];
char* home = getenv("HOME");
char SERVER_FIFO[100];
char CLIENT_FIFO[100];
strcpy(SERVER_FIFO,home);
strcat(SERVER_FIFO, SERVER_FIFO_FILE);
strcpy(CLIENT_FIFO,home);
strcat(CLIENT_FIFO, CLIENT_FIFO_FILE);
c_write = open(SERVER_FIFO, O_WRONLY | O_NONBLOCK);
c_read = open(CLIENT_FIFO, O_RDONLY | O_NONBLOCK);
while (!exit)
{
if (writing)
{
printf(">>>");
getline(&w_buffer,&w_buffer_len,stdin);
if (strcmp("exit",w_buffer)==0)
{
exit = 1;
}
else
{
write(c_write,w_buffer,w_buffer_len);
writing = 0;
listening = 1;
}
}
else
{
if (listening)
{
printf("listening...\n");
listening = 0;
}
read(c_read, r_buffer, BUFSIZ);
if (strlen(r_buffer) != 0)
{
printf("%s",r_buffer);
writing = 1;
}
}
memset(r_buffer, 0, sizeof(r_buffer));
}
close(c_read);
close(c_write);
return 0;
}
当前输出:
服务器:
listening...
hello
>>>hi
listening...
客户端:
>>>hello
listening...
hi
>>how are you
listening...
问题:服务器未收到来自服务器的消息“你好吗”。 客户进入了聆听阶段,导致程序陷入聆听的循环中。
已解决我所要做的就是添加标志O_SYNC以写入服务器和客户端,这样它就会自动刷新到磁盘,导致它变慢,这样读取就会有足够的时间来捕获电话。