java web 项目启动开启 socket server

web.xml中配置监听

  <!-- 开启socket服务 -->
  <listener>
    <listener-class>com.ainong.socket.server.SocketServiceLoader</listener-class>
  </listener>

监听类

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 将socket service随tomcat启动
 * @author zhangzhongwen
 *
 */
public class SocketServiceLoader implements ServletContextListener{
	//socket server 线程
	private SocketThread socketThread;
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		if(null!=socketThread && !socketThread.isInterrupted())
		   {
		    socketThread.closeSocketServer();
		    socketThread.interrupt();
		   }
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
	    if(null==socketThread)
	    {
	     //新建线程类
	     socketThread=new SocketThread(null);
	     //启动线程
	     socketThread.start();
	    }
	}

}



生产socket线程类:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.servlet.ServletContext;

/**
 * socket 线程类
 * @author zhangzhongwen
 *
 */
public class SocketThread extends Thread{
	private ServerSocket serverSocket = null;
	
	public SocketThread(ServerSocket serverScoket){
		try {
			if(null == serverSocket){
				this.serverSocket = new ServerSocket(8877);
				System.out.println("socket start");
			}
		} catch (Exception e) {
			System.out.println("SocketThread创建socket服务出错");
			e.printStackTrace();
		}

	}
	
	public void run(){
		while(!this.isInterrupted()){
			try {
				Socket socket = serverSocket.accept();
				
				if(null != socket && !socket.isClosed()){   
					//处理接受的数据
					new SocketOperate(socket).start();
				}
				socket.setSoTimeout(30000);
				
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public void closeSocketServer(){
	   try {
		    if(null!=serverSocket && !serverSocket.isClosed())
		    {
		     serverSocket.close();
		    }
	   } catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	   }
	 }
	
	
	
	
	
	
	
}



处理客户端发送报文的类

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.servlet.ServletContext;

/**
 * 多线程处理socket接收的数据
 * @author zhangzhongwen
 *
 */
public class SocketOperate extends Thread{
	 private Socket socket;
	 
	 public SocketOperate(Socket socket) {
		this.socket=socket;
	 }
	 @SuppressWarnings("unused")
	public void run()
	 {
		 try{   

			 	InputStream in= socket.getInputStream();  

				PrintWriter out=new PrintWriter(socket.getOutputStream());  

				//BufferedReader wt = new BufferedReader(new InputStreamReader(System.in)); 
 
				while(true){  
					//读取客户端发送的信息
					String strXML = "";
					byte[] temp = new byte[1024];
					int  length = 0;
					while((length = in.read(temp)) != -1){
						strXML += new String(temp,0,length);
					}
					if("end".equals(strXML)){   
						System.out.println("准备关闭socket");
						break; 
					}
					if("".equals(strXML))
						continue;
					
					System.out.println("客户端发来:"+strXML.toString()); 
					
					MethodHandler mh = new MethodHandler(ReadXML.readXML(strXML.toString()));
					String resultXML = mh.getResultXML();
					System.out.println("返回:"+resultXML.toString());
					
					
					if(!"".equals(resultXML)){
						out.print(resultXML);
						out.flush(); 
						out.close();
					}

					  
				}   
				socket.close();   
				System.out.println("socket stop.....");

			}catch(IOException ex){   

			}finally{   
			  
			}   
	 }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值