Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 移动互联网  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 一个用socket接收大量数据的问题
streamh





发贴: 1
积分: 0
于 2007-11-16 10:11 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
大家好!
本人初学J2ME,在做client和server通信的时候遇到一个收发数据的问题。Server不断地往client端发送图像数据,每个图像数据前面有一个四个字节的包头,标明图像的大小,然后通过TCP往client不停的发不同的图像数据。
client端通过一个while循环,不断的解析,接收,程序如下:

public int revImageData()
{
try
{
int v_ret;
v_ImageDate = new Vector();

//建立连接
String server = "socket://1723.16.123.231:4150");
byte[] imageData ;//图像数据
byte[] buf = null; //包头信息
int count = 0;

SocketConnection sock = (SocketConnection)Connector.open(server.toString(),Connector.READ);
DataInputStream inStream = sock.openDataInputStream();

while(true)
{
buf = new byte[4];
count = inStream.read( buf,0, buf.length);
if ( (count == 4) && (buf != null) )
{
dataLength = bytes2int(buf);
//先收4个字节,bytes2int函数解析出数据包长度
}

//receive the image data
imageData = new byte[dataLength ];
count = inStream.read(imageData,0,imageData.length);
//接下来处理收到的图像数据,不用管
ImageDataStore imageObj = new ImageDataStore(imageData);
this.v_ImageDate.addElement(imageObj);
//放到一个vector中
}//end while
return 1;
}
catch(Exception e_exp1)
{
System.out.println(e_exp1.toString());
return 43;
}
}

在这里出现的问题是:前面N幅图像都是完整地收到的,但是再次进行第N+1次循环的时候,dataLength就错了,是个很大的数值(由server端可知是个错误的值,把buf里面的内容打出来跟server对比,可知收到的buf就是已经错了,问题不在解析那)。而这个N值是不定的;

这段程序有什么问题么?接收大量的数据该如何处理为好?
想了很久,没想明白是怎么回事,望各位不吝赐教!!!




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923