Java中使用连接池管理Socket
/**
*
*/
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”
补充:软件开发 , Java ,