gpt4 book ai didi

c++ - 命名管道中的多个写入文件和读取文件

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:50 25 4
gpt4 key购买 nike

我需要通过管道传递一些字符串。我们不能通过管道传递指针,我们必须传递数据。为了传递一个字符串,我们可以发送一个字符数组。但我不想使用数组。我需要一种发送可变大小字符串的方法。

我使用 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。

最佳答案

如果正在以消息模式读取命名管道并且下一条消息比 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

仅供引用

关于c++ - 命名管道中的多个写入文件和读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16188802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com