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{
}
}
}
版权声明:本文为Mchange原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。