簡體   English   中英

tcp ip客戶端讀取接收到的數據c#

[英]tcp ip client read received data c#

TcpClient client = new(SERVER_IP, PORT_NO);
        NetworkStream stream = client.GetStream();
        stream.Write(WriteArry, 0, WriteArry.Length);
        
        byte[] data = new byte[10];
        
        int recivedbyte = stream.Read(data, 0, 10);

如何檢查接收數據的長度是否小於 10 等待剩余數據? 或多次收到數據? 謝謝

byte[] data = new byte[10];

int read, remaining = data.Length;
while (remaining > 0 && (read =
    stream.Read(data, data.Length - remaining, remaining)) > 0)
{
    remaining -= read;
}
if (remaining != 0) throw new EndOfStreamException(); // EOF

暫無
暫無

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

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