当前位置 主页 > 网站技术 > 代码类 >

    javaweb启动时启动socket服务端代码实现

    栏目:代码类 时间:2019-11-28 12:03

    这篇文章主要介绍了javaweb启动时启动socket服务端代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    说明:在javaweb项目启动的时候启动socket服务端,可以接受客户端发送过来的数据,客户端可以反复断开连接

    1.新建SocketThread线程类

    package com.wlw.modules.startImplement.web;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    /**
     * socket 线程类
     * @Author: zhouhe
     * @Date: 2019/4/16 14:58
     */
    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) {
    
          }
        }
      }
    
    
      public void closeSocketServer(){
        try {
          if(null!=serverSocket && !serverSocket.isClosed())
          {
            serverSocket.close();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }

    2.新建SocketOperate,用于接受客户端传来的数据

    package com.wlw.modules.startImplement.web;
    
    import com.wlw.modules.lhjh.socket.tstandard.MessageParsing;
    
    import java.io.BufferedInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.Socket;
    
    /**
     * 多线程处理socket接收的数据
     * @Author: zhouhe
     * @Date: 2019/4/16 14:59
     */
    public class SocketOperate extends Thread {
      private Socket socket;
    
      public SocketOperate(Socket socket) {
        this.socket=socket;
      }
    
      @Override
      public void run() {
        try {
          //封装输入流(接收客户端的流)
          BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
          DataInputStream dis = new DataInputStream(bis);
          byte[] bytes = new byte[1]; // 一次读取一个byte
    
          String ret = "";
          while (dis.read(bytes) != -1) {
            ret += MessageParsing.bytesToHexString(bytes) + "";
            if (dis.available() == 0) { //一个请求
    
              System.out.println("转换为字符串后:"+MessageParsing.hexStringToString(ret));
              System.out.println("转为map后的数据:"+MessageParsing.stringToMap(MessageParsing.hexStringToString(ret)));
    
              ret = "";
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          System.out.println("client is over");
          if (socket != null) {
            try {
              socket.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }
      }
    }