写了一个java 多线程socket,能够运行,是服务器每3秒能够接受10次一个客户端发送的信息,当打开第二个客户端时就不行,提示端口已被占用。但我想实现的是 一个服务器能够监听10个不同客户端,有一个客户端掉线就能够通过服务器通知其他客户端。求高手指教,怎么把每个客户端当成一个线程,服务器端口被占用的问题如何解决?下面是代码。
服务器端
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.*;
import java.util.concurrent.*;
public class MultiThreadServer {
static int workThreadNum = 0;
private ServerSocket serverSocket;
private ExecutorService executorService;//线程池
private final int POOL_SIZE=10;//单个CPU线程池大小
private int port=8821;
private static final String SERVER = "192.168.1.109";
public void service() throws IOException{
serverSocket=new ServerSocket(port);
executorService=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
if (serverSocket == null) {
System.out.println("创建ServerSocket失败!");
return;
}
while(true){
Socket socket=null;
try {
//接收客户连接,只要客户进行了连接,就会触发accept();从而建立连接
socket=serverSocket.accept();
executorService.execute(new Handler(socket));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public ServerSocket startListenUserReport(int port) {
try {
ServerSocket serverSocket = new ServerSocket();
if (!serverSocket.getReuseAddress()) {
serverSocket.setReuseAddress(true);
}
serverSocket.bind(new InetSocketAddress(SERVER, port));
System.out.println("开始在" + serverSocket.getLocalSocketAddress()
+ "上侦听用户的心跳包请求!");
return serverSocket;
} catch (IOException e) {
System.out.println("端口" + port + "已经被占用!");
if (serverSocket != null) {
if (!serverSocket.isClosed()) {
try {
serverSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
return serverSocket;
}
class Handler implements Runnable {
private Socket socket;
/**
* 构造函数,从调用者那里取得socket
*
*/
public Handler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
workThreadNum = workThreadNum + 1;
System.out.println("第" + workThreadNum + "个的连接:" + socket.getInetAddress() + ":" + socket.getPort());
while (true) {
try {
ObjectInputStream ois = new ObjectInputStream(
new BufferedInputStream(socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//System.out.println("用户已经断开连接!");
} finally {
if (socket != null) {
try {
// 断开连接
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
客户端
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultiThreadClient {
public void ThreadClient() {
int numTasks = 10;
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < numTasks; i++) {
exec.execute(createTask(i));
}
}
//定义一个任务
private static Runnable createTask(final int taskID) {
return new Runnable() {
Socket server;
public void run() {
System.out.println("Task " + taskID + ":start");
try {
server = new Socket(InetAddress.getLocalHost(), 8821);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
ObjectOutputStream out=null;
try {
out = new ObjectOutputStream(server.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.writeObject(HRip.getHostIP());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
};
}
}
class NodeSever implements Runnable{
MultiThreadServer client = null;
private boolean stoped = false;
private int interval = 1000;
public NodeSever() {
}
public MultiThreadServer getNodeClient() {
if (client == null) {
client = new MultiThreadServer();
}
return client;
}
public void run() {
while (!stoped) {
try {
NodeClient.send();
synchronized (this) {
this.wait(interval);
}
} catch (Exception e) {
}
}
}
public void destroy() {
stoped = true;
synchronized (this) {
this.notify();
}
}
} |