我想通過兩臺計算機之間的命名管道發送一些字節。客戶端發送65537字節,並返回PipeStream.Write()
,就像字節被髮送一樣,但服務器從不接收這些字節。爲什麼客戶對我說謊?我想我可以發送我的字節大塊,但在PipeStream.Write
會爲我做這件事的印象。其中一臺機器是虛擬的,一臺是物理的。如果客戶端發送少一個字節(65536字節)的服務器將全部接收它們。在另一種情況下,如果兩臺機器都是物理的,那麼所有65537字節也會收到。這裏是重現行爲的代碼。在另一臺機器上的一個機器和test.exe 65537 other.comp.name
上運行test.exe 100000 -
:寫入很多網絡管道時字節丟失
class Program
{
const string PipeName = "TestPipe";
static void Main(string[] args)
{
int bufferSize = int.Parse(args[0]);
if (args[1] == "-")
{
using (var server = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1,
PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
server.WaitForConnection();
byte[] result = new byte[bufferSize];
for (int offset = 0; offset < bufferSize;)
{
IAsyncResult asyncResult = server.BeginRead(result, offset, bufferSize - offset, null, null);
asyncResult.AsyncWaitHandle.WaitOne();
int bytesRead = server.EndRead(asyncResult);
if (bytesRead == 0)
{
Console.WriteLine("Client closed the pipe after {0} bytes.", offset);
return;
}
offset += bytesRead;
}
Console.WriteLine("All bytes are read.");
}
}
else
{
using (var client = new NamedPipeClientStream(args[1], PipeName, PipeDirection.InOut, PipeOptions.Asynchronous))
{
client.Connect(1000);
byte[] buffer = new byte[bufferSize];
client.Write(buffer, 0, buffer.Length);
client.WaitForPipeDrain();
Console.WriteLine("All bytes are written.");
}
}
}
}
這聲響起來,有一些虛擬機具有64KB管道緩衝區限制。不知道爲什麼我們必須猜測風味,也許[這有助於](http://home.gna.org/pysfst/tests/pipe-limit.html)。只需修補內核:-P。 4096永遠是一個很好的I/O緩衝區大小。 –