SocketConnection 参数详细介绍

来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 15:11 点击:
SocketConnection 参数详细介绍
文章分类:移动开发
请大家看下面的代码:
len = is.read(gData);
这句代码会阻塞在这里,用了上面红色标出的代码后,能不能实现,超过5秒没反应后,read就不阻塞,而向下执行代码! 
try
{
int len = 0;
    sc = (SocketConnection) Connector.open("socket://100.42.25.3:885");
    is = sc.openInputStream();
    os = sc.openOutputStream();
    sc.setSocketOption(SocketConnection.LINGER, 5);
    pmm.gDataBuf = null;
    sender = new Sender(os);
    sender.send(pmm.gStringReq);
    // Loop forever, receiving data
    gData = new byte[pmm.BUF_LENGTH];
    currentIndex = 0;
    gBuf = new byte[50*1024];
    while (!pmm.bStopConnect)
    {
len = is.read(gData);
    System.arraycopy(gData,0,gBuf,currentIndex,len);
    currentIndex += len;
    if(currentIndex > 300){
    if(newVerifyXml()){
    if((currentIndex%8) != 0){
    int left = 8 - currentIndex%8;
    byte[] byteTmp = new byte[left];
    is.read(byteTmp);
}
    opHandle();currentIndex=0;
    }
    }
    }
    stop();
} catch (ConnectionNotFoundException cnfe) {
    Alert a = new Alert("错误", "无法连接服务器", null, AlertType.ERROR);
    a.setTimeout(Alert.FOREVER);
    display.setCurrent(a) ;
} catch (IOException ioe) {
    if (!stop) {
ioe.printStackTrace();
    }
} catch (Exception e) {
    e.printStackTrace();
}
实现不了吧, 你弄个timer吧。 超时就把所有object 设置为null
 
SocketConnection.DELAY:开启Nagle算法?(0/1)
SocketConnection.LINGER:服务器悬挂等待时间
SocketConnection.KEEPALIVE:长连接时间
SocketConnection.RCVBUF:接收缓冲
SocketConnection.SNDBUF:发送缓冲
不过虚拟机上可能支持的不好,或者不保证每次都准确
建议使用定时器
超时就把该关流,连接的全关了
再置null

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Java编程语言的八大优点
      • JVM对象生命周期详细介绍
      • Java平台上的CRM系统
      • Java 算数测试小程序
      • Command(命令模式)
      • Java 一个简单的画图程序
      • Java环境 使用Resin在NT环境下配置JSP环境
      • Java 日历的小程序
      • Java 统计代码的小工具
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1