簡體   English   中英

從NetworkStream讀取前6個字節?

[英]Read First 6 Bytes From NetworkStream?

我正在通過tcpclient / networkstream發送圖像(特別是屏幕截圖)。 為了在接收端正確讀取圖像字節,我需要知道圖像的長度。 我打算做的是將緩沖區的前6個字節保存為圖像大小(因為它似乎從未超過6個數字),然后從那里開始將緩沖區的其余部分作為圖像。 我遇到的問題是我無法僅讀取前6個字節。

服務器代碼

       int data = 0;

        byte[] readBuffer = new byte[getSelectedClient().ReceiveBufferSize];

        **data = stream.Read(readBuffer, 0, 5, readBuffer.Length);** <-- sort of thing im trying to do

        string pictureSize = Encoding.ASCII.GetString(readBuffer, 0, data);

        Console.WriteLine(pictureSize); //for debugging purposes

        string x = new Random().Next().ToString();

        FileStream f = new FileStream(x + ".bmp", FileMode.Create, FileAccess.Write);

        while (new FileInfo(x + ".bmp").Length != Convert.ToInt32(pictureSize))
        {
            **data = stream.Read(readBuffer, 6, readBuffer.Length);** <-- then it would read from the 6th byte, which would be the start of the image
            f.Write(readBuffer, 0, data);
        }

        f.Close();

        Process.Start(x + ".bmp");

        screenShotBTN.Enabled = true;

客戶代碼

MemoryStream ms = new MemoryStream();
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
               writeToStream(combineBytes(Encoding.ASCII.GetBytes(ms.Length.ToString()), ms.ToArray()));
ms.Close();


public static byte[] combineBytes(byte[] b1, byte[] b2)
    {
        byte[] b3 = new byte[b1.Length + b2.Length];
        Array.Copy(b1, 0, b3, 0, b1.Length);
        Array.Copy(b2, 0, b3, b1.Length, b2.Length);
        return b3;
    }

您要實現的是“長度前綴”消息傳遞。

要記住的事情是,當您從網絡流中讀取數據時,不一定會立即收到要查找的所有字節(大多數情況下,在本地測試時,所有內容都可以,但是您需要進行編程以確保可能在最壞的情況下只能接收1個字節)。

因此,您應該進入一種狀態(awaitinglength,awaitingpayload)。 然后,您在等待長度的同時開始讀取字節,然后在緩沖區中建立該數據。 一旦有了長度(在這種情況下為6個字節),就可以將狀態切換為awaitingpayload,然后將其讀取到輔助緩沖區中,直到擁有完整的有效負載,然后就可以開始使用了。

閱讀Stephen Cleary關於消息框架的文章可能是值得的。 通過閱讀這些內容,我對網絡的了解不多。

http://blog.stephencleary.com/2009/04/sample-code-length-prefix-message.html

我自己解決了這個問題。

服務器

int data = 0;

byte[] readBuffer = new byte[getSelectedClient().ReceiveBufferSize];

data = stream.Read(readBuffer, 0, 6); //only read first 6

string pictureSize = Encoding.ASCII.GetString(readBuffer, 0, data);

Console.WriteLine(pictureSize); //for debugging purposes

string x = new Random().Next().ToString();

FileStream f = new FileStream(x + ".bmp", FileMode.Create, FileAccess.Write);

while (new FileInfo(x + ".bmp").Length != Convert.ToInt32(pictureSize))
{
    data = stream.Read(readBuffer, 0, readBuffer.Length); 
    f.Write(readBuffer, 0, data);
}

f.Close();

Process.Start(x + ".bmp");

screenShotBTN.Enabled = true;

客戶代碼

MemoryStream ms = new MemoryStream();
Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Console.WriteLine(ms.Length.ToString());
writeToStream(combineBytes(Encoding.ASCII.GetBytes(ms.Length.ToString()), ms.ToArray()));
ms.Close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM