jNetServer Socket Framework
server applicationÀ» ÀçÈ°¿ëÇϱâ À§ÇÑ Â÷¿ø¿¡¼ Socket Server Framework
À» ¸¸µé¾î º¸¾Ò´Ù. ¿äÁò ÇÁ·ÎÁ§Æ®°¡ ´Ù À¥À̶ó ÀÌ·± Socket server¸¦ ¾ó¸¶³ª ¸¹ÀÌ °³¹ß ÇÒÁö´Â
¸ð¸£°ÚÁö¸¸, Áö±Ý Âü¿©ÇÏ°í ÀÖ´Â ÇÁ·ÎÁ§Æ®(EAI)¿¡¼´Â Áß¿äÇÑ ºÎºÐÀ¸·Î »ç¿ë µÇ°í ÀÖ´Ù. ( 2003/12/01
) 350
Written by specular - ÀüÈ«¼º
1 of 1
jNetServer1.0 Readme
Network Server ApplicationÀ» ±¸ÇöÇÒ ¶§ ¸¶´Ù ´À³¢´Â °ÍÀÌÁö¸¸ ¸Ç³¯ ¹Ýº¹µÇ´Â Äڵ忡
Áö°ã´Ù´Â »ý°¢À» ¸¹ÀÌ Çß´Ù. ¹°·Ð Ãʺ¸ÀÚ¿¡°Ô´Â Thread, Socket, IO °ü·Ã ÇÁ·Î±×·¥
ÀÌ ¾î·Æ°Ô ´À²¸Áö°Ú°í, ±×¸®°í, Thread Pool, Concurrency IssueµîÀÇ ¹®Á¦µµ °ñÄ¡°¡
¾ÆÇðÍÀÌ´Ù. ¾Æ¸¶µµ ÀÌ·± ÀÌÀ¯ ¶§¹®¿¡ application server¸¦ ¾²´ÂÁöµµ ¸ð¸£°Ú´Ù. ÇÏ
Áö¸¸ application server¿¡¼´Â Socket Connector´Â Á¦°øÇÏÁö ¾Ê´Â´Ù.
¾î°µç server applicationÀ» ÀçÈ°¿ëÇϱâ À§ÇÑ Â÷¿ø¿¡¼ Socket Server Framework
À» ¸¸µé¾î º¸¾Ò´Ù. ¿äÁò ÇÁ·ÎÁ§Æ®°¡ ´Ù À¥À̶ó ÀÌ·± Socket server¸¦ ¾ó¸¶³ª ¸¹ÀÌ °³¹ß
ÇÒÁö´Â ¸ð¸£°ÚÁö¸¸, Áö±Ý Âü¿©ÇÏ°í ÀÖ´Â ÇÁ·ÎÁ§Æ®(EAI)¿¡¼´Â Áß¿äÇÑ ºÎºÐÀ¸·Î »ç¿ë
µÇ°í ÀÖ´Ù. (2003.11.23 ÀÏ¿äÀÏ)
written by Jeon HongSeong [hsjeon70@dreamwiz.com]
jNetServer1.0 Java Documentation
API
1. °³ ¿ä
¡ß jNetServer1.0 °³¹ß ½Ã ÀÌ¿ëÇÑ open source List
- MX4J-1.1 : JMX1.0 Reference Implementation
- Jakarta Log4J-1.2.8
- Jakarta Common Digester-1.5
- Jakarta Common Pooling-1.1
- Jakarta Tomcat source ÀϺÎ
¡ß jNetServer FrameworkÀº Java Network Server Programming¿¡ ´ëÇÑ Basic
Infrastructure¸¦ Á¦°øÇÑ´Ù.
¡ß Network Server ApplicationÀ» ±¸ÇöÇÒ ¶§ ÇÊ¿äÇÑ Thread PoolingÀ̳ª, Object
Pooling, Logger µîÀÇ ±â´ÉÀ» Á¦°øÇÑ´Ù.
¡ß API¿¡ Á¤ÀÇµÈ NetTask ³» Ŭ¶óÀ̾ðÆ®·ÎºÎÅÍ µ¥ÀÌÅ͸¦ readÇÏ°í, °á°ú¸¦ writeÇÏ´Â
·ÎÁ÷¸¸ °£´ÜÈ÷ ±¸ÇöÇÏ¸é µÈ´Ù.
¡ß Configuration ¼³Á¤¸¸À¸·Î Network Server Connector, InputAdapter, Object
PoolÀÌ »ý¼º ÃʱâÈ µÈ´Ù.
¡ß Ŭ¶óÀ̾ðÆ® ½Ã½ºÅÛ(IP) º°·Î Á¢±Ù ±ÇÇÑ ¹× Åë½Å ÇÁ·ÎÅäÄÝÀ» ´Ù¸£°Ô Á¤ÀÇÇÒ ¼ö
ÀÖ´Ù.
¡ß ¸ðµç Java ObjectµéÀÌ JMX MBean object·Î °ü¸®µÇ°í, MX4J¿¡¼ Á¦°øÇÏ´Â JMX Http
Admin °ü¸® ÄܼÖÀ» Á¦°øÇÑ´Ù.
¡ß °ü¸® ÄÜ¼Ö »ó¿¡¼ ¼¹ö ¹× ¸ðµç MBean object¸¦ Á¦¾î ÇÒ ¼ö ÀÖ´Ù.
¡ß JMX Monitor BeanÀ» ÀÌ¿ëÇØ ConnectorÀÇ »óŸ¦ Counter, Gauge ¹æ½ÄÀ¸·Î ¸ð´ÏÅÍ
¸µ ÇÒ ¼ö ÀÖ´Ù.
¡ß jNetServer Framework ³»¿¡¼ configuration ¼³Á¤ ¸¸À¸·Î SSL(Secure Socket Layer)
¸¦ Áö¿øÇÑ´Ù. SSL¿ë NetTaskÀÇ ±¸ÇöÀº ÀÏ¹Ý Socket À϶§¿Í µ¿ÀÏÇÏ´Ù.
¡ß NetTaskÀÇ È®ÀåÀ¸·Î ÇÁ¶ô½Ã ¼¹ö, ·Îµå ¹ß·±½º µîÀÇ ¼¹ö¸¦ ½±°Ô ±¸ÇöÇÒ ¼ö ÀÖ´Ù.
2. ¼³Ä¡ ¹× ½ÇÇà
¡ß jNetServer1.0-app.jar ÆÄÀÏÀ» c:\jNetServer1.0 µð·ºÅ丮 ¹Ø¿¡ ¾ÐÃàÀ» ÇØÁ¦ÇÑ´Ù.
C:\jNetServer1.0>jar xvf jNetServer1.0-app.jar
C:\jNetServer1.0>dir
C µå¶óÀ̺êÀÇ º¼·ý¿¡´Â À̸§ÀÌ ¾ø½À´Ï´Ù.
º¼·ý ÀÏ·Ã ¹øÈ£: 3D26-12D4
C:\jNetServer1.0 µð·ºÅ͸®
2003-11-29 12:48a <DIR> .
2003-11-29 12:48a <DIR> ..
2003-11-29 12:48a 637 runSsl.bat
2003-11-29 12:48a 789 startup.bat
2003-11-29 12:48a 18 lcp.bat
2003-11-29 12:48a 705 runMulti.bat
2003-11-29 12:48a 720 stop.bat
2003-11-29 12:48a 645 runEcho.bat
2003-11-29 12:48a <DIR> logs
2003-11-29 12:48a <DIR> docs
2003-11-29 12:48a <DIR> server
2003-11-29 12:48a <DIR> common
2003-11-29 12:48a <DIR> config
6°³ ÆÄÀÏ 3,514 ¹ÙÀÌÆ®
7 µð·ºÅ͸® 2,378,612,736 ¹ÙÀÌÆ® ³²À½
¡ß jNetServer1.0/config/server.xml¿¡ Server ű×ÀÇ address ¼Ó¼ºÀ» ¼³Ä¡ ½Ã½ºÅÛÀÇ
IP ÁÖ¼Ò·Î º¯°æÇÑ´Ù.
<ServerGroup>
<Server info="jNetServer1.0" name="svr1"
address="192.168.0.13 " port="8110"
mode="standalone" shutdown="SHUTDOWN">
¡ß startup.bat ÆÄÀÏÀ» ½ÇÇàÇÏ¿© jNetServer¸¦ start ½ÃŲ´Ù. À̶§ keystore fileÀÇ
ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇØ¾ß Çϴµ¥ "java11"À» ÀÔ·ÂÇÑ´Ù.
C:\jNetServer1.0>startup
CLASSPATH=.;D:\bea\weblogic81\server\lib\weblogic.jar;server\lib\jNetServer1.0.j
ar;server\lib\jakarta\commons-configuration-0.8.1.jar;server\lib\jakarta\commons
-digester.jar;server\lib\jakarta\commons-net-1.0.1-dev.jar;server\lib\jakarta\co
mmons-daemon.jar;server\lib\jakarta\commons-collections.jar;server\lib\jakarta\c
ommons-logging-api.jar;server\lib\jakarta\commons-beanutils.jar;server\lib\jakar
ta\commons-logging.jar;server\lib\jakarta\commons-pool-1.1.jar;server\lib\jakart
a\commons-dbcp.jar;server\lib\jakarta\commons-lang.jar;server\lib\log4j\log4j-1.
2.8.jar;server\lib\mx4j\mx4j-jmx.jar;server\lib\mx4j\mx4j-tools.jar;common\class
es;config
[00:51:29] INFO - @ jNetServer1.0 @@@@@@@@@@@@@@@@@@@@@@@@@@
[00:51:29] INFO - >> jnet.home=.
[00:51:29] INFO - >> jnet.server=svr1
[00:51:29] INFO - >> Logger=jnet.logger
Enter the password of the keystore file : java11
[00:54:12] INFO - >> Including directory C:\jNetServer1.0\.\common\classes
[00:54:12] INFO - StandardConnector{7130} listen 192.168.0.13
[00:54:12] DEBUG - EchoTask#taskCreate()- EchoTask{echo1}[0]
[00:54:12] DEBUG - EchoTask#taskPassivate()- EchoTask{echo1}[0]
[00:54:12] DEBUG - EchoTask#taskCreate()- EchoTask{echo1}[1]
[00:54:12] DEBUG - EchoTask#taskPassivate()- EchoTask{echo1}[1]
[00:54:12] DEBUG - EchoTask#taskCreate()- EchoTask{echo1}[2]
[00:54:12] DEBUG - EchoTask#taskPassivate()- EchoTask{echo1}[2]
[00:54:12] INFO - StandardTask{echo1} started
[00:54:12] INFO - StandardInputAdapter{everyone} started
[00:54:12] INFO - StandardMonitor[idleHandlers] started
[00:54:12] INFO - StandardMonitor[curHandlers] started
[00:54:12] INFO - Handler{7130}[0] has been started
[00:54:12] INFO - Handler{7130}[1] has been started
[00:54:12] INFO - Handler{7130}[2] has been started
[00:54:12] INFO - Handler{7130}[3] has been started
[00:54:12] INFO - Handler{7130}[4] has been started
[00:54:12] INFO - StandardConnector{7130} started
[00:54:12] INFO - StandardConnector{7131} listen 192.168.0.13
[00:54:13] DEBUG - SSLProxyTask#taskCreate()- SSLProxyTask{proxy}[0]
[00:54:13] DEBUG - SSLProxyTask#taskPassivate()- SSLProxyTask{proxy}[0]
[00:54:13] DEBUG - SSLProxyTask#taskCreate()- SSLProxyTask{proxy}[1]
[00:54:13] DEBUG - SSLProxyTask#taskPassivate()- SSLProxyTask{proxy}[1]
[00:54:13] DEBUG - SSLProxyTask#taskCreate()- SSLProxyTask{proxy}[2]
[00:54:13] DEBUG - SSLProxyTask#taskPassivate()- SSLProxyTask{proxy}[2]
[00:54:13] DEBUG - SSLProxyTask#taskCreate()- SSLProxyTask{proxy}[3]
[00:54:13] DEBUG - SSLProxyTask#taskPassivate()- SSLProxyTask{proxy}[3]
[00:54:13] DEBUG - SSLProxyTask#taskCreate()- SSLProxyTask{proxy}[4]
[00:54:13] DEBUG - SSLProxyTask#taskPassivate()- SSLProxyTask{proxy}[4]
[00:54:13] INFO - StandardTask{proxy} started
[00:54:13] INFO - StandardInputAdapter{192.168.0.13} started
[00:54:13] DEBUG - SSLEchoTask#taskCreate()- SSLEchoTask{echo2}[0]
[00:54:13] DEBUG - SSLEchoTask#taskPassivate()- SSLEchoTask{echo2}[0]
[00:54:13] DEBUG - SSLEchoTask#taskCreate()- SSLEchoTask{echo2}[1]
[00:54:13] DEBUG - SSLEchoTask#taskPassivate()- SSLEchoTask{echo2}[1]
[00:54:13] DEBUG - SSLEchoTask#taskCreate()- SSLEchoTask{echo2}[2]
[00:54:13] DEBUG - SSLEchoTask#taskPassivate()- SSLEchoTask{echo2}[2]
[00:54:13] DEBUG - SSLEchoTask#taskCreate()- SSLEchoTask{echo2}[3]
[00:54:13] DEBUG - SSLEchoTask#taskPassivate()- SSLEchoTask{echo2}[3]
[00:54:13] DEBUG - SSLEchoTask#taskCreate()- SSLEchoTask{echo2}[4]
[00:54:13] DEBUG - SSLEchoTask#taskPassivate()- SSLEchoTask{echo2}[4]
[00:54:13] INFO - StandardTask{echo2} started
[00:54:13] INFO - StandardInputAdapter{everyone} started
[00:54:13] INFO - Handler{7131}[0] has been started
[00:54:13] INFO - Handler{7131}[1] has been started
[00:54:13] INFO - Handler{7131}[2] has been started
[00:54:13] INFO - Handler{7131}[3] has been started
[00:54:13] INFO - Handler{7131}[4] has been started
[00:54:13] INFO - StandardConnector{7131} started
[00:54:13] INFO - StandardServer{svr1} started
[00:54:13] INFO - StandardServerGroup started
[00:54:13] INFO - ConsoleGaugeListener>> MonitorNotification [ sequence=1, time
Stamp=Sat Nov 29 00:54:13 KST 2003, type=jmx.monitor.gauge.low, userData=null, m
essage=, derivedGauge=5, observedObject=jnet.server:name=svr1/con7130, observedA
ttribute=IdleHandlers, trigger=5, source=javax.management.monitor.GaugeMonitor@c
623af ]
[00:54:13] INFO - ConsoleCounterListener>> MonitorNotification [ sequence=1, ti
meStamp=Sat Nov 29 00:54:13 KST 2003, type=jmx.monitor.counter.threshold, userDa
ta=null, message=, derivedGauge=5, observedObject=jnet.server:name=svr1/con7130,
observedAttribute=CurHandlers, trigger=3, source=javax.management.monitor.Count
erMonitor@50ca0c ]
3. Admin Console
¡ß jNetServer°¡ ½ÇÇàµÇ¸é, Http Admin Console¿¡ Á¢±ÙÇØ °ü¸®ÇÒ¼ö ÀÖ´Ù.
http://192.168.0.13:8080
ºê¶ó¿ìÀú·Î 8080 Æ÷Æ®·Î Á¢±ÙÇØ º¸¸é ¾Æ·¡¿Í °°ÀÌ ·Î±×¿Â ȸéÀÌ ³ªÅ¸³ª´Âµ¥,
jlook/jlookÀ¸·Î ¿À±×¿ÂÀ» ÇÑ´Ù. admin °èÁ¤°ú ºñ¹Ð¹øÈ£´Â Àº config/server.xml
¿¡ ¼³Á¤µÇ¾î ÀÖ´Ù.
¡ß ·Î±×ÀÎÇϸé startµÈ jNetServerÀÇ JMX MBean object¸¦ °ü¸®ÇÒ¼ö ÀÖ´Â ±â´É°ú »óÅÂ
¸¦ ¸ð´ÏÅ͸µ ÇÒ ¼ö Àִ ȸéÀ» Á¦°øÇÑ´Ù.
4. EchoTask Bean ¹× Å×½ºÆ®
¡ß ¼³Ä¡µÈ jNetServer¿¡´Â ¿¹Á¦·Î EchoTask BeanÀÌ Á¦°øµÈ´Ù. Task BeanÀÇ
°³¹ßÀº jlook.jnet.task.NetTask ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÏ¸é µÈ´Ù. ´ÙÀ½Àº NetTaskÀÇ
¼Ò½ºÀÌ´Ù.
package jlook.jnet.task;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
/**
* Socket ¿äûÀ» ¹Þ¾Æ ó¸®ÇÒ Beans¸¦ ±¸Á¤ÀÇÇϱâ À§ÇÑ NetTask interface
*
* @since jNetServer1.0
* @author HongSeong Jeon(hsjeon70@dreamwiz.com)
*/
public interface NetTask {
/**
* NetTask objectÀÇ id¸¦ ¹ÝȯÇÑ´Ù.
*
* @return id
*/
public String getId();
/**
* NetTask Beans object »ý¼º ÈÄ callback
*/
public void taskCreate();
/**
* NetTask Beans object°¡ ¿äû¿¡ ÀÇÇØ poolingÀ¸·Î ºÎÅÍ ³ª¿Í ÇÒ´çµÈ
* ÈÄ callback
*/
public void taskActivate();
/**
* NetTask Beans object°¡ ¿äûÀ» ó¸®ÇÏ°í, poolingÀ¸·Î ¹ÝȯµÈ ÈÄ
* callback
*/
public void taskPassivate();
/**
* NetTask Beans object°¡ poolingÀ¸·Î ºÎÅÍ deallocated µÉ¶§ callback
*/
public void taskDestroy();
/**
* Ŭ¶óÀ̾ðÆ®ÀÇ ¿äûÀ» ¹Þ¾Æ NetContext object¸¦ ÃʱâÈÇϱâ À§ÇØ
* ½ÇÇàµÈ´Ù.
*
* @param context NetContext object
*/
public void setContext(NetContext context);
/**
* Ŭ¶óÀ̾ðÆ®ÀÇ ¿äû¿¡ ´ëÇÑ Ã³¸®·ÎÁ÷À» À§ÇØ ½ÇÇàµÈ´Ù.
*
* @param in BufferedInputStream object
* @param out BufferedOutputStream object
* @exception TaskException
*/
public void doTask(BufferedInputStream in, BufferedOutputStream out)
throws TaskException;
/**
* Ŭ¶óÀ̾ðÆ®ÀÇ ¿äû¿¡ ´ëÇÑ Ã³¸®·ÎÁ÷À» ½ÇÇàÇÑ ÈÄ ÈÄó¸® ÀÛ¾÷À»
* À§ÇØ ½ÇÇàµÈ´Ù.
*
* @param success doTask() ¸Þ¼µå¿¡¼ ExceptionÀÇ ¹ß»ý¿©ºÎ¸¦
* ³ªÅ¸³»´Â flag
*/
public void doEnd(boolean success);
}
- taskCreate() : Task BeanÀÌ »ý¼ºµÇ°í, BeanÀÇ ÃʱâÈ ÀÛ¾÷À» À§ÇØ È£ÃâµÈ´Ù.
- taskDestory() : Task BeanÀÌ Object Pool¿¡¼ »èÁ¦µÉ ¶§ ÃʱâÈ ÀÛ¾÷ÀÇ undo¸¦
À§ÇØ È£ÃâµÈ´Ù.
- taskActivate() : Task BeanÀÌ Object Pool¿¡ ¹ÝȯµÉ¶§ È£ÃâµÈ´Ù.
- taskPassivate() : Task BeanÀÌ Object Pool·Î ºÎÅÍ ³ª¿Í ¼ºñ½º µÇ±â Á÷Àü¿¡
È£ÃâµÈ´Ù.
- setContext() : Ŭ¶óÀ̾ðÆ®ÀÇ ¿äû ½Ã Ŭ¶óÀ̾ðÆ®ÀÇ Á¤º¸¸¦ °®´Â Context Á¤º¸¸¦
ÃʱâÈ Çϱâ À§ÇØ È£ÃâµÈ´Ù.\
- doTask() : Ŭ¶óÀ̾ðÆ® ¿äû½Ã setContext() °¡ ½ÇÇàµÈ ´ÙÀ½ È£ÃâµÈ´Ù. ÆĶó¹ÌÅÍÀÇ
InputStream, OutputStreamÀ» ÀÌ¿ëÇØ ½ÇÀç ÇØ´ç ¿äûÀÇ Ã³¸®·ÎÁ÷À»
±¸ÇöÇÑ´Ù.
- doEnd() : ÈÄó¸® ÀÛ¾÷À» À§ÇØ ¸¶Áö¸·¿¡ È£ÃâµÈ´Ù.
¡ß NetTaskÀÇ ±¸ÇöÀº ½ÇÀç NetTaskSupport Ŭ·¡½º¸¦ »ó¼Ó¹Þ¾Æ Á¤ÀÇÇÑ´Ù. ´ÙÀ½Àº Á¦°ø
µÇ´Â EchoTaskÀÇ ¼Ò½ºÀÌ´Ù.
package jlook.jnet.task;
import java.io.*;
import jlook.jnet.Keys;
import jlook.jnet.util.Logger;
public class EchoTask extends NetTaskSupport {
private static Logger logger = Logger.getLogger(Keys.LOGGER);
public void taskCreate() {
logger.debug("EchoTask#taskCreate()- "+id);
}
public void taskActivate(){
logger.debug("EchoTask#taskActivate()- "+id);
}
public void taskPassivate(){
logger.debug("EchoTask#taskPassivate()- "+id);
}
public void taskDestroy(){
logger.debug("EchoTask#taskDestroy()- "+id);
}
public void setContext(NetContext context) {
super.setContext(context);
logger.debug("EchoTask#setContext()- "+id);
}
public void doTask(BufferedInputStream in, BufferedOutputStream out)
throws TaskException {
Logger logger = context.getLogger();
logger.debug("EchoTask#doTask()- "+id);
byte[] msg = new byte[100];
logger.debug(id+"#doTask()- read....");
try {
int len = in.read(msg);
String str = new String(msg,0,len);
logger.debug(id+"#doTask()- received msg>> "+str);
str = "Hi... "+str;
byte[] rt = str.getBytes();
out.write(rt, 0, rt.length);
} catch(Exception e) {
throw new TaskException(e.getMessage());
}
}
public void doEnd(boolean success) {
logger.debug("EchoTask#doEnd()- "+id);
}
public String toString() {
return "SampleNetTask>>"+id;
}
}
¡ß °³¹ßµÈ NetTask BeanÀº config/server.xml¿¡ ´ÙÀ½°ú °°ÀÌ ¼³Á¤ÇØ¾ß ÇÑ´Ù.
<InputAdapter source="everyone"
connectionTimeout="10000"
sendBufferSize="10240"
receiveBufferSize="10240"
className="jlook.jnet.connector.StandardInputAdapter">
<Task name="echo1" initSize="5" maxSize="50"
className="jlook.jnet.connector.StandardTask"
beanClass="jlook.jnet.task.EchoTask"/>
</InputAdapter>
- name : unique name
- initSize : object poolÀÇ initial size
- maxSize : object poolÀÇ maximum size
- className : Tag¿¡ ´ëÇÑ Á¤º¸¸¦ °®´Â BeanÀ¸·Î À§¿Í °°ÀÌ ¹Ýµå½Ã Á¦°øµÇ´Â
StandardTask Ŭ·¡½º¸¦ ¼³Á¤ÇÑ´Ù.
- beanClass : °³¹ßÇÑ NetTask Bean class¸¦ ¼³Á¤ÇÑ´Ù.
¡ß ´ÙÀ½Àº EchoTask¿¡ ´ëÇÑ Client ÇÁ·Î±×·¥ÀÌ´Ù.
package jlook.jnet.task;
import java.io.*;
import java.net.*;
public class EchoClient implements Runnable {
private String server;
private int port;
private String message;
private boolean success;
private Socket sck;
public EchoClient(String server, int port, String message)
throws Exception {
this.server = server;
this.port = port;
this.message = message;
try {
sck = new Socket(server, port);
} catch(Exception e) {
System.out.println(Thread.currentThread().getName() +
" Error>> "+e.toString());
sck = new Socket(server, port);
}
}
public static void main(String[] args) throws Exception {
if(args.length!=3) {
System.out.println("usage : java EchoClient "+
" ");
return;
}
int port = -1;
try {
port = Integer.parseInt(args[1]);
} catch(Exception e) {
System.out.println("invalid port - "+port);
throw e;
}
EchoClient ec = new EchoClient(args[0], port, args[2]);
Thread t = new Thread(ec);
t.start();
}
public boolean isSuccess() {
return success;
}
public void run() {
InputStream in = null;
OutputStream out = null;
try {
in = sck.getInputStream();
out = sck.getOutputStream();
byte[] b = message.getBytes();
out.write(b, 0, b.length);
byte[] buff = new byte[100];
int len = in.read(buff);
System.out.println(Thread.currentThread().getName()+
"] "+ new String(buff, 0, buff.length));
success = true;
} catch(Exception e) {
success = false;
System.out.println(Thread.currentThread().getName() +
" Error>> "+e.toString());
e.printStackTrace();
} finally {
try { notifyAll();} catch(Exception e){}
try { if(in!=null) in.close();} catch(Exception e) {}
try { if(out!=null) out.close();} catch(Exception e) {}
try { if(sck!=null) sck.close();} catch(Exception e) {}
}
}
}
¡ß À§ EchoClient¸¦ Å×½ºÆ® Çϱâ À§ÇØ runEcho.bat ÆÄÀÏ ³» ip¸¦ ¼öÁ¤ÇÏ°í, ´ÙÀ½°ú
°°ÀÌ runEcho.bat¸¦ ½ÇÇàÇÑ´Ù.
C:\jNetServer1.0>runEcho
CLASSPATH=.;D:\bea\weblogic81\server\lib\weblogic.jar;lib\jNetServer1.0.jar;
classes;config
Thread-1] Hi... hongseong
¡ß ´ÙÀ½Àº jNetServerÂÊ Äֿܼ¡ ³ªÅ¸³ ·Î±× ³»¿ëÀÌ´Ù. ÀÌ ·Î±×¸¦ ÅëÇØ NetTask¿¡ Á¤ÀǵÈ
¸Þ¼µå°¡ ¾ðÁ¦ ½ÇÇàµÇ´ÂÁö ÀÌÇØÇÒ ¼ö ÀÖ´Ù.
[21:32:48] DEBUG - @ request client address : 192.168.0.13
[21:32:48] DEBUG - StandardInputAdapter#doExecute() is started
[21:32:48] DEBUG - EchoTask#taskActivate()- EchoTask{echo1}[4]
[21:32:48] DEBUG - EchoTask#setContext()- EchoTask{echo1}[4]
[21:32:48] DEBUG - EchoTask#doTask()- EchoTask{echo1}[4]
[21:32:48] DEBUG - EchoTask{echo1}[4]#doTask()- read....
[21:32:48] DEBUG - EchoTask{echo1}[4]#doTask()- received msg>> hongseong
[21:32:48] DEBUG - EchoTask#doEnd()- EchoTask{echo1}[4]
[21:32:48] DEBUG - EchoTask#taskPassivate()- EchoTask{echo1}[4]
[21:32:49] DEBUG - Handler{7130}[4] process time : 0.38 sec
5. SSL ¼³Á¤ ¹× Å×½ºÆ®
¡ß config/server.xmlÀÇ ³»¿ëÀ» º¸¸é ´ÙÀ½°ú °°ÀÌ 7131 Æ÷Æ®°¡ SSL ·Î ¼³Á¤µÇ¾î ÀÖ´Ù.
<Connector name="con7131" className="jlook.jnet.connector.StandardConnector"
port="7131" ssl="true"
enableLookups="true" acceptCount="50"
minHandlers="5" maxHandlers="50">
<InputAdapter source="everyone"
connectionTimeout="10000"
sendBufferSize="10240"
receiveBufferSize="10240"
className="jlook.jnet.connector.StandardInputAdapter">
<Task name="echo2" initSize="5" maxSize="50"
className="jlook.jnet.connector.StandardTask"
beanClass="jlook.jnet.task.SSLEchoTask"/>
</InputAdapter>
<InputAdapter source="192.168.0.10"
connectionTimeout="10000"
sendBufferSize="10240"
receiveBufferSize="10240"
className="jlook.jnet.connector.StandardInputAdapter">
<Task name="proxy" initSize="5" maxSize="50"
className="jlook.jnet.connector.StandardTask"
beanClass="jlook.jnet.task.SSLProxyTask">
<Parameter name="target.server" value="192.168.0.13"/>
<Parameter name="target.port" value="7130"/>
<Parameter name="buffer.size" value="9"/>
</Task>
</InputAdapter>
¡ß 7131ÀÌ SSL·Î ¼³Á¤µÇ¾î ÀÖÀ¸¹Ç·Î ¾Õ¿¡¼ ½ÇÇàÇß´ø runEcho.bat ÆÄÀÏÀÇ port¸¦ 7131·Î
¼öÁ¤ ÈÄ ½ÇÇàÇØ º¸¸é ´ÙÀ½°ú °°ÀÌ ¿¡·¯°¡ ¹ß»ýÇÑ´Ù.
### Client console]
C:\jNetServer1.0>runEcho
CLASSPATH=.;D:\bea\weblogic81\server\lib\weblogic.jar;server\lib\jNetServer1.0.j
ar;common\classes;config
Thread-1]
### jNetServer console]
[01:16:24] ERROR - Unrecognized SSL message, plaintext connection?
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.net.ssl.internal.ssl.InputRecord.b(DashoA6275)
at com.sun.net.ssl.internal.ssl.InputRecord.read(DashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.a(DashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.j(DashoA6275)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(DashoA6275)
¡ß runssl.bat ÆÄÀÏÀ» ¿ÀÇÂÇØ ip{192.168.0.13}¸¦ ½Ã½ºÅÛ¿¡ ¸Â°Ô º¯°æÇÏ°í, ½ÇÇàÇØ
º¸¸é ´ÙÀ½°ú °°Àº °á°ú¸¦ È®ÀÎÇØ º¼¼ö ÀÖ´Ù.
### Client console]
C:\jNetServer1.0>runssl
CLASSPATH=.;D:\bea\weblogic81\server\lib\weblogic.jar;server\lib\jNetServer1.0.j
ar;common\classes;config
connect to the server.[192.168.0.13, 7131]
received message>> Hello hongseong
### jNetServer console]
[01:27:12] DEBUG - @ request client address : 192.168.0.13
[01:27:12] DEBUG - StandardInputAdapter#doExecute() is started
[01:27:12] DEBUG - InputAdapter>>svr1/con7131/everyone
[01:27:12] DEBUG - StandardInputAdapter#doExecute()- start handshake
[01:27:12] DEBUG - SSLEchoTask#taskActivate()- SSLEchoTask{echo2}[4]
[01:27:12] DEBUG - SSLEchoTask#setContext()- SSLEchoTask{echo2}[4]
[01:27:12] DEBUG - SSLEchoTask#doTask()- SSLEchoTask{echo2}[4]
[01:27:12] DEBUG - SSLEchoTask{echo2}[4]#doTask()- read....
[01:27:12] DEBUG - SSLEchoTask{echo2}[4]#doTask()- received msg>> hongseong
[01:27:12] DEBUG - SSLEchoTask#taskPassivate()- SSLEchoTask{echo2}[4]
[01:27:12] DEBUG - Handler{7131}[4] process time : 0.431 sec
¡ß Ŭ¶óÀ̾ðÆ® ½Ã½ºÅÛÀÌ 192.168.0.13 À̹ǷΠŬ¶óÀ̾ðÆ® ½Ã½ºÅÛ¿¡ ´ëÇÏ InputAdapter°¡
¼³Á¤µÇ¾î ÀÖÁö ¾Ê±â ¶§¹®¿¡ defaultÀÎ "everyone"ÀÇ InputAdapter°¡ µ¿ÀÛÇÑ °ÍÀÌ´Ù.
±×¸®°í, Task beans´Â SSLEchoTask°¡ ½ÇÇàµÇ¾ú°í, ±× ¼Ò½º´Â ¾Æ·¡¿Í °°´Ù. SSL °ü·Ã
Ãß°¡ ÄÚµå´Â ÇÊ¿äÇÏÁö ¾Ê´Ù.
package jlook.jnet.task;
import java.io.*;
import jlook.jnet.Keys;
import jlook.jnet.util.Logger;
public class SSLEchoTask extends NetTaskSupport {
private static Logger logger = Logger.getLogger(Keys.LOGGER);
public void taskCreate() {
logger.debug("SSLEchoTask#taskCreate()- "+id);
}
public void taskActivate(){
logger.debug("SSLEchoTask#taskActivate()- "+id);
}
public void taskPassivate(){
logger.debug("SSLEchoTask#taskPassivate()- "+id);
}
public void taskDestroy(){
logger.debug("SSLEchoTask#taskDestroy()- "+id);
}
public void setContext(NetContext context) {
super.setContext(context);
logger.debug("SSLEchoTask#setContext()- "+id);
}
public void doTask(BufferedInputStream in, BufferedOutputStream out)
throws TaskException {
Logger logger = context.getLogger();
logger.debug("SSLEchoTask#doTask()- "+id);
byte[] msg = new byte[100];
logger.debug(id+"#doTask()- read....");
try {
int len = in.read(msg);
String str = new String(msg,0,len);
logger.debug(id+"#doTask()- received msg>> "+str);
str = "Hi... "+str;
byte[] rt = str.getBytes();
out.write(rt, 0, rt.length);
} catch(Exception e) {
logger.error(e.getMessage(),e);
throw new TaskException(e.getMessage());
}
}
public void doEnd(boolean success) {
logger.debug("SSLEchoTask#doEnd()- "+id);
}
public String toString() {
return "SampleNetTask>>"+id;
}
}
¡ß ´ÙÀ½Àº À§¿¡¼ Å×½ºÆ®ÇÑ ssl Client application ¼Ò½ºÀÌ´Ù. ¼¹ö¿Í µ¿ÀÏÇÑ keystore
ÆÄÀÏÀ» ÀÌ¿ëÇÑ °ÍÀ» ¾Ë ¼ö ÀÖ´Ù.
package jlook.jnet.task;
import java.io.*;
import java.net.*;
import java.security.*;
import javax.net.*;
import javax.net.ssl.*;
public class SSLEchoClient {
public static void main(String[] args) throws Exception {
System.setProperty("javax.net.ssl.trustStore",
"config/jnet.keystore");
if(args.length!=2) {
System.out.println("java SSLEchoClient ");
return;
}
System.out.println("connect to the server.["+
args[0]+", "+args[1]+"]");
int port = Integer.parseInt(args[1]);
SSLContext ctx;
KeyManagerFactory kmf;
KeyStore ks;
char[] passphrase = "java11".toCharArray();
ctx = SSLContext.getInstance("TLS");
kmf = KeyManagerFactory.getInstance("SunX509");
ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("config/jnet.keystore"), passphrase);
kmf.init(ks, passphrase);
ctx.init(kmf.getKeyManagers(), null, null);
SSLSocketFactory factory = ctx.getSocketFactory();
SSLSocket sck = (SSLSocket)factory.createSocket(args[0], port);
sck.setEnabledCipherSuites(sck.getSupportedCipherSuites());
sck.startHandshake();
BufferedInputStream in =
new BufferedInputStream(sck.getInputStream());
BufferedOutputStream out =
new BufferedOutputStream(sck.getOutputStream());
String msg = "hongseong";
byte[] tmp = msg.getBytes();
out.write(tmp, 0, tmp.length);
out.flush();
byte[] buff = new byte[1024];
int len = in.read(buff);
System.out.println("received message>> "+new String(buff, 0, len));
out.close();
in.close();
sck.close();
}
}
6. SSLProxyTask Beans
¡ß NetTask ÀÀ¿ëÀ¸·Î Proxy Server Beans¸¦ ¼Ò°³ÇÑ´Ù. ¼¹ö¸¦ 7130, SSL 7131·Î ¼ºñ½º ÇÒ¶§
7131·Î µé¾î¿Â ¿äûÀ» 7130ÂÊÀ¸·Î forwardingÇÏ´Â SSLProxyTaskÀÇ ¼Ò½º¿Í ¼³Á¤À» »ìÆ캸ÀÚ.
´ÙÀ½Àº server.xmlÀÇ 7131ÂÊ Connector ¼³Á¤ ºÎºÐÀε¥, ssl="true"·Î ¼³Á¤µÈ °ÍÀ»
È®ÀÎÇÒ ¼ö ÀÖ´Ù. ±×¸®°í, ¾Õ ¿¹Á¦¿Í´Â ´Ù¸£°Ô InputAdapterÀÇ source°¡ Çö ½Ã½ºÅÛÀÇ
ip{192.168.0.13}·Î º¯°æµÈ°ÍÀ» È®ÀÎÇÒ ¼ö ÀÖ´Ù. Áï, 192.168.0.13 ½Ã½ºÅÛÀÇ Å¬¶óÀÌ
¾ðÆ®ÀÇ ¿äûÀ» SSLProxyTask °¡ ó¸®ÇÑ´Ù´Â °ÍÀ» ¼³Á¤ÇÑ °ÍÀÌ´Ù.
<Connector name="con7131" className="jlook.jnet.connector.StandardConnector"
port="7131" ssl="true"
enableLookups="true" acceptCount="50"
minHandlers="5" maxHandlers="50">
<InputAdapter source="everyone"
connectionTimeout="10000"
sendBufferSize="10240"
receiveBufferSize="10240"
className="jlook.jnet.connector.StandardInputAdapter">
<Task name="echo2" initSize="5" maxSize="50"
className="jlook.jnet.connector.StandardTask"
beanClass="jlook.jnet.task.SSLEchoTask"/>
</InputAdapter>
<InputAdapter source="192.168.0.13"
connectionTimeout="10000"
sendBufferSize="10240"
receiveBufferSize="10240"
className="jlook.jnet.connector.StandardInputAdapter">
<Task name="proxy" initSize="5" maxSize="50"
className="jlook.jnet.connector.StandardTask"
beanClass="jlook.jnet.task.SSLProxyTask">
<Parameter name="target.server" value="192.168.0.13"/>
<Parameter name="target.port" value="7130"/>
<Parameter name="buffer.size" value="9"/>
</Task>
</InputAdapter>
¡ß ÀÌÁ¦ SSLProxyTask BeansÀÇ ¼Ò½º¸¦ »ìÆì º¼°ÍÀε¥, À§¿¡¼ TaskÀÇ Parameter ű״Â
Task Beans¿¡ ³Ñ±æ key/value µ¥ÀÌÅ͸¦ ¼³Á¤ÇÑ Å±×ÀÌ´Ù. Æ÷¿öµùÇÒ ¼¹ö ¹× Æ÷Æ®
Á¤º¸¸¦ ¼³Á¤Çß´Ù. ÀÌ°ÍÀ» ¾î¶»°Ô ¾ò¾î ³»¾ú´ÂÁö »ìÆ캸±â ¹Ù¶õ´Ù.
package jlook.jnet.task;
import java.io.*;
import java.net.*;
import jlook.jnet.Keys;
import jlook.jnet.util.Logger;
public class SSLProxyTask extends NetTaskSupport {
private static Logger logger = Logger.getLogger(Keys.LOGGER);
private String server;
private int port;
private int bufferSize;
public static final String TARGET_SERVER = "target.server";
public static final String TARGET_PORT = "target.port";
public static final String BUFFER_SIZE = "buffer.size";
public void taskCreate() {
logger.debug("SSLProxyTask#taskCreate()- "+id);
}
public void taskActivate(){
logger.debug("SSLProxyTask#taskActivate()- "+id);
}
public void taskPassivate(){
logger.debug("SSLProxyTask#taskPassivate()- "+id);
}
public void taskDestroy(){
logger.debug("SSLProxyTask#taskDestroy()- "+id);
}
public void setContext(NetContext context) {
super.setContext(context);
logger.debug("SSLProxyTask#setContext()- "+id);
server = context.getParameter(TARGET_SERVER);
if(server == null) server = "127.0.0.1";
try {
port = Integer.parseInt(context.getParameter(TARGET_PORT));
} catch(Exception e) {
port=80;
}
try {
bufferSize = Integer.parseInt(
context.getParameter(BUFFER_SIZE));
} catch(Exception e) {
bufferSize=10240;
}
}
public void doTask(BufferedInputStream in, BufferedOutputStream out)
throws TaskException {
Logger logger = context.getLogger();
logger.debug("SSLProxyTask#doTask()- "+id);
Socket proxy = null;
BufferedInputStream pin = null;
BufferedOutputStream pout = null;
try {
proxy= new Socket(server,port);
pin = new BufferedInputStream(proxy.getInputStream());
pout = new BufferedOutputStream(proxy.getOutputStream());
} catch(Exception e) {
String msg = "Cannot connect to target system>> "+
e.getMessage();
logger.error(msg, e);
byte[] tmp = msg.getBytes();
try {
out.write(tmp, 0, tmp.length);
out.flush();
} catch(IOException ex){}
return;
}
try {
byte[] buff = new byte[bufferSize];
while(true) {
int len = in.read(buff);
if(len>"+id;
}
}
¡ß ´ÙÀ½Àº runssl.bat ÆÄÀÏÀÇ ½ÇÇà °á°úÀÌ´Ù. ¼¹ö ÄܼÖÀ» º¸¸é Ŭ¶óÀ̾ðÆ® ¿äûÀÌ
7131{SSLProxyTask}, 7130{EchoTask} µÎ¹ø 󸮵ȰÍÀ» È®ÀÎÇÒ ¼ö ÀÖ´Ù.
### Client console]
C:\jNetServer1.0>runssl
CLASSPATH=.;D:\bea\weblogic81\server\lib\weblogic.jar;server\lib\jNetServer1.0.j
ar;common\classes;config
connect to the server.[192.168.0.13, 7131]
received message>> Hello hongseong
### Server console]
[01:54:29] DEBUG - @ request client address : 192.168.0.13
[01:54:29] DEBUG - StandardInputAdapter#doExecute() is started
[01:54:29] DEBUG - InputAdapter>>svr1/con7131/192.168.0.13
[01:54:29] DEBUG - StandardInputAdapter#doExecute()- start handshake
[01:54:29] DEBUG - SSLProxyTask#taskActivate()- SSLProxyTask{proxy}[4]
[01:54:29] DEBUG - SSLProxyTask#setContext()- SSLProxyTask{proxy}[4]
[01:54:29] DEBUG - SSLProxyTask#doTask()- SSLProxyTask{proxy}[4]
[01:54:29] DEBUG - @ request client address : 192.168.0.13
[01:54:29] DEBUG - StandardInputAdapter#doExecute() is started
[01:54:29] DEBUG - InputAdapter>>svr1/con7130/everyone
[01:54:29] DEBUG - EchoTask#taskActivate()- EchoTask{echo1}[2]
[01:54:29] DEBUG - EchoTask#setContext()- EchoTask{echo1}[2]
[01:54:29] DEBUG - EchoTask#doTask()- EchoTask{echo1}[2]
[01:54:29] DEBUG - EchoTask{echo1}[2]#doTask()- read....
[01:54:29] DEBUG - EchoTask{echo1}[2]#doTask()- received msg>> hongseong
[01:54:29] DEBUG - EchoTask#doEnd()- EchoTask{echo1}[2]
[01:54:29] DEBUG - EchoTask#taskPassivate()- EchoTask{echo1}[2]
[01:54:29] DEBUG - SSLProxyTask#doEnd()- SSLProxyTask{proxy}[4]
[01:54:29] DEBUG - SSLProxyTask#taskPassivate()- SSLProxyTask{proxy}[4]
[01:54:29] DEBUG - Handler{7130}[4] process time : 0.01 sec
[01:54:29] DEBUG - Handler{7131}[4] process time : 0.37 sec
written by Jeon HongSeong [hsjeon70@dreamwiz.com]
1
References
This
Article Source : jNetServer1.0-app.jar