c# - 如何使用命名管道在 C# 和 MQL5 之间读取和写入消息
问题描述
我正在尝试在 C# 命名管道服务器和 MQL5 命名管道客户端之间建立一个通信通道。命名管道服务器和客户端正在成功创建,并且客户端也连接到服务器。从客户端向服务器发送消息,从服务器成功读取数据。但问题是当从服务器向客户端发送消息时,客户端无法读取数据。或者不确定服务器是否成功发送数据。
这是创建 C# 命名管道服务器的服务器源。
namespace PipeServer
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!!!");
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("TestPipeServer", PipeDirection.InOut, 1, PipeTransmissionMode.Byte))
{
Console.WriteLine("NamedPipeServerStream object created.");
// Wait for a client to connect
Console.Write("Waiting for client connection...");
pipeServer.WaitForConnection();
Console.WriteLine("Client connected.");
Console.WriteLine("Read client's response");
const int BUFFERSIZE = 256;
bool completed = false;
while (!completed)
{
byte[] buffer = new byte[BUFFERSIZE];
int nRead = pipeServer.Read(buffer, 0, BUFFERSIZE);
string line = Encoding.UTF8.GetString(buffer, 0, nRead);
Console.WriteLine(line);
if (line == "bye")
completed = true;
}
Console.WriteLine("Send response");
byte[] msgAck = Encoding.UTF8.GetBytes("ACK from server");
pipeServer.Write(msgAck, 0, msgAck.Length);
pipeServer.Flush();
pipeServer.WaitForPipeDrain();
}
Console.WriteLine("\npress any key to exit the process...");
// basic use of "Console.ReadKey()" method
Console.ReadKey();
}
}
}
这是用 MQL5 编写的客户端源代码,用于 MT5 作为 EA
//+------------------------------------------------------------------+
//| TestEA.mq5 |
//| Copyright 2021, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include <Files\FilePipe.mqh>
CFilePipe ExtPipe;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(10);
while(!IsStopped())
{
if(ExtPipe.Open("\\\\.\\pipe\\TestPipeServer",FILE_READ|FILE_WRITE|FILE_BIN)!=INVALID_HANDLE)
break;
Sleep(250);
}
Print("Client: pipe opened");
//--- send welcome message
if(!ExtPipe.WriteString(__FILE__+" on MQL5 build "+IntegerToString(__MQ5BUILD__)))
{
Print("Client: sending welcome message failed");
return(INIT_FAILED);
}
else
{
ExtPipe.WriteString("bye");
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
ExtPipe.Close();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//printf(__FUNCTION__+": method called");
MqlTick last_tick;
string sendStr;
if(SymbolInfoTick(Symbol(), last_tick))
{
sendStr = StringFormat("Symbol: %s, Bid: %f, Ask: %f", Symbol(), last_tick.bid, last_tick.ask);
//printf(sendStr);
}
else
{
printf("SymbolInfoTick() failed, error = ",GetLastError());
}
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//printf(__FUNCTION__ + ": method called");
//--- read data from server
string str;
if(!ExtPipe.ReadString(str))
{
Print("Client: reading string failed");
return;
}
Print("Server: ",str," received");
}
//+------------------------------------------------------------------+
//| Trade function |
//+------------------------------------------------------------------+
void OnTrade()
{
//---
}
//+------------------------------------------------------------------+
谷歌了一下,但没有找到任何合适的解决方案。如果有类似的问题,请分享。谢谢。
解决方案
推荐阅读
- java - 这个片段在junit中的含义是什么?
- node.js - npm 5.8.0 不支持 Ubuntu 19.04 上的 Node.js v10.15.2
- python - 比较两个列表的函数出现越界错误
- php - SQL/PHP 中是否有一种快速的方法可以让所有用户拥有所有分支权限?
- python - 在python中建造*塔
- python - 如何在 GridSearchCV 中正确选择最佳模型 - sklearn 和 caret 都做错了
- git - Git 合并状态“已经是最新的”,提交在 master 但更改基本上在下一次提交时被覆盖
- swift - 为什么不建议将 UIImage 数据保存到 UserDefaults?
- php - 子文件夹 htaccess 的 RewriteRule
- c# - 如何在 Asp.Net Core 应用程序中配置 MassTransit Saga