编程专栏
2012-03-12 15:40
采纳率: 0%
浏览 459
已采纳

java socket连接池的问题

应用场景:
前端使用JSF作为表现层,浏览器客户端发出请求时我必须先通过socket与集群服务器进行通信之后在将服务器的响应信息进行解析再展现给客户端,为了减少系统开销,我打算在socket这一层使用socket连接池的方式实现。
但是目前遇到一个问题,我从池中取出一个socket后不能判断出该socket是否已经被服务器端关闭了连接,如果服务器端已经关闭了连接,我必须创建一个新的socket,然后对上一次发送失败的请求进行重发处理。
但是,在判断socket是否已经被服务器端关闭这一点,是在是做不到啊!
JAVA API中的socket.isClosed(),socket.isConnected(),以及socket.sendUrgentData()检测的方式都不起作用,麻烦由这方面经验的大哥给些指点。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • long13131462101 2012-03-12 16:29
    已采纳

    [code="java"]package com.duoguo.util;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.LinkedList;
    import java.util.List;
    /**

    • @author shyboy(chao.shen@duoguo.cn) * / public class ConnectionPoolHandler implements Runnable { private Socket socket; @SuppressWarnings("unchecked") private static List pool = new LinkedList(); /*
    • 处理连接 / public void handleConnection() { try { PrintWriter out = new PrintWriter(socket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(socket .getInputStream())); String line = in.readLine(); BufferedReader bufferedReader = new BufferedReader(new FileReader( line)); String value = null; while ((value = bufferedReader.readLine()) != null) { out.println(value); } bufferedReader.close(); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public static void processRequest(Socket socket) { synchronized (pool) { pool.add(pool.size(), socket); pool.notifyAll(); } } /
    • (non-Javadoc) *
    • @see java.lang.Runnable#run()
      */
      @Override
      public void run() {
      while (true) {
      synchronized (pool) {
      while (pool.isEmpty()) {
      try {
      pool.wait();
      } catch (InterruptedException e) {
      e.printStackTrace();
      }
      }
      socket = (Socket) pool.remove(0);
      }
      handleConnection();
      }
      }
      }

      /**
      *
      /
      package com.duoguo.socket;
      import java.io.IOException;
      import java.net.ServerSocket;
      import java.net.Socket;
      import com.duoguo.util.ConnectionPoolHandler;
      /
      *

    • @author shyboy(chao.shen@duoguo.cn)
      *
      /
      public class PoolMultiServer {
      private int maxConnections;// 最大连接数
      private int listenerPort;// 监听端口号
      private ServerSocket serverSocket;
      /
      *

    • 构造方法
      *

    • @param maxConnections

    • :最大连接数

      • @param listenerPort
    • :监听端口号
      /
      public PoolMultiServer(int maxConnections, int listenerPort) {
      this.maxConnections = maxConnections;
      this.listenerPort = listenerPort;
      }
      /
      *

    • 接受连接
      /
      public void acceptConnection() {
      try {
      serverSocket = new ServerSocket(listenerPort, maxConnections);
      while (true) {
      Socket socket = serverSocket.accept();
      handleConnection(socket);
      }
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      /
      *

    • 处理连接
      *

    • @param socket

    • :套接字
      /
      public void handleConnection(Socket socket) {
      ConnectionPoolHandler.processRequest(socket);
      }
      public void setUpHandlers() {
      for (int i = 0; i < maxConnections; i++) {
      ConnectionPoolHandler connectionPoolHandler = new ConnectionPoolHandler();
      new Thread(connectionPoolHandler, "处理器" + i).start();
      }
      }
      /
      *

    • @param args
      */
      public static void main(String[] args) {
      PoolMultiServer poolMultiServer = new PoolMultiServer(5, 9999);
      poolMultiServer.setUpHandlers();
      poolMultiServer.acceptConnection();
      }
      }

      /**
      *
      /
      package com.duoguo.socket;
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.io.PrintWriter;
      import java.net.Socket;
      import java.net.UnknownHostException;
      /
      *

    • @author shyboy(chao.shen@duoguo.cn)
      *
      /
      public class Client {
      private String hostIp;// 客户端主机IP
      private int hostPort;// 客户端主机端口号
      private BufferedReader in;// 客户端输入流
      private PrintWriter out;// 客户端输出流
      /
      *

    • 构造方法
      *

    • @param hostip

    • :IP地址

      • @param hostPort
    • :端口号
      /
      public Client(String hostip, int hostPort) {
      this.hostIp = hostip;
      this.hostPort = hostPort;
      }
      /
      *

    • 建立连接
      /
      public void setUpConnection() {
      try {
      Socket client = new Socket(hostIp, hostPort);
      in = new BufferedReader(new InputStreamReader(client
      .getInputStream()));// 客户端输入流
      out = new PrintWriter(client.getOutputStream());// 客户端输出流
      } catch (UnknownHostException e) {
      System.out.println("找不到相应的主机!");
      e.printStackTrace();
      } catch (IOException e) {
      System.out.println("抛出相应的流异常信息");
      e.printStackTrace();
      }
      }
      /
      *

    • 获取文件
      *

    • @param fileName

    • :文件名称

      • @return String / public String getFile(String fileName) { StringBuilder sb = new StringBuilder(); out.println(fileName); out.flush(); String line = null; try { System.out.println("客户端连接成功!"); while ((line = in.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { System.out.println("文件读入失败!"); e.printStackTrace(); } return sb.toString(); } /*
    • 断开连接
      /
      public void tearDownConnection() {
      try {
      out.close();// 关闭输出流
      in.close();// 关闭输入流
      } catch (IOException e) {
      System.out.println("断开连接失败!");
      e.printStackTrace();
      }
      }
      /
      *

    • @param args
      */
      public static void main(String[] args) {
      Client client = new Client("127.0.0.1", 9999);
      client.setUpConnection();
      String fileContent = client.getFile("D:\hello.txt");
      System.out.println("文件内容为:" + "\n" + fileContent);
      client.tearDownConnection();
      }
      }

    作者“shenjichao2009”

    [/code]现成的代码

    点赞 打赏 评论

相关推荐 更多相似问题