'2010/09/17'에 해당되는 글 1건

  1. non-blocking SocketChannel에서 read() timeout 처리하기.. 2010/09/17
SocketChannel의 read 메쏘드는 timeout이 존재하지 않습니다.
혹, SocketChannel 객체의 .socket() 메쏘드로  소켓객체의  타임아웃을 세팅해도 먹지 않죠.. ^^;;
그래서, non-blocking의 read 메쏘드에 대한 timeout은 busy waiting하는 형태로 타임아웃을 줄 수 있겠습니다..

                // 20초               
                long timeout = 20 * 1000;  
                long stime = new Date().getTime();
                long rtime = 0L;
               
                int rvalue =0;
                int rvalues = 0;
                while (rtime < timeout ) {
                    rtime = System.currentTimeMillis() - stime;
                    rvalue = channel.read(rbuffer);
                    if(rvalue == -1)
                        throw new Exception("connection closed");
                   
                    rvalues = rvalues + rvalue;
                    if(rtime > timeout)
                        throw new Exception("read timeout exception");

                    if(rvalues >= 4)
                        break;
                }

저작자 표시