六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 41|回复: 0

SocketConnection 参数详细介绍

[复制链接]

升级  90.33%

147

主题

147

主题

147

主题

举人

Rank: 3Rank: 3

积分
471
 楼主| 发表于 2013-1-15 02:39:03 | 显示全部楼层 |阅读模式
<span style="font-family: arial; line-height: 18px;"><div style="font-size: 14px; padding: 0px; margin: 0px;" class="f14">请大家看下面的代码: 
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(); 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表