110,893
社区成员
发帖
与我相关
我的任务
分享
internal bool SendByteArray(byte[] buffer, NetworkStream stream)
{
if (buffer.Length == 0) return false;
try
{
WriteSize(stream, buffer.Length);
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
return true;
}
catch (Exception)
{
return false;
}
}
internal byte[] ReceiveByteArray(NetworkStream stream)
{
int bufferlen = GetSize(stream);
byte[] resultbyte = new byte[bufferlen];
for (int i = 0; i < bufferlen; i++)
{
resultbyte.SetValue((byte)stream.ReadByte(), i);//每次顺序读取一个字节
}
return resultbyte;
}
internal byte[] ReceiveByteArray(NetworkStream stream)
{
int bufferlen = GetSize(stream);
byte[] resultbyte = new byte[bufferlen];
stream.Read(resultbyte, 0, resultbyte.Length);
return resultbyte;
}
internal byte[] ReceiveByteArray(NetworkStream stream)
{
try
{
int bufferlen = GetSize(stream);
byte[] resultbyte = new byte[bufferlen];
stream.Read(resultbyte, 0, resultbyte.Length);
return resultbyte;
byte[] data = new byte[1024];
int index = 0, bytesread = 0;
//int r = data.Length;
while (index < bufferlen && (bytesread = stream.Read(data, 0, data.Length)) > 0)
{
//r = bufferlen - index >= 1024 ? 1024 : bufferlen - index;
data.CopyTo(resultbyte, index);
index += bytesread;
}
return resultbyte;
}
catch (Exception)
{
throw;
}
static byte[] ReadBuffer(NetworkStream stream, int length)
{
byte[] buffer = new byte[length];
int index = 0;
do
{
int len = stream.Read(buffer, index, length - index);
index += len;
} while (index < length);
return buffer;
}
private int GetSize(NetworkStream stream)
{
int count = 0;
try
{
count = BitConverter.ToInt32(ReadBuffer(stream, _messageLength), 0);
}
catch (Exception ex)
{
Logger.ExceptionLog(ex, "");
}
return count;
}
internal byte[] ReceiveByteArray(NetworkStream stream)
{
int len = GetSize(stream);
return ReadBuffer(stream, len);
}
internal byte[] ReceiveByteArray(NetworkStream stream)
{
try
{
int bufferlen = GetSize(stream);
byte[] resultbyte = new byte[bufferlen];
int offset = 0, bytesread = 0;
while (offset < bufferlen)
{
bytesread = stream.Read(resultbyte, offset, bufferlen - offset);
if (bytesread == 0)
throw new Exception("网络异常断开,数据读取不完整。");
else
offset += bytesread;
}
return resultbyte;
}
catch (Exception)
{
throw;
}
}
internal byte[] ReceiveByteArray(NetworkStream stream)
{
int bufferlen = GetSize(stream);
byte[] resultbyte = new byte[bufferlen];
int recieveLength = 0;
READ:
int rec = stream.Read(resultbyte, recieveLength, bufferLen - receiveLength);
if(rec == 0) {
//网络断了,关闭
stream.Close();
return null;
}
recieveLength += rec;
if(recieveLength < bufferLen) {
goto READ;
}
return resultbyte;
}
internal byte[] ReceiveByteArray(NetworkStream stream)
{
try
{
int bufferlen = GetSize(stream);
byte[] resultbyte = new byte[bufferlen];
byte[] data = new byte[1024];
int index = 0, bytesread = 0;
while (index < bufferlen && (bytesread = stream.Read(data, 0, data.Length)) != 0)
{
data.CopyTo(resultbyte, index);
index += bytesread;
}
return resultbyte;
}
catch (Exception)
{
throw;
}
}
private void WriteSize(NetworkStream stream, int size)
{
byte[] length = new byte[_messageLength];
BitConverter.GetBytes(size).CopyTo(length, 0);
stream.Write(length, 0, _messageLength);
}
private int GetSize(NetworkStream stream)
{
int count = 0;
byte[] countBytes = new byte[_messageLength];
try
{
if (stream.Read(countBytes, 0, _messageLength) == _messageLength)
{
count = BitConverter.ToInt32(countBytes, 0);
}
else
{
return 0;
}
}
catch (Exception ex)
{
Logger.ExceptionLog(ex, "");
}
return count;
}
byte[] allstreambyte = new byte[GetSize(stream)];
byte[] buffer=new byte[1024];
int num=0;
while(readstream.Read(buffer, 0, buffer.Length)>0)
{
for (int i = 0; i < buffer.Length; i++)
{
allstreambyte[num * buffer.Length + i] = buffer[i];
}
num++;
}