我需要通過管道傳遞一些字符串。我們不能通過管道傳遞指針,我們必須傳遞數據。爲傳遞一個字符串,我們可以發送一個字符數組。但我不想使用數組。我需要一種發送可變大小的字符串的方法。命名管道中的多個寫入文件和讀取文件
我用MSDN示例創建管道服務器和管道客戶端:在管客戶端和管服務器只有一個的WriteFile和ReadFile函數的
但inestead我用他們這樣三個時間:
我用用於保存我的字符串大小的結構。首先這個結構被髮送。那麼我的兩個字符串將被髮送。所以在管道服務器中,首先讀取字符串的大小,然後再接收兩個字符串。
我在客戶端和服務器程序定義是這樣的結構:
typedef struct
{
int fileNameLen;
int commandArgLen;
}pipeData,*PpipeData;
pipeData dataToWrite;
pipeData *pdataToWrite = &dataToWrite;
在管道客戶
我想給這個字符串:
LPTSTR s1 = TEXT("file1");
LPTSTR s2 = TEXT("startCmd");
dataToWrite.commandArgLen = sizeof(s1);
dataToWrite.fileNameLen = sizeof(s2);
我發送的結構通過管道客戶在這辦法。
fSuccess = WriteFile(
hPipe, // pipe handle
pdataToWrite, // message
sizeof(dataToWrite), // message length
&cbWritten, // bytes written
NULL); // not overlapped
if (! fSuccess)
{
_tprintf(TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError());
return -1;
}
fSuccess = WriteFile(
hPipe, // pipe handle
s1, // message
sizeof(s1), // message length
&cbWritten, // bytes written
NULL); // not overlapped
if (! fSuccess)
{
_tprintf(TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError());
return -1;
}
fSuccess = WriteFile(
hPipe, // pipe handle
s2, // message
sizeof(s2), // message length
&cbWritten, // bytes written
NULL); // not overlapped
if (! fSuccess)
{
_tprintf(TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError());
return -1;
}
在讀管道管服務器
我使用3個READFILE這樣的:
fSuccess = ReadFile(
hPipe, // handle to pipe
pdataToWrite, // buffer to receive data
sizeof(pdataToWrite), // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
if (!fSuccess || cbBytesRead == 0)
{
if (GetLastError() == ERROR_BROKEN_PIPE)
{
_tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError());
break;
}
else
{
_tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError());
break;
}
}
// Process the incoming message.
GetAnswerToRequest(TEXT("structure recieved"), pchReply, &cbReplyBytes);
fSuccess = ReadFile(
hPipe, // handle to pipe
s1, // buffer to receive data
dataToWrite.commandArgLen, // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
if (!fSuccess || cbBytesRead == 0)
{
if (GetLastError() == ERROR_BROKEN_PIPE)
{
_tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError());
}
else
{
_tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError());
}
break;
}
GetAnswerToRequest(s1, pchReply, &cbReplyBytes);
fSuccess = ReadFile(
hPipe, // handle to pipe
s2, // buffer to receive data
dataToWrite.fileNameLen, // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
if (!fSuccess || cbBytesRead == 0)
{
if (GetLastError() == ERROR_BROKEN_PIPE)
{
_tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError());
}
else
{
_tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError());
}
break;
}
GetAnswerToRequest(s2, pchReply, &cbReplyBytes);
這種方式不能正常工作。當管道服務器讀取第一READFILE數據,它可能會返回此錯誤:ERROR_MORE_DATA(如果我在CreateNamedPipe時使用PIPE_TYPE_MESSAGE)
我不知道我怎麼可以使用多個寫文件和READFILE管道客戶端和服務器。