파이프별로 문자열을 전달해야합니다. 파이프로 포인터를 전달할 수 없으며 데이터를 전달해야합니다. 문자열을 전달하기 위해 char 배열을 보낼 수 있습니다. 하지만 배열을 사용하고 싶지 않습니다. 가변 길이로 문자열을 보내는 방법이 필요합니다. 내가 사용명명 된 파이프에 여러 개의 writefile 및 readfile이 있습니다.
: 나는 세 시간이 방법을 사용하는 파이프 클라이언트와 파이프 서버에 하나의 WriteFile과 ReadFile을 기능의
하지만 inestead :
나는 파이프 서버와 파이프 클라이언트를 만들기위한 MSDN의 샘플을 사용 내 문자열 크기를 저장하기위한 구조. 우선이 구조가 보내집니다. 내 두 문자열이 전송됩니다. 그래서 처음에는 파이프 서버에서 문자열의 크기를 읽고 두 문자열을받습니다. 나는 클라이언트와 서버 프로그램에서 이런 구조를 정의 : 파이프 클라이언트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 = 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);
이 방법이 제대로 작동하지 않습니다 파이프를 읽기위한 파이프 서버
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을 사용합니다. 파이프 서버가 첫 번째 readFile에서 데이터를 읽을 때이 오류를 반환 할 수 있습니다. ERROR_MORE_DATA (createNamedPipe에서 PIPE_TYPE_MESSAGE를 사용한 경우)
파이프 클라이언트와 서버에서 여러 개의 writeFile 및 readFile을 어떻게 사용할 수 있는지 잘 모릅니다.