gpt4 book ai didi

c# - 使用命名管道的两种 C++ 到 C# 通信

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:03 25 4
gpt4 key购买 nike

我正在尝试在 VC++ 6 应用程序和 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#代码:

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();

}
}
}

最佳答案

处置 StreamWriterStreamReader 将关闭底层流。

因此,您的 using 语句将导致流关闭。

    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
StreamWriter sw = new StreamWriter(pipeStream))
sw.WriteLine("What's your status?");

//Read server reply
StreamReader sr = new StreamReader(pipeStream)
string temp = "";
temp = sr.ReadLine(); //Pipe is already closed here ... why?

MessageBox.Show(temp);
}
}

还应注意,因为您将流包装在 using 语句中,所以不需要注释掉的 pipeStream.Close() 函数。

关于c# - 使用命名管道的两种 C++ 到 C# 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10740479/

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