當我有兩個過程,服務器和一個客戶端,這應該通過管道(C++,Linux)的通信。 服務器打開與O_RDONLY
標誌管道,並與O_WRONLY
客戶端。 但是,服務器模塊在open
功能,而客戶端似乎已成功運行(在open
函數返回成功等方面做了write
功能)。open()的塊試圖打開管,用於讀取
我已閱讀,如果O_NONBLOCK
標誌設置,讀功能將繼續,但我不希望它繼續,如果沒有客戶端連接 - 這是確定阻塞,直到客戶端連接,但在我的情況下,客戶端運行完成後,即使它仍然受阻......
你能普萊舍告訴我,我做錯了......?
下面是代碼:
// Server side
int pipe;
int status, nr_read = 0;
status = mkfifo(FIFO_NAME, 0666);
if (status < 0)
{
// If the file already exists, delete it
unlink(FIFO_NAME);
// Try again
status = mkfifo(FIFO_NAME, 0666);
if(status < 0)
{
printf("mkfifo error: %d\n", status);
return status;
}
}
pipe = open(FIFO_NAME, O_RDONLY);
printf("Never gets here...\n");
[...]
nr_read = read(pipe, my_char_array, CHAR_ARRAY_SIZE);
[...]
close(pipe);
unlink(FIFO_NAME);
它從來沒有得到的 「printf」 上線......
// Client side:
int pipe, nr_sent = 0;
int status = 0;
pipe = open(FIFO_NAME, O_WRONLY);
if (pipe < 0)
{
printf("open fifo error: %d\n", status);
return pipe;
}
[...]
nr_sent = write(pipe, my_char_array, CHAR_ARRAY_LENGTH);
[...]
close(pipe);
編輯
我沒有提該行 #define FIFO_NAME "MYFIFO"
...這裏是問題: 爲喬迪Hagins說,該路徑是相對的一個過程,從不同的文件夾被啓動,他們試圖打開不同的文件。
難道你accidentially重新FIFO中的客戶端連接後? –
在調試過程中,我等待服務器到達「打開」行,點擊繼續,然後運行客戶端...所以我不相信... – Ioanna
您是否檢查過該文件實際上是由'mkfifo'創建的? –