zoukankan      html  css  js  c++  java
  • java之Socket多线程传递对象

    服务器端利用线程池回复客户端:

    public class Server implements Runnable {
    
        private final ServerSocket server;
        private final ExecutorService pool;
    
        public Server(int port, int poolSize) throws IOException {
            this.server = new ServerSocket(port);
            this.pool = Executors.newFixedThreadPool(poolSize);
        }
    
        public void run() {
            while (true) {
                Socket socket;
                try {
                    socket = this.server.accept();
                    pool.execute(new ServerRepleyLines(socket));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    
        }
    
    }
    ServerRepleyLines类具体代码,里面用到的数据类可以暂不考虑:
    public class ServerRepleyLines extends Thread {
    
        private DBHelper helper;
        private Socket client;
    
        public ServerRepleyLines(Socket client) {
            this.client = client;
            this.helper = new DBHelper();
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                ObjectInputStream fin = new ObjectInputStream(
                        new BufferedInputStream(this.client.getInputStream()));
                try {
                    TicketInfo qa = (TicketInfo) fin.readObject();
                    System.out.println(qa.getStarting_station());
                    if (qa.getType() == 2) {
                        LineInfos lines = this.helper.getAllLineInfos(
                                qa.getStarting_station(), qa.getTerminal_station(),qa.getDate_time().toString());
                        ObjectOutputStream fout = new ObjectOutputStream(
                                client.getOutputStream());
                        fout.writeObject(lines);
                        fout.flush();
                    }
                    fin.close();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    
    

    客户端Socket传输对象:

    public LineInfos sendAndGetLinesInfo(TicketInfo ticket) {
            Socket socket = this.CreateASocket(2);
            try {
                ObjectOutputStream fout = new ObjectOutputStream(
                        socket.getOutputStream());
                fout.writeObject(ticket);
                fout.flush();
                LineInfos lines = null;
                ObjectInputStream fin = new ObjectInputStream(
                        new BufferedInputStream(socket.getInputStream()));
                try {
                    lines = (LineInfos) fin.readObject();
                    fin.close();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                fout.close();
                return lines;
    
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    核心代码就这些:

    调用开启线程:Server server=new Server2(6668,3);  server.start();

     
  • 相关阅读:
    安装和强行卸载fuse
    Elasticsearch snapshot 备份的使用方法 【备忘】
    MYSQL数据仓库infobright【备忘】
    Tomcat8 启动慢 Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [53,161] milliseconds
    python爬虫实例
    JDK1.8 JVM参数配置
    QQ登录用到的URL
    CAS5.3.X 配置备忘
    Nexus3忘记admin密码时的解决办法
    CentOS7利用systemctl添加自定义系统服务
  • 原文地址:https://www.cnblogs.com/nannanITeye/p/3461662.html
Copyright © 2011-2022 走看看