双向C++;使用命名管道进行C#通信
我正在尝试在VC++6应用程序和C#应用程序之间进行双向通信。我正在使用命名管道。在我的C++代码中,我可以读取来自C客户端的消息,但是服务器“死亡”,我必须重新启动它。p> 我想做的是让C++应用程序连接到C++应用程序,请求一个状态,C++应用程序关闭并检查状态,然后返回“忙”或“空闲”。 我无法向C#客户端回写任何内容,因为它说连接已关闭。我已经注释掉的一些东西是我已经尝试过的 C++代码(作为线程启动)双向C++;使用命名管道进行C#通信,c#,c++,visual-c++,named-pipes,C#,C++,Visual C++,Named Pipes,我正在尝试在VC++6应用程序和C#应用程序之间进行双向通信。我正在使用命名管道。在我的C++代码中,我可以读取来自C客户端的消息,但是服务器“死亡”,我必须重新启动它。p> 我想做的是让C++应用程序连接到C++应用程序,请求一个状态,C++应用程序关闭并检查状态,然后返回“忙”或“空闲”。 我无法向C#客户端回写任何内容,因为它说连接已关闭。我已经注释掉的一些东西是我已经尝试过的 C++代码(作为线程启动) 处理StreamWriter或StreamReader将关闭底层流 因此,您的usi
处理
StreamWriter
或StreamReader
将关闭底层流
因此,您的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#客户:我实际上没有从客户端向服务器发送任何内容,因为我不需要。。。其中的关键是CreateNamedPipe()函数中的PIPE_WAIT参数。这会使服务器等待客户端连接。谢谢,这是一个很大的帮助。我会修改代码,看看会发生什么。正如你可能已经猜到的,我是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();
}
}
}
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);
}
}
UINT CNamedPipe::StartNamedPipeServer()
{
if(!m_bServerActive)
return 0;
LPTSTR lpszPipename = "\\\\.\\pipe\\MyPipe";
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_WAIT, //HAS TO BE THIS
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);
strcpy( szServerUpdate,"busy");
//Write status to Client
flg = WriteFile(hPipe, szServerUpdate, strlen(szServerUpdate), &dwWrite, NULL);
EndServer();
StartServer();
return 0;
}
public void ThreadStartClient(object obj)
{
// Ensure that we only start the client after the server has created the pipe
ManualResetEvent SyncClientServer = (ManualResetEvent)obj;
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "MyPipe", PipeDirection.InOut))
{
// 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();
if (!pipeStream.IsConnected) //It thinks it's connected but can't read anything ....
{
MessageBox.Show("Failed to connect ....");
return;
}
//Read server reply
StreamReader sr = new StreamReader(pipeStream);
char[] c = new char[200];
while (sr.Peek() >= 0)
{
sr.Read(c, 0, c.Length);
}
string s = new string(c);
MessageBox.Show(s);
}
}