我需要通过管道传递一些字符串。我们不能通过管道传递指针,我们必须传递数据。为了传递一个字符串,我们可以发送一个字符数组。但我不想使用数组。我需要一种方法来发送可变大小的字符串。
我使用msdn样本创建管道服务器和管道客户端:
但是在管道客户端和管道服务器中只有一个writeFile和readFile函数,我以这种方式使用它们三次:
我使用了一种结构来保存我的字符串大小。首先发送这个结构。然后我的两个字符串将被发送。所以在管道服务器中,首先会读取字符串的大小,然后接收两个字符串。
我在客户端和服务器程序中定义了这样的结构:
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)
我不知道如何在管道客户端和服务器中使用多个writeFile和readFile。
答案 0 :(得分:1)
如果正在以消息模式读取命名管道,并且下一条消息的长度超过nNumberOfBytesToRead参数指定的值,则ReadFile返回FALSE,GetLastError返回ERROR_MORE_DATA。可以通过后续调用ReadFile或PeekNamedPipe函数来读取消息的其余部分。通过MSDN
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
FYI