http://blog.csdn.net/mchange/article/details/8815679

 

web.xml中配置监听

 

[html] view plaincopy
 
  1. <!-- 开启socket服务 -->  
  2. <listener>  
  3.   <listener-class>com.ainong.socket.server.SocketServiceLoader</listener-class>  
  4. </listener>  

 

 

监听类

 

[java] view plaincopy
 
  1. import javax.servlet.ServletContext;  
  2. import javax.servlet.ServletContextEvent;  
  3. import javax.servlet.ServletContextListener;  
  4.   
  5. /** 
  6.  * 将socket service随tomcat启动 
  7.  * @author zhangzhongwen 
  8.  * 
  9.  */  
  10. public class SocketServiceLoader implements ServletContextListener{  
  11.     //socket server 线程  
  12.     private SocketThread socketThread;  
  13.       
  14.     @Override  
  15.     public void contextDestroyed(ServletContextEvent arg0) {  
  16.         if(null!=socketThread && !socketThread.isInterrupted())  
  17.            {  
  18.             socketThread.closeSocketServer();  
  19.             socketThread.interrupt();  
  20.            }  
  21.     }  
  22.   
  23.     @Override  
  24.     public void contextInitialized(ServletContextEvent arg0) {  
  25.         // TODO Auto-generated method stub  
  26.         if(null==socketThread)  
  27.         {  
  28.          //新建线程类  
  29.          socketThread=new SocketThread(null);  
  30.          //启动线程  
  31.          socketThread.start();  
  32.         }  
  33.     }  
  34.   
  35. }  

 

 

 

生产socket线程类:

[java] view plaincopy
 
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.io.PrintWriter;  
  5. import java.net.ServerSocket;  
  6. import java.net.Socket;  
  7.   
  8. import javax.servlet.ServletContext;  
  9.   
  10. /** 
  11.  * socket 线程类 
  12.  * @author zhangzhongwen 
  13.  * 
  14.  */  
  15. public class SocketThread extends Thread{  
  16.     private ServerSocket serverSocket = null;  
  17.       
  18.     public SocketThread(ServerSocket serverScoket){  
  19.         try {  
  20.             if(null == serverSocket){  
  21.                 this.serverSocket = new ServerSocket(8877);  
  22.                 System.out.println("socket start");  
  23.             }  
  24.         } catch (Exception e) {  
  25.             System.out.println("SocketThread创建socket服务出错");  
  26.             e.printStackTrace();  
  27.         }  
  28.   
  29.     }  
  30.       
  31.     public void run(){  
  32.         while(!this.isInterrupted()){  
  33.             try {  
  34.                 Socket socket = serverSocket.accept();  
  35.                   
  36.                 if(null != socket && !socket.isClosed()){     
  37.                     //处理接受的数据  
  38.                     new SocketOperate(socket).start();  
  39.                 }  
  40.                 socket.setSoTimeout(30000);  
  41.                   
  42.             }catch (Exception e) {  
  43.                 e.printStackTrace();  
  44.             }  
  45.         }  
  46.     }  
  47.       
  48.       
  49.     public void closeSocketServer(){  
  50.        try {  
  51.             if(null!=serverSocket && !serverSocket.isClosed())  
  52.             {  
  53.              serverSocket.close();  
  54.             }  
  55.        } catch (IOException e) {  
  56.         // TODO Auto-generated catch block  
  57.         e.printStackTrace();  
  58.        }  
  59.      }  
  60.       
  61.       
  62.       
  63.       
  64.       
  65.       
  66.       
  67. }  

 

 

处理客户端发送报文的类

 

[java] view plaincopy
 
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5. import java.io.PrintWriter;  
  6. import java.net.Socket;  
  7.   
  8. import javax.servlet.ServletContext;  
  9.   
  10. /** 
  11.  * 多线程处理socket接收的数据 
  12.  * @author zhangzhongwen 
  13.  * 
  14.  */  
  15. public class SocketOperate extends Thread{  
  16.      private Socket socket;  
  17.        
  18.      public SocketOperate(Socket socket) {  
  19.         this.socket=socket;  
  20.      }  
  21.      @SuppressWarnings("unused")  
  22.     public void run()  
  23.      {  
  24.          try{     
  25.   
  26.                 InputStream in= socket.getInputStream();    
  27.   
  28.                 PrintWriter out=new PrintWriter(socket.getOutputStream());    
  29.   
  30.                 //BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));   
  31.    
  32.                 while(true){    
  33.                     //读取客户端发送的信息  
  34.                     String strXML = "";  
  35.                     byte[] temp = new byte[1024];  
  36.                     int  length = 0;  
  37.                     while((length = in.read(temp)) != -1){  
  38.                         strXML += new String(temp,0,length);  
  39.                     }  
  40.                     if("end".equals(strXML)){     
  41.                         System.out.println("准备关闭socket");  
  42.                         break;   
  43.                     }  
  44.                     if("".equals(strXML))  
  45.                         continue;  
  46.                       
  47.                     System.out.println("客户端发来:"+strXML.toString());   
  48.                       
  49.                     MethodHandler mh = new MethodHandler(ReadXML.readXML(strXML.toString()));  
  50.                     String resultXML = mh.getResultXML();  
  51.                     System.out.println("返回:"+resultXML.toString());  
  52.                       
  53.                       
  54.                     if(!"".equals(resultXML)){  
  55.                         out.print(resultXML);  
  56.                         out.flush();   
  57.                         out.close();  
  58.                     }  
  59.   
  60.                         
  61.                 }     
  62.                 socket.close();     
  63.                 System.out.println("socket stop.....");  
  64.   
  65.             }catch(IOException ex){     
  66.   
  67.             }finally{     
  68.                 
  69.             }     
  70.      }  
  71. }  
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 pcwiki 的頭像
    pcwiki

    pcwiki的部落格

    pcwiki 發表在 痞客邦 留言(0) 人氣()