cl534854121 2015-05-07 05:50 采纳率: 0%
浏览 8561

socket设置setsotimeout后为什么没有作用?

    try {
        ServerSocket serverSocket=null;
        serverSocket=new ServerSocket(1989);
        Socket socket=serverSocket.accept();
        serverSocket.setSoTimeout(2000);
        int temp=0;
        String chan_name;
        long chan_id;
        int chan_lock;
        int chan_skip;
        long chan_group;
        byte[] buf = new byte[1024*4];

        while(true){
            // socket.setSoTimeout(1000);

try {
InputStream ipInputStream=socket.getInputStream();
while((temp=ipInputStream.read(buf))!=-1){

    try {
        JSONObject prodate;
        String recStr = new String(buf, 0,
                temp);
        prodate = new JSONObject(recStr);
         chan_name = prodate.getString("mservice_name");
         chan_id = prodate.getLong("prog_id");
         chan_lock=prodate.getInt("mlock_flag");
         chan_skip=prodate.getInt("mskip_flag");
         chan_group=prodate.getLong("mfav_group");
        if(chan_name.contains(".")){
        StringBuffer tmp2 = new StringBuffer(chan_name);
        Bundle bundle = new Bundle();

// bundle.putString("chan_index",
// tmp2.substring( tmp2.indexOf(".")-1, tmp2.indexOf(".")));
//Log.d(TAG, "chan_index: " + tmp2.substring(0, tmp2.indexOf(".")));
bundle.putString("chan_name",tmp2.substring(tmp2.indexOf(".")+1));
bundle.putLong("chan_id", chan_id);
bundle.putInt("chan_lock", chan_lock);
bundle.putInt("chan_skip", chan_skip);
bundle.putLong("chan_group", chan_group);
Log.d(TAG, "chan_name: " + tmp2.substring(tmp2.indexOf(".")+1));
msg = chanHandler.obtainMessage();
msg.setData(bundle);
msg.what = 1;
chanHandler.sendMessage(msg);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

    }

} catch (SocketTimeoutException e) {
// TODO: handle exception
Log.d(TAG, "scan server timeout... ");
msg = chanHandler.obtainMessage();
msg.what = 0;
chanHandler.sendMessage(msg);
}
catch(SocketException e){

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

        }

            }


        catch (Exception e) {
        Log.e(TAG, e.getLocalizedMessage());
    }
  • 写回答

4条回答 默认 最新

  • cl534854121 2015-05-07 06:59
    关注

    来个大神帮帮忙啊,一直catch不到超时的动作。

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧