2008년 09월 03일
Socket , Non Blocking 비교
public class ClientAcceptor {
public void run() {
...
Socket socket = serverSocket.accept();
socketList.addSocket(socket); // 소켓을 처리 목록에 추가
...
}
}
public class ClientProcessor {
public void run() {
while(true) {
Thread.sleep(100); // 0.1초간 대기
for (int i = 0 ; i < socketList.size() ; i++) {
// 클라이언트의 요청을 차례대로 처리
Socket socket = socketList.getSocket(i);
in = socket.getInputStream();
// 클라이언트와의 입출력 처리
in.read(..); // 블럭킹되므로 시간 대기 문제 발생
...
}
}
}
}
-----------------------------------------------------------------------------------------------------------------
public class ClientAcceptor {
public void run() {
ServerSocketChannel ssc = null;
try {
ssc = ServerSocketChannel.open();
InetSocketAddress address = new InetSocketAddress(port);
ssc.socket().bind(address);
while(true) {
SocketChannel socketChannel = serverChannel.accept();
// 소켓채널 논블럭킹 모드 지정
socketChannel.configureBlocking(false);
socketList.addSocket(socketChannel);
...
...
}
} catch(IOException ex) {
...
} finally {
...
}
}
}
public class ClientProcessor {
public void run() {
ByteBuffer buffer = ....;
...
while(true) {
Thread.sleep(100); // 0.1초간 대기
for (int i = 0 ; i < socketList.size() ; i++) {
// 클라이언트의 요청을 차례대로 처리
SocketChannel socket = socketList.getSocket(i);
buffer.clear();
socket.read(buffer); // 블럭킹 되지 않음
if (buffer.position() > 0) {
... // 소켓에서 읽어온 데이터 처리
}
}
}
}
}
public void run() {
...
Socket socket = serverSocket.accept();
socketList.addSocket(socket); // 소켓을 처리 목록에 추가
...
}
}
public class ClientProcessor {
public void run() {
while(true) {
Thread.sleep(100); // 0.1초간 대기
for (int i = 0 ; i < socketList.size() ; i++) {
// 클라이언트의 요청을 차례대로 처리
Socket socket = socketList.getSocket(i);
in = socket.getInputStream();
// 클라이언트와의 입출력 처리
in.read(..); // 블럭킹되므로 시간 대기 문제 발생
...
}
}
}
}
-----------------------------------------------------------------------------------------------------------------
public class ClientAcceptor {
public void run() {
ServerSocketChannel ssc = null;
try {
ssc = ServerSocketChannel.open();
InetSocketAddress address = new InetSocketAddress(port);
ssc.socket().bind(address);
while(true) {
SocketChannel socketChannel = serverChannel.accept();
// 소켓채널 논블럭킹 모드 지정
socketChannel.configureBlocking(false);
socketList.addSocket(socketChannel);
...
...
}
} catch(IOException ex) {
...
} finally {
...
}
}
}
public class ClientProcessor {
public void run() {
ByteBuffer buffer = ....;
...
while(true) {
Thread.sleep(100); // 0.1초간 대기
for (int i = 0 ; i < socketList.size() ; i++) {
// 클라이언트의 요청을 차례대로 처리
SocketChannel socket = socketList.getSocket(i);
buffer.clear();
socket.read(buffer); // 블럭킹 되지 않음
if (buffer.position() > 0) {
... // 소켓에서 읽어온 데이터 처리
}
}
}
}
}
# by | 2008/09/03 11:36 | JAVA | 트랙백 | 덧글(0)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]