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();

 

转载于:https://www.cnblogs.com/nannanITeye/p/3461662.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值