当前位置:网站首页>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 ");
}
}
}
}
边栏推荐
- 用语雀写文章了,功能真心强大!
- Preparing for the interview and sharing experience
- Tcp/ip protocol stack
- VTOL in Px4_ att_ Control source code analysis [supplement]
- Cannot find module 'xxx' or its corresponding type declaration
- 使用 BlocConsumer 同时构建响应式组件和监听状态
- [open source] Net ORM accessing Firebird database
- The whole network "chases" Zhong Xuegao
- Have you ever been confused? Once a test / development programmer, ignorant gadget C bird upgrade
- OpeGL personal notes - lights
猜你喜欢
OpenGL configuration vs2019
PKPM 2020软件安装包下载及安装教程
Two kinds of updates lost and Solutions
Display optimization when the resolution of easycvr configuration center video recording plan page is adjusted
It's worth seeing. Interview sites and interview skills
null == undefined
The strongest installation of the twin tower model, Google is playing "antique" again?
vite Unrestricted file system access to
How to make agile digital transformation strategy for manufacturing enterprises
建立自己的网站(18)
随机推荐
Embedded development: how to choose the right RTOS for the project?
Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
Crawler (17) - Interview (2) | crawler interview question bank
Interview question 01.02 Determine whether it is character rearrangement - auxiliary array algorithm
An in-depth understanding of fp/fn/precision/recall
The whole network "chases" Zhong Xuegao
Anti climbing killer
Use json Stringify() to realize deep copy, be careful, there may be a huge hole
Matplotlib快速入门
Revit secondary development - modify wall thickness
Use blocconsumer to build responsive components and monitor status at the same time
Matplotlib drawing interface settings
Jerry's key to initiate pairing [chapter]
Two methods of calling WCF service by C #
Pre sale 179000, hengchi 5 can fire? Product power online depends on how it is sold
Remember aximp once Use of exe tool
Oracle advanced (VI) Oracle expdp/impdp details
How to close eslint related rules
vite Unrestricted file system access to
客户案例|华律网,通过观测云大幅缩短故障定位时间