首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

Socket+AMF3粘包有关问题

2012-06-26 
Socket+AMF3粘包问题问题:服务端Mina 前台接收Flex? ?参照的原型为http://www.klstudio.com/post/202.html

Socket+AMF3粘包问题

问题:

服务端Mina 前台接收Flex? ?
参照的原型为http://www.klstudio.com/post/202.html
<[????? this.isReadHead = true;
??? }
??? //如果读了头部,并且当前可读长度大于等于消息长度,则开始读取
??? if (isReadHead && this.sk.bytesAvailable >= this.msgLen)
????? {
????? var objByte:ByteArray = new ByteArray();
????? sk.readBytes(objByte, 0, this.msgLen);
????? this.isReadHead = false;
????? //objByte.uncompress();
????? var obj:Object = objByte.readObject();
????? this.readMsg(obj);?? //读完了,可以去解释了
????? }
??? //如果是读过头,则如果当前消息大于消息长度则再次调用读取,否则则判断是否可读头部
????? this.sk.bytesAvailable > 0 && this.readData();
????????????
?? }?

至于server端,其原理是一样的,都是读的时候先读头,发的时候也是先发个头。


补充server端代码(C#,解码类为FluorineFx,仅供参考思路)

server -> as3 发送

??? public void send(object obj)
??????????? {
??????????????? try
??????????????? {
??????????????????? ByteArray objByte = new ByteArray();
??????????????????? objByte.WriteObject(obj);
?????????????????? // objByte.Compress();
??????????????????? byte[] objBuff = new byte[objByte.Length];
??????????????????? objByte.Position = 0;
??????????????????? objByte.ReadBytes(objBuff, (uint)0, (uint)objBuff.Length);
?????????????????
??????????????????? ByteArray msgByte = new ByteArray();
??????????????????? msgByte.WriteInt(objBuff.Length);
??????????????????? msgByte.WriteBytes(objBuff, 0, objBuff.Length);
??????????????????? byte[] msgBuff = new byte[msgByte.Length];
??????????????????? msgByte.Position = 0;
??????????????????? msgByte.ReadBytes(msgBuff, (uint)0, (uint)msgBuff.Length);
??????????????????? //发送
?????????????????? this.ng.sk.BeginSend(msgBuff, 0, msgBuff.Length, SocketFlags.None, onSendOver, this.ng);
?????
??????????????? }
??????????????? catch
??????????????? {
??????????????????? Core.mainForm.addLog("发送失败");
??????????????? }
??????????? }

???????????????? int ind = 0;
???????????????? if (size > 0)
???????????????? {
???????????????????? while (size > 0)
???????????????????? {
???????????????????????? //读消息头
???????????????????????? MemoryStream lenMs = new MemoryStream(ng.buff, ind, 4);
???????????????????????? ByteArray lenByte = new ByteArray(lenMs);
???????????????????????? Int32 len = lenByte.ReadInt();
??????????????????????? // Core.mainForm.addLog("消息长度:" + len.ToString());
???????????????????????? if (len == 0) { break; }
???????????????????????? //根据头长度取内容
???????????????????????? MemoryStream ms = new MemoryStream(ng.buff, ind + 4, len);
???????????????????????? ByteArray ba = new ByteArray(ms);
??????????????????????? // ba.Uncompress();
???????????????????????? object obj = ba.ReadObject();
???????????????????????? this.readMsg(obj);
???????????????????????? ind += (len + 4);
???????????????????????? size -= (len + 4);
???????????????????? }
???????????????????? this.ng.buff.Initialize();
???????????????????? ng.sk.BeginReceive(this.ng.buff, 0, this.ng.buffSize, SocketFlags.None, this.onReceOver, this.ng);
???????????????? }
???????????????? else
???????????????? {
???????????????????? ng.sk.Close();
???????????????????? this.offLine();
???????????????? }
???????? }

热点排行