如何传递一个以上的字节
How to pass more than one byte
我有两个控制台应用程序可以通过 named pipes that can be downloaded from NuGet, I found a small example here 相互通信。
控制台应用程序 1:
static void Main(string[] args)
{
SendByteAndReceiveResponse();
}
private static void SendByteAndReceiveResponse()
{
using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("test-pipe"))
{
namedPipeServer.WaitForConnection();
namedPipeServer.WriteByte(1);
int byteFromClient = namedPipeServer.ReadByte();
Console.WriteLine(byteFromClient);
}
}
Consoleapp2:
static void Main(string[] args)
{
ReceiveByteAndRespond();
}
private static void ReceiveByteAndRespond()
{
using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream("test-pipe"))
{
namedPipeClient.Connect();
Console.WriteLine(namedPipeClient.ReadByte());
namedPipeClient.WriteByte(2);
}
}
我的问题:如何传递多个字节或多个变量?
可以使用Write
方法写入多个字节。问题是你不知道接收端的长度,所以你必须将它传递给服务器。
此代码用于服务器。它向客户端发送字节。首先它告诉有多少字节即将到来,然后它写入内容:
byte[] bytes = new byte[] { 1, 2, 3, 4 };
int length = bytes.Length;
byte[] lengthAsBytes = BitConverter.GetBytes(length);
namedPipeServer.Write(lengthAsBytes, 0, 4); // an int is four bytes
namedPipeServer.Write(bytes, 0, length);
然后另一边,先读长度,再读内容:
byte[] lengthAsBytes = new byte[4]; // an int is four bytes
namedPipeServer.Read(lengthAsBytes, 0, 4);
int length = BitConverter.ToInt32(lengthAsBytes, 0);
byte[] bytes = new byte[length];
namedPipeServer.Read(bytes, 0, length);
您可以使 byte
类型的数组用您想要的字节数对其进行初始化。然后在循环中使用 namedPipeServer.Write(byte[] buffer, int offset, int count )
来迭代所有数组元素。
我有两个控制台应用程序可以通过 named pipes that can be downloaded from NuGet, I found a small example here 相互通信。
控制台应用程序 1:
static void Main(string[] args)
{
SendByteAndReceiveResponse();
}
private static void SendByteAndReceiveResponse()
{
using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("test-pipe"))
{
namedPipeServer.WaitForConnection();
namedPipeServer.WriteByte(1);
int byteFromClient = namedPipeServer.ReadByte();
Console.WriteLine(byteFromClient);
}
}
Consoleapp2:
static void Main(string[] args)
{
ReceiveByteAndRespond();
}
private static void ReceiveByteAndRespond()
{
using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream("test-pipe"))
{
namedPipeClient.Connect();
Console.WriteLine(namedPipeClient.ReadByte());
namedPipeClient.WriteByte(2);
}
}
我的问题:如何传递多个字节或多个变量?
可以使用Write
方法写入多个字节。问题是你不知道接收端的长度,所以你必须将它传递给服务器。
此代码用于服务器。它向客户端发送字节。首先它告诉有多少字节即将到来,然后它写入内容:
byte[] bytes = new byte[] { 1, 2, 3, 4 };
int length = bytes.Length;
byte[] lengthAsBytes = BitConverter.GetBytes(length);
namedPipeServer.Write(lengthAsBytes, 0, 4); // an int is four bytes
namedPipeServer.Write(bytes, 0, length);
然后另一边,先读长度,再读内容:
byte[] lengthAsBytes = new byte[4]; // an int is four bytes
namedPipeServer.Read(lengthAsBytes, 0, 4);
int length = BitConverter.ToInt32(lengthAsBytes, 0);
byte[] bytes = new byte[length];
namedPipeServer.Read(bytes, 0, length);
您可以使 byte
类型的数组用您想要的字节数对其进行初始化。然后在循环中使用 namedPipeServer.Write(byte[] buffer, int offset, int count )
来迭代所有数组元素。