VC++ 6 응용 프로그램과 C# 응용 프로그램 간의 양방향 통신을 시도하고 있습니다. 명명 된 파이프를 사용하고 있습니다. 내 C++ 코드에서 C# 클라이언트의 메시지를 읽을 수 있지만 서버가 "죽습니다."다시 메시지를 다시 시작해야합니다.명명 된 파이프를 사용하는 양방향 C++에서 C#으로 통신
내가 원하는 것은 C# 응용 프로그램을 C++ 응용 프로그램에 연결하고 상태를 요청하고 C++ 응용 프로그램이 꺼지고 상태를 확인한 다음 "사용 중"또는 "유휴 중 하나"를 반환합니다.
연결이 닫혔다 고해서 C# 클라이언트에 아무 것도 쓸 수 없습니다. 내가 주석 처리 한 것들은 이미 시도한 것들이다. (스레드로 시작)
C++ 코드
UINT CNamedPipe::StartNamedPipeServer()
{
LPTSTR lpszPipename = "\\\\.\\pipe\\SAPipe";
HANDLE hPipe;
BOOL flg;
DWORD dwWrite,dwRead;
char szServerUpdate[200];
char szClientUpdate[200];
hPipe = CreateNamedPipe ( lpszPipename,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE |
PIPE_NOWAIT, //changed from nowait
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
PIPE_TIMEOUT, // client time-out
NULL); // no security attribute
if (hPipe == INVALID_HANDLE_VALUE)
return 0;
ConnectNamedPipe(hPipe, NULL);
while(m_bServerActive) //This seems to work well ....
{
//Read from client
flg = ReadFile(hPipe,szClientUpdate,strlen(szClientUpdate),&dwRead, NULL);
if(flg) //Read something from the client!!!!
{
CString csMsg,csTmp;
for(int i=0;i<dwRead;i++){
csTmp.Format("%c",szClientUpdate[i]);
csMsg += csTmp;
}
AfxMessageBox("Client message: " + csMsg);
strcpy(szServerUpdate,"busy");
//Write status to Client
flg = WriteFile(hPipe, szServerUpdate, strlen(szServerUpdate), &dwWrite, NULL);
EndServer();
StartServer();
}
}
return 0;
}
C# 코드 : StreamWriter
또는 StreamReader
폐기
public void ThreadStartClient(object obj)
{
// Ensure that we only start the client after the server has created the pipe
ManualResetEvent SyncClientServer = (ManualResetEvent)obj;
// Only continue after the server was created -- otherwise we just fail badly
// SyncClientServer.WaitOne();
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "SAPipe"))
{
// The connect function will indefinately wait for the pipe to become available
// If that is not acceptable specify a maximum waiting time (in ms)
pipeStream.Connect();
//Write from client to server
using (StreamWriter sw = new StreamWriter(pipeStream))
{
sw.WriteLine("What's your status?");
}
//Read server reply
/*using (StreamReader sr = new StreamReader(pipeStream))
{
string temp = "";
temp = sr.ReadLine(); //Pipe is already closed here ... why?
MessageBox.Show(temp);
}*/
//pipeStream.Close();
}
}
}
감사합니다. 큰 도움이됩니다. 코드를 변경하고 무슨 일이 일어나는 지 보겠습니다. 짐작 하셨겠지만 저는 C#을 처음 사용합니다. –
아무런 문제가 없습니다. 투표에서 수표를 누르는 것을 잊지 마십시오. 그러면 더 나은지지 등급을 얻게됩니다! – Blam