This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
Receive the messy code when using easyClient to transmit picture #90
Comments
不贴代码怎么帮你分析? |
江哥,你好:
不好意思,前端时间比较忙,没有来得及回复,之前的说的那个接受图片乱码的问题解决了,但是又出来一个新的问题,客户端接收到的图片不清晰,将客户端接收到的图片的byte[]以字符串的形式打印出来与服务端发送的byte[]比较,发现客户端接收到的要比服务端发送的byte[]少了最后面的几段byte[]不知道是不是我客户端接收代码写错了。
图片是分割成一段一段发送,每段都是6位头信息加图片的片段byte[],头信息前4位表示数据类型,为"p"或"ep"位数不够的前面用空格补全,后两位表示长度的byte[],由Int16类型转化而来
以下是我的客户端easyClient的代码
client = new EasyClient();
client.ReceiveBufferSize = 16384;
int bufferSize = client.ReceiveBufferSize;
// Initialize the client with the receive filter and request handler
client.Initialize(new MyReceiveFilter(), (request) =>
{
string keyStr = request.Key.Replace(" ", "");
switch (keyStr)
{
case "p":
//接收到图片片段(非最后一个)
picByteList.AddRange(request.Body);
break;
case "ep":
picByteList.AddRange(request.Body);
UnityEngine.Debug.Log("最终接收到图片大小为:" + picByteList.Count);
picHasGotten = true;//表示图片已接收完毕
pictureBuf=picByteList.ToArray();
picByteList.Clear();
break;
default:
break;
}
我的过滤器的代码:
class MyReceiveFilter : FixedHeaderReceiveFilter<BinaryPackageInfo>
{
int receiveLen=0;//记录接收到的数据片段的长度
public MyReceiveFilter()
: base(6)//6为接收的数据头信息的长度,前4位表示信息类型,后2位表示接收的数据片段的长度
{
}
/// <summary>
/// 获取传输的数据byte[]主体
/// </summary>
/// <param name="bufferStream">包含接收到的数据的byte[]</param>
/// <returns>BinaryPackageInfo实例</returns>
public override BinaryPackageInfo ResolvePackage(IBufferStream bufferStream)
{
UnityEngine.Debug.Log("receiveLength为"+ receiveLen);
string type = bufferStream.ReadString(4, Encoding.UTF8);
byte[] buffer = new byte[Size - 6];
bufferStream.Skip(2).Read(buffer, 0,Size-6);
return new BinaryPackageInfo(type, buffer);
}
protected override int GetBodyLengthFromHeader(IBufferStream bufferStream, int length)
{
List<byte> lenBytes = new List<byte>();
int ibuffer = bufferStream.Skip(4).ReadByte();//读取6位头信息中后两位的长度信息
lenBytes.Add((byte)ibuffer);
int ibuffer2 = bufferStream.ReadByte();
lenBytes.Add((byte)ibuffer2);//lenBytes中已存放长度的byte[]
Int16 len = BitConverter.ToInt16(lenBytes.ToArray(), 0);
receiveLen = len;
UnityEngine.Debug.Log("长度值:" + len);
lenBytes.Clear();
return len;
}
}
…--
Thanks and regards,
韩振洲 研发部 工程师
Han zhenzhou Research and Development Dept. Engineer
上海安托信息技术有限公司
ATOZ information technology (Shanghai) Ltd.
上海市闵行区顾戴路2337号丰树商业城B栋9楼B室
手机: +86 15751012531
E-mail:[email protected]
发件人:"waleswood" <[email protected]>
发送日期:2018-11-13 15:30:35
收件人:"kerryjiang/SuperSocket.ClientEngine" <[email protected]>
抄送人:hanzz0502 <[email protected]>,Author <[email protected]>
主题:Re: [kerryjiang/SuperSocket.ClientEngine] Receive the messy code when using easyClient to transmit picture (#90)
不贴代码怎么帮你分析?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
我用SuperSocket写了一个服务端,然后用easyClient写了两个客户端,然后两个客户端都连接上服务端,其中一个客户端通过服务端中转向另一个客户端发送图片,图片分段发送,但是发了几个片段后,客户端的GetBodyLengthFromHeader函数中的string strLen = Encoding.UTF8.GetString(bufferStream.Buffers[0].Array, 4, 10);的strLen变成了乱码,导致无法转化为数字(即无法获得片段的长度信息),该如何解决?一开始接收的几个片段是好的,能获得长度,但是从某个片段开始,突然就是乱码了。(使用的协议为:FixedHeaderReceiveFilter)
The text was updated successfully, but these errors were encountered: