/********************************************************************
* 项目名称 :rochoc
* 包名称 :rochoc.net.security
* 文件名称 :SSLServer
* 编写者 : LuckyStar
* 编写日期 :2008-2-13
* 程序功能(类)描述 :用于安全通讯的服务Socket,采用java中的SSLServerSocket
*
* 程序变更日期 :
* 变更作者 :
* 变更说明 :
********************************************************************/packagerochoc.net.security;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.Socket;importjava.security.KeyStore;importjava.security.SecureRandom;importjavax.net.ssl.KeyManagerFactory;importjavax.net.ssl.SSLContext;importjavax.net.ssl.SSLServerSocket;importjavax.net.ssl.TrustManagerFactory;/*** 类名:SSLServer
* 类描述:安全通讯的服务端
* 编写者 :luoc
* 编写日期 :2005-6-30
* 主要public成员变量:
* 主要public方法:
**/publicclassSSLServerimplementsRunnable
{/***构造函数说明:
*参数说明:
**/publicSSLServer()
{
init();
}/*** 方法名称:init
* 方法功能:初始化服务Socket
* 参数说明:
* 返回:void
* 作者:luoc
* 日期:2005-6-30
**/publicvoidinit()
{
String type="TLS";//类型String keyf="..\\key\\srvstore";//key文件路径String trustf="..\\key\\mytrust";
String pass="123456";//密码intport=2001;//端口try{//初始化上下文SSLContext ctx=SSLContext.getInstance(type);
KeyManagerFactory kmf=KeyManagerFactory.getInstance("SunX509");
TrustManagerFactory tmf=TrustManagerFactory.getInstance("SunX509");
KeyStore ks=KeyStore.getInstance("JKS");
KeyStore tks=KeyStore.getInstance("JKS");//载入keystoreks.load(newFileInputStream(keyf),pass.toCharArray());
tks.load(newFileInputStream(trustf),pass.toCharArray());
kmf.init(ks,pass.toCharArray());
tmf.init(tks);
ctx.init(kmf.getKeyManagers(),tmf.getTrustManagers(),newSecureRandom());
ss=(SSLServerSocket)ctx.getServerSocketFactory().createServerSocket(port);
ss.setNeedClientAuth(true);//客户端要认证}catch(Exception e) {
e.printStackTrace();
}
}/*** 方法名称:newListener
* 方法功能:创建服务器监听
* 参数说明:
* 返回:void
* 作者:luoc
* 日期:2005-6-30
**/privatevoidnewListener()
{
(newThread(this)).start();
}/*** 重载方法:run 处理客户端的请求
* 参阅:@seejava.lang.Runnable#run()
* 参数说明:
**/publicvoidrun()
{
Socket socket=null;//accept a connectiontry{
socket=ss.accept();
}catch(IOException e)
{
System.out.println("Class Server died:"+e.getMessage());
e.printStackTrace();return;
}//create a new thread to accept the next connectionnewListener();//process connectiontry{
OutputStream out=socket.getOutputStream();
InputStream in=socket.getInputStream();//read data from clientbytebuff[]=newbyte[512];bytedata[]=newbyte[1024];
System.out.println("buff len="+buff.length);intlen=0;intstartpos=0;while((len=in.read(buff))!=-1)
{//读联欢数据if(len==1&&buff[0]==TranTool.DATA_END)//数据结束标志break;
data=TranTool.byteDynExt(data,buff,len,startpos);
System.out.println("read len:"+len+"data:["+newString(buff,0,len)+"]");
startpos+=len;
}
System.out.println("recv from client:[");
System.out.print(newString(data,0,startpos));
System.out.println("] data end.");//send message to clientout.write((startpos+"data success receive.").getBytes());
out.write(TranTool.DATA_END);
System.out.println("success echo is send.");
out.flush();
}catch(IOException e)
{
e.printStackTrace();return;
}finally{try{
socket.close();
}catch(IOException e)
{}
}
}/*全局变量*/SSLServerSocket ss=null;//测试函数publicstaticvoidmain(String args[])
{
System.out.println("init SSLServer
");
SSLServer srv=newSSLServer();newThread(srv).start();
System.out.println("SSLServer listener begin.");
}
}