[问题] 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