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

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 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备