오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(신입사원채용무료교육, 오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷)
이전 예제인 EchoServer 의 경우 동시에 여러 개의 클라이언트를 처리하는데 있어서는 read 메소드의 Blocking 으로 인해 어려움이 있습니다. 즉 동시에 여러 클라이언트의 요구를 처리 하지 못한다는거죠... 이문제를 해결하기 위해 다중 스레딩 (Multi-Threading) 을 구현한 서버를 사용하는 것인데 다중 스레드 서버는 클라이언트가 접속 할때 마다 1 개 이상의 스레드를 만들어 돌리기 때문에 결국 블록킹 I/O 문제를 해결해 주는 것 입니다. 즉 메인 스레드는 클라이언트의 연결을 받기만 하고 클라이언트와 데이터를 주고 받는 일은 별도의 Thread 에서 처리 하도록 구성을 하는 겁니다.
[ MultiThreadEchoServer.java] -- 서버이니까 실행시 포트 번호만 인자로 주세요... java MultiThreadEchoServer 1000
import java.io.*;
import java.net.*;
class MultiThreadEchoServer extends Thread {
protected Socket sock;
//----------------------- Constructor
MultiThreadEchoServer (Socket sock) {
this.sock = sock;
}
//------------------------------------
public void run() {
try {
System.out.println(sock + ": 연결됨");
InputStream fromClient = sock.getInputStream();
OutputStream toClient = sock.getOutputStream();
byte[] buf = new byte[1024];
int count;
while( (count = fromClient.read(buf)) != -1 ) {
toClient.write( buf, 0, count );
System.out.write(buf, 0, count);
}
toClient.close();
System.out.println(sock + ": 연결 종료");
}
catch( IOException ex ) {
System.out.println(sock + ": 연결 종료 (" + ex + ")");
}
finally {
try {
if ( sock != null ) sock.close();
}
catch( IOException ex ) {}
}
}
//------------------------------------
public static void main( String[] args ) throws IOException {
ServerSocket serverSock = new ServerSocket( Integer.parseInt(args[0]) );
System.out.println(serverSock + ": 서버 소켓 생성");
while(true) {
Socket client = serverSock.accept();
MultiThreadEchoServer myServer = new MultiThreadEchoServer(client);
myServer.start();
}
}
}
클라이언트 프로그램은 이전 강좌의 EchoClient.java와 동일 합니다.
[EchoClient.java] //클라이언트 이므로 실행시 서버, 포크번호 2개를 인자로 줍니다. java EchoClient 1000
import java.io.*;
import java.net.*;
class EchoClient
{ public static void main( String[] args )
throws IOException
{
Socket sock = null;
try
{
sock = new Socket(args[0], Integer.parseInt(args[1]));
System.out.println(sock + ": 연결됨");
OutputStream toServer = sock.getOutputStream();
InputStream fromServer = sock.getInputStream();
byte[] buf = new byte[1024];
int count;
while( (count = System.in.read(buf)) != -1 )
{
toServer.write( buf, 0, count );
count = fromServer.read( buf );
System.out.write( buf, 0, count );
}
toServer.close();
while((count = fromServer.read(buf)) != -1 )
System.out.write( buf, 0, count );
System.out.close();
System.out.println(sock + ": 연결 종료");
} catch( IOException ex )
{
System.out.println("연결 종료 (" + ex + ")");
} finally
{
try
{
if ( sock != null )
sock.close();
} catch( IOException ex ) {}
}
}
}
[결과]
위는 MultiThreadEchoServer 실행 화면
클라이언트1
클라이언트2
이번 강좌는 여기까지 하도록 하겠습니다. 그럼 다은 강좌에서 뵙도록 하겠습니다.
댓글 없음:
댓글 쓰기