如何在socket发送数据的时候,如何把数据的长度字段放在包里

pekinguniver 2008-01-23 05:54:39
如何在socket发送数据的时候,如何把数据的长度字段放在包里,不是appendnum的方法(该方法会将整型数转化成字符型.如长度为99 时就变成'99' 100 时为'100' 我就想用两字节来表示. 那么如果大于99 这方法就得三字节。而且很不好,对于服务端的解析). 各位可否给点你们做的 一些代码片段..
...全文
168 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Juncof 2008-01-25
  • 打赏
  • 举报
回复
TChar
Support
Supported from 5.0

Description
Holds a character value and provides a number of utility functions to manipulate it and test its properties. For example, functions to convert the character to uppercase and test whether it’s a control character.

The character value is stored as a 32-bit unsigned integer. The shorthand “TChar value” is used to describe the character value wrapped by a TChar object.

TChar can be used to represent Unicode values outside plane 0 (that is, the extended Unicode range from 0x10000 to 0xFFFFF). This differentiates it from TText which can only be used for 16-bit Unicode character values.

pekinguniver 2008-01-25
  • 打赏
  • 举报
回复
最后通过memcpy实现..
char buf[200] =
{
'\0'
};
TUint16 tmp = 15;//长度值
tmp = htons (tmp);
memcpy(&buf[3],&tmp,2);//第三个字节开始两字节用于存放长度字段

char* cc = buf;//把 buf (char)类型转换成所需参数类型const TDesC8& aData
TPtrC8 a;
a.Set ( (const TUint8*)cc,20);//20 发送数据总长度
iSocketsEngine->WriteL (a);//发送数据 原型 void CSocketsEngine::WriteL( const TDesC8& aData)
这问题其实还比较简单.. 因为c++ 基础不好, 所以导致出现一堆的问题.. 谢谢各位的参与.. 有意共同进步的可以QQ联系 33372449
pekinguniver 2008-01-24
  • 打赏
  • 举报
回复
这用法不对,编译过不去啊,, function call '[TBuf8<220>].Insert(int, {lval} unsigned char)' does not match 原型void Insert(TInt aPos,const TDesC8& aDes); 'TDes8::Insert(int, const TDesC8 &)' (non-static)
dyw 2008-01-24
  • 打赏
  • 举报
回复
假定数据长度为:
TInt16 len=...;

可将len变量的两个字节插入到待发送buffer。
TPtr8 ptr = ...;    // 假定buffer足够长
TText8* pLen = (TText8*)&len;
ptr.Insert(0, *pLen); // 低字节
ptr.Insert(0, *(pLen+1)); // 高字节

接收端按这里的编排顺序解析。
caitian6 2008-01-24
  • 打赏
  • 举报
回复
up
pekinguniver 2008-01-24
  • 打赏
  • 举报
回复
"直接按字节发送,长度固定。" 能否给点代码参考下,就是socket数据组包那部分就好了. symbian平台下的
dyw 2008-01-24
  • 打赏
  • 举报
回复
抱歉!草拟代码可能存在错误,但意图应该能明白。应用楼上兄弟们的方法或自己参阅SDK Help。
anel 2008-01-24
  • 打赏
  • 举报
回复
哈哈,简单:
buf8.Append(len);//len 0~0xffff
caitian6 2008-01-24
  • 打赏
  • 举报
回复
用这个方法实验下,我最近也在弄这个网络通讯
HBufC8* pBuf = HBufC8::NewL(77);
CleanupStack::PushL(pBuf);

TPtr8 buf = pBuf->Des();
buf.Zero();

int datalong = 512;
unsigned char* uclong =(unsigned char*)&datalong;
buf.Append((const TUint8*)uclong,4);

.......
CleanupStack::PopAndDestroy( pBuf );

加我Q吧,共同学习
258092549
注明 symbian
anel 2008-01-23
  • 打赏
  • 举报
回复
buf.append(' ');
unsigned short *p=(unsigned short *)buf.Ptr()
p+=buf.Lenght()-1;
*p=len;
dyw 2008-01-23
  • 打赏
  • 举报
回复
直接按字节发送,长度固定。
pekinguniver 2008-01-23
  • 打赏
  • 举报
回复
可能你误解我意思了,我是在想将数据转成能直接存在TBUF里(以整数的形式,不是那种appendnum),服务端在解包时就不用做其他的工作。
anel 2008-01-23
  • 打赏
  • 举报
回复
len只表示data的长度,而不是data+2

3,119

社区成员

发帖
与我相关
我的任务
社区描述
塞班系统(Symbian系统)是塞班公司为手机而设计的操作系统,它的前身是英国宝意昂公司的 EP ( Electronic Piece of cheese)操作系统。
社区管理员
  • Symbian社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧