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

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

 

最新文章

  1. strace 解决库依赖问题
  2. CI框架源码阅读笔记2 一切的入口 index.php
  3. MS SQLServer 操作XML语句的存储过程
  4. Maven配置不成功
  5. Vijos P1459 车展 treap求任意区间中位数
  6. thinkphp常用Config.php配置项
  7. php mssql 中文各种乱码
  8. Ubuntu14.04下安装QQ 国际版
  9. OpenJudge 2749 分解因数
  10. 配置虚拟主机并更改Apache默认解析路径
  11. 关于firefox启动就崩溃的问题
  12. 内功心法 -- java.util.ArrayList<E> (3)
  13. 论林耐斯-Linux系统的重要性
  14. Redtiger SQL注入练习(二)
  15. C++STL——优先队列
  16. request.environ.get('wsgi.websocket')
  17. python实现最大重叠子串的查找
  18. 文本编辑器js插件
  19. swift的一些东西
  20. html5--6-59 其他常用CSS属性

热门文章

  1. mongoDB副本集+分片集群
  2. mybatis组合实体查询
  3. Unity 移动 和 旋转 [小结]
  4. hdu2067 小兔的棋盘
  5. python3 杂记
  6. 3 不用IDE开发groovy
  7. python3 练习3
  8. oracle merge into函数中插入clob字段
  9. java字符串转Date
  10. shell中各种括号()、(())、[]、[[]]、{}的作用和区别