当前位置:网站首页>C development - interprocess communication - named pipeline
C development - interprocess communication - named pipeline
2022-07-07 22:23:00 【Hey, hey, hey, hey, hey】
Simple named pipes Demo
client : Input 1+1 Click Send , take 1+1 To the server
Server side : After the operation, the result is returned to the client
Here is the core code , complete Demo Click here
client:
private void sendMsgToServer()
{
string _name = "sendMsgToServer";
using (var client = new NamedPipeClientStream(".", m_sPipeName, PipeDirection.InOut))
{
try
{
client.Connect(2000);
Helper.LogHelper.Log_Info(m_sClassName + _name, " Successfully connected to the server , Pipe name :" + m_sPipeName);
using (var pipe = new Helper.PipeStream(client))
{
Helper.LogHelper.Log_Info(m_sClassName + _name, " Start sending and receiving messages circularly , Pipe name :" + m_sPipeName);
while (!m_bClosePipe)
{
if (m_bSend)
{
m_bSend = !m_bSend;
m_bReceive = !m_bReceive;
this.Dispatcher.Invoke(delegate ()
{
Helper.MsgModel msg = GetMsg();
if (msg != null)
{
string sendMsg = JsonConvert.SerializeObject(msg);
pipe.Send(sendMsg);
Helper.LogHelper.Log_Info(m_sClassName + _name, " Send a message to the server , Pipe name :" + m_sPipeName);
}
});
}
if (m_bReceive)
{
m_bReceive = !m_bReceive;
string receive = pipe.Receive();
if (!string.IsNullOrEmpty(receive))
{
MessageBox.Show(receive, " Received a message from the server ");
Helper.LogHelper.Log_Info(m_sClassName + _name, " Received a message from the server , Pipe name :" + m_sPipeName);
}
}
}
string closeMsg = JsonConvert.SerializeObject(null);
pipe.Send(closeMsg);
Helper.LogHelper.Log_Info(m_sClassName + _name, " Send a close pipeline message to the server , Pipe name :" + m_sPipeName);
}
client.Close();
Helper.LogHelper.Log_Info(m_sClassName + _name, " Client pipeline closed , Pipe name :" + m_sPipeName);
MessageBox.Show(" The client's current named pipe is closed ", " client ");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), " client ");
Helper.LogHelper.Log_Error(m_sClassName + _name, " error :" + ex.Message.ToString());
}
}
}
server:
private void listenClient()
{
while (!m_bClose)
{
using (m_PipeServer = new NamedPipeServerStream(m_sPipeName, PipeDirection.InOut, m_iMaxNumber))
{
try
{
m_PipeServer.WaitForConnection();
using (m_pipeStream = new Helper.PipeStream(m_PipeServer))
{
while (true)
{
string msgJson = m_pipeStream.Receive();
if (msgJson != "null")
{
Helper.MsgModel msg = JsonConvert.DeserializeObject<Helper.MsgModel>(msgJson);
double number1 = Convert.ToDouble(msg.number1);
double number2 = Convert.ToDouble(msg.number2);
switch (msg.type)
{
case "+":
DoMain_A(number1, number2);
break;
case "-":
DoMain_B(number1, number2);
break;
case "*":
DoMain_C(number1, number2);
break;
case "/":
DoMain_D(number1, number2);
break;
}
m_PipeServer.RunAsClient(SendToClent);
}
else
{
break;
}
}
}
m_PipeServer.Close();
MessageBox.Show(" The current named pipeline of the server is closed "," The server ");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString(), " The server ");
}
}
}
}
边栏推荐
- How to turn on win11 game mode? How to turn on game mode in win11
- null == undefined
- Jerry's about TWS pairing mode configuration [chapter]
- Jerry's configuration of TWS cross pairing [article]
- Antd date component appears in English
- Firefox browser installation impression notes clipping
- 三元表达式、各生成式、匿名函数
- Vs custom template - take the custom class template as an example
- How to realize the movement control of characters in horizontal game
- Remember that a development is encountered in the pit of origin string sorting
猜你喜欢
ByteDance Android interview, summary of knowledge points + analysis of interview questions
PKPM 2020软件安装包下载及安装教程
Anti climbing killer
使用 BlocConsumer 同时构建响应式组件和监听状态
Px4 autonomous flight
Firefox browser installation impression notes clipping
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
Win11如何解禁键盘?Win11解禁键盘的方法
Use json Stringify() to realize deep copy, be careful, there may be a huge hole
Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘
随机推荐
An in-depth understanding of fp/fn/precision/recall
What if the win11u disk does not display? Solution to failure of win11 plug-in USB flash disk
Jerry's key to initiate pairing [chapter]
Node:504 error reporting
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
Jerry's manual matching method [chapter]
Dayu200 experience officer MPPT photovoltaic power generation project dayu200, hi3861, Huawei cloud iotda
The strongest installation of the twin tower model, Google is playing "antique" again?
PDF文档签名指南
EasyCVR配置中心录像计划页面调整分辨率时的显示优化
强化学习-学习笔记9 | Multi-Step-TD-Target
The function is really powerful!
Main functions of OS, Sys and random Standard Libraries
Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
如何实现横版游戏中角色的移动控制
反爬通杀神器
NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?
Dbsync adds support for mongodb and ES
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
Programming mode - table driven programming