Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks
/********************************************************************
 * 项目名称    :rochoc   <p>
 * 包名称      :rochoc.net.security <p>
 * 文件名称    :SSLServer   <p>
 * 编写者      : LuckyStar    <p>
 * 编写日期    :2008-2-13    <p>
 * 程序功能(类)描述 :用于安全通讯的服务Socket,采用java中的SSLServerSocket<p>
 *
 * 程序变更日期   :
 * 变更作者    :
 * 变更说明    :
*******************************************************************
*/
package rochoc.net.security;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.KeyStore;
import java.security.SecureRandom;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.TrustManagerFactory;

/**
 * 类名:SSLServer  <p>
 * 类描述:安全通讯的服务端 <p>
 * 编写者 :luoc<p>
 * 编写日期 :2005-6-30<p>
 * 主要public成员变量:<p>
 * 主要public方法:   <p>
 *
*/

public class SSLServer implements Runnable
{
    
/**
     *构造函数说明:       <p>
     *参数说明:   <p>
    *
*/
    
public SSLServer()
    {       
        init();
    }
   
    
/**
    * 方法名称:init<p>
    * 方法功能:初始化服务Socket            <p>
    * 参数说明: <p>
    * 返回:void <p>
    * 作者:luoc
    * 日期:2005-6-30
    *
*/
    
public void init()
    {
        String type
="TLS";//类型
        String keyf="..\\key\\srvstore";//key文件路径
        String trustf="..\\key\\mytrust";
        String pass
="123456";//密码
        int port=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");
            
//载入keystore
            ks.load(new FileInputStream(keyf),pass.toCharArray());
            tks.load(
new FileInputStream(trustf),pass.toCharArray());
            kmf.init(ks,pass.toCharArray());
            tmf.init(tks);
            ctx.init(kmf.getKeyManagers(),tmf.getTrustManagers(),
new SecureRandom());
            ss
=(SSLServerSocket)ctx.getServerSocketFactory().createServerSocket(port);
            ss.setNeedClientAuth(
true);//客户端要认证
        }catch (Exception e) {
      e.printStackTrace();
     }       
    }
   
    
/**
    * 方法名称:newListener<p>
    * 方法功能:创建服务器监听            <p>
    * 参数说明: <p>
    * 返回:void <p>
    * 作者:luoc
    * 日期:2005-6-30
    *
*/
    
private void newListener()
    {
        (
new Thread(this)).start();
    }
   
    
/**
     * 重载方法:run 处理客户端的请求<p>
     * 参阅:
@see java.lang.Runnable#run() <p>
     * 参数说明: <p>
    *
*/
    
public void run()
    {
        Socket socket
=null;
        
//accept a connection
        try
        {
            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 connection
        newListener();
       
        
//process connection
        try
        {
            OutputStream out
=socket.getOutputStream();
            InputStream in
=socket.getInputStream();
            
//read data from client    
            byte buff[]=new byte [512]; 
            
byte data[]=new byte [1024];
            System.out.println(
"buff len="+buff.length);
            
int len=0;
            
int startpos=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:["+new String(buff,0,len)+"]");
                startpos
+=len;
            }              
            System.out.println(
"recv from client:[");                                  
            System.out.print(
new String(data,0,startpos));           
            System.out.println(
"] data end.");
            
//send message to client
            out.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;
   
    
//测试函数
    public static void main(String args[])
    {
        System.out.println(
"init SSLServer");
        SSLServer srv
=new SSLServer();
        
new Thread(srv).start();      
        System.out.println(
"SSLServer listener begin.");
    }
}
posted on 2008-02-13 22:08 礼物 阅读(527) 评论(0)  编辑  收藏 所属分类: CA