c#和c++之间的异步管道
Async pipe between C# and C++
我试图在c#(服务器)和c++(客户端)之间发送数据。现在我只能在它们之间成功发送一个数据。我如何从c#异步(实时)发送多个值到c++ ?
c#服务器using (NamedPipeServerStream PServer1 =
new NamedPipeServerStream("MyNamedPipe", PipeDirection.InOut))
{
Console.WriteLine("Server created");
Console.WriteLine("Waiting for client connection...");
PServer1.WaitForConnection();
Console.WriteLine("Client conencted");
try
{
// Read user input and send that to the client process.
using (StreamWriter sw = new StreamWriter(PServer1))
{
sw.AutoFlush = true;
Console.Write("Enter text: ");
sw.WriteLine(Console.ReadLine());
}
}
// Catch the IOException that is raised if the pipe is broken
// or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
//PServer1.Close();
}
c++客户HANDLE hPipe;
//Connect to the server pipe using CreateFile()
hPipe = CreateFile(
g_szPipeName, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
if (INVALID_HANDLE_VALUE == hPipe)
{
printf("nError occurred while connecting"
" to the server: %d", GetLastError());
return 1; //Error
}
else
{
printf("nCreateFile() was successful.");
}
//Read server response
char szBuffer[BUFFER_SIZE];
DWORD cbBytes;
BOOL bResult = ReadFile(
hPipe, // handle to pipe
szBuffer, // buffer to receive data
sizeof(szBuffer), // size of buffer
&cbBytes, // number of bytes read
NULL); // not overlapped I/O
if ( (!bResult) || (0 == cbBytes))
{
printf("nError occurred while reading"
" from the server: %d", GetLastError());
CloseHandle(hPipe);
return 1; //Error
}
else
{
printf("nReadFile() was successful.");
}
printf("nServer sent the following message: %s", szBuffer);
//CloseHandle(hPipe);
如果要发送多个消息,则必须在客户端和服务器中设置一个循环。
另外,请注意,由于using
语句,正在调用Pipe的Dispose()
方法并因此关闭它。因此,您必须在using
块内设置循环。
比如:
(服务端)using(var sw = new StreamWriter(PServer1))
{
sw.AutoFlush = true;
while(Condition) // This could be done by analyzing the user's input and looking for something special...
{
Console.Write("Enter text: ");
sw.WriteLine(Console.ReadLine());
}
}
相关文章:
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- IPC使用多个管道和分支进程来运行Python程序
- 如何创建函数管道,以便函数一个接一个地运行?
- Gstreamer 管道从命令 lne 到 c 代码
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 如何在C++中实现带有packaged_task的异步等待循环?
- 将旧管道转换为现代 openGL 时出现问题
- 创建 Spdlog 异步文件记录器时遇到困难
- C ++异步键盘输入(标准方式)
- 如何使用管道在父级和子级之间来回传递文件
- 在没有管理员权限的情况下连接到同一网络中的命名管道
- 如何测量管道延迟?
- 我如何使用此程序管道多个命令?C++
- 在 gtkmm 中异步加载图像
- 先进先出:一个进程永远不会从管道读取
- Node.js fs.open() 在尝试打开 4 个以上的命名管道 (FIFO) 后挂起
- 带有 Boost.Beast 的异步读取标头
- 未收到C++Windows异步IO命名管道的第一条消息
- c#和c++之间的异步管道