问题描述
我正在尝试在 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,BUFFERSIZE);
string line = Encoding.UTF8.GetString(buffer,nRead);
Console.WriteLine(line);
if (line == "bye")
completed = true;
}
Console.WriteLine("Send response");
byte[] msgAck = Encoding.UTF8.GetBytes("ACK from server");
pipeServer.Write(msgAck,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()
{
//---
}
//+------------------------------------------------------------------+
用谷歌搜索了一下,但没有找到任何合适的解决方案。 如果有任何类似的问题,请分享。 谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)