[問題] tcpClient接收NetworkStream的問題

作者: unstoppable (BEN)   2014-07-30 00:22:41
byte[] Receivebytes = new byte[0];
int ReceivebyteRead = 0; //單次接收的長度
int BytesMergeIndex = 0; //要合併bytes的位置
NetworkStream NS = tcpclient.GetStream();
if(NS.CanRead)
{
do
{
byte[] TempBytes = new byte[tcpClient.ReceiveBufferSize];
ReceivebyteRead = NS.Read(TempBytes, 0, tcpClient.ReceiveBufferSize);
Array.Resize<byte>(ref Receivebytes,
Receivebytes.Length + ReceivebyteRead);
Array.ConstrainedCopy(TempBytes, 0, Receivebytes,
BytesMergeIndex, ReceivebyteRead);
BytesMergeIndex += ReceivebyteRead;
Thread.Sleep(200);
}while(NS.DataAvailable)
BinaryFormatter BF = new BinaryFormatter();
MemoryStream MS = new MemoryStream(Receivebytes);
Data_List = (List<object>)BF.Deserialize(MS);
}
作者: Abbee (阿比)   2014-07-30 23:54:00
重傳?
作者: a926 (Aaron)   2014-07-31 08:56:00
把內容大小傳過去Client端 Clinet比對目前接收的內容大小如果不符合就繼續等資料傳完,或是重傳。
作者: EricTsai (dream of you...)   2014-07-31 21:51:00
請handle NS.read的相關exception

Links booklink

Contact Us: admin [ a t ] ucptt.com