gpt4 book ai didi

c# - 无法获取要通过 NamedPipeServerStream 和 NamedPipeClientStream 传递的字符串数据

转载 作者:行者123 更新时间:2023-12-02 13:37:08 24 4
gpt4 key购买 nike

我正在尝试使用两个简单的 C# 表单解决方案在我的 Win-XP 工作站上实现双向命名管道通信。一种用于客户端,一种用于服务器。它们看起来几乎相同,并使用 NamedPipeServerStream 和 NamedPipeClientStream (.NET 3.5)。客户端和服务器均通过 PipeDirection.InOut

设置为双向通信

启动事件的顺序是:1)启动服务器。它等待来自客户端的连接。2)启动客户端,它立即找到并连接到服务器。同样,服务器完成与客户端的连接。3) 客户端和服务器都启动它们的“Read”线程,这些线程又创建streamreader 的实例。然后这些线程调用 ReadLn() 并阻塞等待数据。在所有情况下,自动刷新都是 true。

然后,我使用streamwriter.WriteLn() 将字符串数据从服务器发送到客户端(反之亦然)。但是,执行永远不会从该调用返回。我不知道为什么,任何见解都会受到热烈欢迎。

我花了相当多的时间研究这个主题的所有内容,但我仍然遗漏了一些东西。

显示了客户端和服务器代码片段:

服务器:

   private void ListenForClients()
{
// Only one server as this will be a 1-1 connection
m_pipeServerStream = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1);

// Wait for a client to connect
m_pipeServerStream.WaitForConnection();

// Ccould not create handle - server probably not running
if (!m_pipeServerStream.IsConnected)
return;

// Create a stream writer which flushes after every write
m_pipeServerWriter = new StreamWriter(m_pipeServerStream);
m_pipeServerWriter.AutoFlush = true;

Connected = true;

// Start listening for messages
if (m_pipeServerStream.CanRead)
{
ReadThread = new Thread(new ParameterizedThreadStart(Read));
ReadThread.Start(m_pipeServerStream);
}
}

/// <summary>
/// Reads data from the client
/// </summary>
/// <param name="serverObj"></param>
private void Read(object serverObj)
{
NamedPipeServerStream pipeStream = (NamedPipeServerStream)serverObj;
using (StreamReader sr = new StreamReader(pipeStream))
{
while (true)
{
string buffer;
try
{
buffer = sr.ReadLine();
}
catch
{
//read error has occurred
break;
}

//client has disconnected
if (buffer.Length == 0)
break;

//fire message received event
if (MessageReceived != null)
{
MessageReceived(buffer);
}
}
}
}

/// <summary>
/// Sends a message to the connected client
/// </summary>
/// <param name="message">the message to send</param>
public void SendMessage(string message)
{
if (m_pipeServerWriter != null)
{
m_pipeServerWriter.WriteLine(message);
m_pipeServerWriter.Flush();
}
}

客户:

   private void ConnectToServer()
{
// Seek out the one server
m_pipeClientStream = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut);

// Connect to the waiting server
m_pipeClientStream.Connect();

// Ccould not create handle - server probably not running
if (!m_pipeClientStream.IsConnected)
return;

// Create a stream writer which flushes after every write
m_pipeClientWriter = new StreamWriter(m_pipeClientStream);
m_pipeClientWriter.AutoFlush = true;

Connected = true;

// Start listening for messages
if (m_pipeClientStream.CanRead)
{
ReadThread = new Thread(new ParameterizedThreadStart(Read));
ReadThread.Start(m_pipeClientStream);
}
}

/// <summary>
/// Reads data from the server
/// </summary>
private void Read(object serverObj)
{
NamedPipeClientStream pipeStream = (NamedPipeClientStream)serverObj;
using (StreamReader sr = new StreamReader(pipeStream))
{
while (true)
{
string buffer;
try
{
buffer = sr.ReadLine();
}
catch
{
//read error has occurred
break;
}

//client has disconnected
if (buffer.Length == 0)
break;

//fire message received event
if (MessageReceived != null)
{
MessageReceived(buffer);
}
}
}
}

/// <summary>
/// Sends a message to the connected server
/// </summary>
/// <param name="message"></param>
public void SendMessage(string message)
{
if (m_pipeClientWriter != null)
{
m_pipeClientWriter.WriteLine(message);
m_pipeClientWriter.Flush();
}
}

最佳答案

尝试在流上设置异步标志:

NamedPipeClientStream(".", PipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

关于c# - 无法获取要通过 NamedPipeServerStream 和 NamedPipeClientStream 传递的字符串数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2975456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com