当前位置:网站首页>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 ");
}
}
}
}边栏推荐
- Which financial products will yield high returns in 2022?
- npm uninstall和rm直接删除的区别
- Node:504 error reporting
- 谈谈制造企业如何制定敏捷的数字化转型策略
- #DAYU200体验官#MPPT光伏发电项目 DAYU200、Hi3861、华为云IotDA
- [JDBC Part 1] overview, get connection, CRUD
- Pdf document signature Guide
- 客户案例|华律网,通过观测云大幅缩短故障定位时间
- How to close eslint related rules
- Remember aximp once Use of exe tool
猜你喜欢

Time standard library

NVR hard disk video recorder is connected to easycvr through the national standard gb28181 protocol. What is the reason why the device channel information is not displayed?

Cv2.resize function reports an error: error: (-215:assertion failed) func= 0 in function ‘cv::hal::resize‘

The strongest installation of the twin tower model, Google is playing "antique" again?

L'enregistreur de disque dur NVR est connecté à easycvr par le Protocole GB 28181. Quelle est la raison pour laquelle l'information sur le canal de l'appareil n'est pas affichée?

嵌入式开发:如何为项目选择合适的RTOS?

Ueeditor custom display insert code

Firefox browser installation impression notes clipping

【JDBC Part 1】概述、获取连接、CRUD

It's worth seeing. Interview sites and interview skills
随机推荐
Talk about relational database and serverless
[开源] .Net ORM 访问 Firebird 数据库
ByteDance Android interview, summary of knowledge points + analysis of interview questions
Revit secondary development - project file to family file
How to turn on win11 game mode? How to turn on game mode in win11
如何实现横版游戏中角色的移动控制
Overseas agent recommendation
UWA问答精选
Typeorm automatically generates entity classes
客户案例|华律网,通过观测云大幅缩短故障定位时间
Firefox browser installation impression notes clipping
Get the exact offset of the element
How to write an augmented matrix into TXT file
Attitude estimation (complementary filtering)
Use partial derivatives to display normals in unity
怎样写一个增广矩阵到txt文件中
【JDBC Part 1】概述、获取连接、CRUD
null == undefined
TCP/IP 协议栈
Two methods of calling WCF service by C #