0
我有一個InPipe意味着讀取二進制數據,而OutPipe意味着寫回通過我的防火牆的二進制數據。命名管道只能讀取而不能寫入
/// The input named pipe, "ToFirewall"
static FILE* InPipe = NULL;
/// The output named pipe, "FromFirewall"
static FILE* OutPipe = NULL;
我在一個單獨的函數中打開兩個管道。
static bool OpenPipes(void)
{
//ToFirewall
InPipe = fopen("ToFirewall", "rb");
if(InPipe == NULL)
{
perror("ERROR, failed to open pipe ToFirewall:");
return false;
}
OutPipe = fopen("FromFirewall", "wb");
if(OutPipe == NULL)
{
perror("ERROR, failed to open pipe FromFirewall:");
return false;
}
return true;
}
出於某種原因,我在數據讀取,它會跳過寫,不會刻意去檢查它是否通過我的防火牆通過與否。我在網上看了一下,看了有關沖洗的解決方案,但沒有幫助。
static void* FilterThread(void* args) {
OpenPipes();
unsigned char* buffer = malloc(1500);
int ret = fread(buffer, 1, 1500, InPipe);
if(ret){
fclose(InPipe);
}
//Check is FilterPacket will allow the packet through the firewall
if(FilterPacket(buffer, args)) {
fwrite(buffer, 1, 60, OutPipe);
fflush(OutPipe);
}
// fflush(OutPipe);
fclose(OutPipe);
return NULL;
}
這裏是我的輸出
RCVR: opened file output.bin
SNDR: Waiting 200ms between packets
SNDR: Number of packets: 18
SNDR: Starting packet 0
SNDR: Starting packet 1
SNDR: Starting packet 2
SNDR: Starting packet 3
SNDR: Starting packet 4
SNDR: Starting packet 5
SNDR: Starting packet 6
SNDR: Starting packet 7
SNDR: Starting packet 8
SNDR: Starting packet 9
SNDR: Starting packet 10
SNDR: Starting packet 11
SNDR: Starting packet 12
SNDR: Starting packet 13
SNDR: Starting packet 14
SNDR: Starting packet 15
SNDR: Starting packet 16
SNDR: Starting packet 17
SNDR: Finished, wrote 18 packets to the pipe
在這裏你可以看到預期的輸出實際上應該看起來像
> RCVR: opened file output.bin
SNDR: Waiting 200ms between packets
SNDR: Number of packets: 18
SNDR: Starting packet 0
SNDR: Starting packet 1
RCVR: 129.21.37.11 -> 74.125.21.103
SNDR: Starting packet 2
RCVR: 74.125.21.103 -> 129.21.37.11
SNDR: Starting packet 3
SNDR: Starting packet 4
RCVR: 129.21.37.11 -> 74.125.21.103
SNDR: Starting packet 5
SNDR: Starting packet 6
RCVR: 74.125.21.103 -> 129.21.37.11
SNDR: Starting packet 7
RCVR: 129.21.37.28 -> 74.125.21.103
SNDR: Starting packet 8
SNDR: Starting packet 9
RCVR: 129.21.37.11 -> 74.125.21.103
SNDR: Starting packet 10
RCVR: 74.125.21.103 -> 129.21.37.28
SNDR: Starting packet 11
SNDR: Starting packet 12
RCVR: 74.125.21.103 -> 129.21.37.11
SNDR: Starting packet 13
RCVR: 129.21.37.28 -> 74.125.21.103
SNDR: Starting packet 14
SNDR: Starting packet 15
SNDR: Starting packet 16
RCVR: 129.21.37.11 -> 74.125.21.103
SNDR: Starting packet 17
RCVR: 74.125.21.103 -> 129.21.37.28
SNDR: Finished, wrote 18 packets to the pipe
FwSim, Commanding firewall to Exit
RCVR: 74.125.21.103 -> 129.21.37.11
Exiting
能否請您嘗試創建一個[最小,完整,可驗證的示例](HTT p://stackoverflow.com/help/mcve)並向我們展示?您在程序中的哪個位置打印輸出?你有沒有檢查'fread'(還有'fwrite')實際返回的結果? –
哦,你知道pipe是* streaming *,意思是它可能不會給你所有在一次讀取調用中要求的數據,你可能需要循環讀取。 –
管道的另一端有什麼? – immibis