问:
有一段socket的程序,每次运行时出现一个问题,总会出现read time out的异常:
        for (int i = 0; i < 2; i++) {
            // 存储查询网管机箱数据
            byte[] sendByte = new byte[5];
            // 网管机箱返回的数据
            byte[] nmsReturnData = new byte[1024];
            // 取得相应网管机箱的状态查询数据
            sendByte = RequestByteClass.getSingalInfo(
                "1",
                GeneralToolUtil.intToRightStyle(i),
                true,
                GeneralToolUtil.intToRightInt(i),
                -1);
            // 调试打印
            LogManagerUtility.printNmsSendData(i, sendByte);
            // 进行socket通信,取得相应的返回数据
            try {
                socket = new Socket(
                    InetAddress.getByName(ipAddress),
                    CommunicationData.iCommPort);
                socket.setTcpNoDelay(true);
                //socket.setSoTimeout(CommunicationData.iTimeout);
                socket.setSoTimeout(1000);
                DataOutputStream output = new DataOutputStream(
                    socket.getOutputStream());
                output.write(sendByte);
                output.flush();
                DataInputStream input = new DataInputStream(
                    socket.getInputStream());
                //input.read(nmsReturnData);                int rcv = 0,temp = 0;
                while((temp = input.read(nmsReturnData, rcv, 40-rcv)) > 0) {
                    rcv += temp;
                }
            } catch (SocketException se) {
                System.out.println("Server not starting!");
                ifLink = true;
            } catch (IOException ioe) {
                System.out.println("9999999999999="+ioe);
                ifLink = true;
            } finally {
                // 调试打印
                LogManagerUtility.printNmsReceData(nmsReturnData);
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException ioe) {}
                if (ifLink) {
                    deviceVec.add(i, null);
                } else {
                    doNmsAction(
                        cp,
                        nmsReturnData,
                        sendByte,
                        ipAddress,
                        i,
                        deviceVec);
                }
            }
        }
每次运行时出现如下的异常:
9999999999999=java.net.SocketTimeoutException: Read timed out
为什么,我设置的时间有1秒,如果不设置时,socket会阻塞.我怎么设置这个时间呢?

解决方案 »

  1.   

    1.建议使用NIO
    2.设置SoTimeout是为了能在阻塞时不影响其它的处理。如果你一定要读完数据,那么不妨把SoTimeout的值设置得更大一点。或者设置小值,并且对超时计数,超过一定的次数,就终止。    int rcv = 0,temp = 0;
        while(temp != -1) {
            try {
                temp = input.read(nmsReturnData, rcv, 40-rcv)
                if (temp >0)
                    rcv += temp;
            } catch(SocketTimeoutException ste) {
                // 执行超时处理
                // 比如:
                // Tread.sleep(); // 需要捕捉违例,略
                
                // 或者,doOtherProcess(),作其它工作以消磨等待时间
            }
        }