Socket通讯解决并发采用线程池

学生我作业交完了,代码分享一下!不多说什么了哈

服务器端ThreadServer

ThreadPool.java

package thread;
import java.util.LinkedList;

public class ThreadPool extends ThreadGroup{
	private boolean isClosed=false;//线程池是否关闭
	private LinkedList workQueue;//表示工作队列
	private static int threadPoolID;//表示线程池ID
	private int threadID;//表示工作线程ID
	
	public ThreadPool(int poolSize){//poolSize指定线程池中的工作线程数
		super("ThreadPool-"+(threadPoolID++));
		setDaemon(true);
		workQueue=new LinkedList();//创建并启动工作线程
		for(int i=0;i
EchoServer.java
package thread;

import java.io.*;
import java.net.*;
/**
 * 
 * @author Mouse
 *
 */

public class EchoServer{
	private int port=8000;
	private ThreadPool threadPool;//线程池
	private ServerSocket serverSocket;
	private final int POOL_SIZE=4;//单个Cpu时线程池中工作的数目
	
	public EchoServer()throws IOException{
		serverSocket=new ServerSocket(port);
		//创建线程池
		//Rumtime的availableProcessors()方法返回当前系统的cup数目
		//系统的cpu越多,线程池中的数目也越多
		threadPool=new ThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
		System.out.println("服务器启动!");
	}
	public void service(){
		while (true) {
			Socket socket=null;
			try {
				socket=serverSocket.accept();
				threadPool.execute(new Handler(socket));//把与客户通讯的任务交给线程
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			
			
		}
		
	}
	
	public static void main(String args[])throws IOException{
	
			new EchoServer().service();
	
		
	}
	

}

//负责与单个客户通讯任务
class Handler implements Runnable{
	private Socket socket;
	public Handler(Socket socket){
		this.socket=socket;
	}
	
	private PrintWriter getWriter(Socket socket)throws IOException{
		OutputStream socketOut=socket.getOutputStream();	
		return new PrintWriter(socketOut, true);//参数为true表示每写一行,PrintWriter缓存就自动溢出,把数据写到目的
	}
 	
	private BufferedReader getReader(Socket socket)throws IOException {
		InputStream socketIn=socket.getInputStream();
		return new BufferedReader(new InputStreamReader(socketIn));
		
	}
	public String echo(String msg){
		return "echo:"+msg;
		
	}
	public void run(){
		try {
			//得到客户端的地址和端口号
			System.out.println("New connection accepted"+socket.getInetAddress()+":"+socket.getPort());
			BufferedReader br=getReader(socket);
			PrintWriter pw=getWriter(socket);
			
			String msg=null;
			while ((msg=br.readLine())!=null) {
				System.out.println(msg);
				pw.println(echo(msg));
				if(msg.endsWith("bye")){
					break;
				}
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(socket!=null){
					socket.close();
				}
				
			} catch (IOException  e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
}

客户端

EchoClient.java

package thread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class EchoClient {
	private String host="localhost";
	private int port=8000;
	private Socket socket;
	Socket[]sockets=new Socket[1000];
	public EchoClient()throws IOException{
		
//		for(int i=0;i<1000;i++){
			socket=new Socket(host,port);
//		}
		
	}
	public static void main(String args[])throws IOException{
		new EchoClient().talk();
		
	}
	
	private PrintWriter getWriter(Socket socket)throws IOException{
		OutputStream socketOut=socket.getOutputStream();	
		return new PrintWriter(socketOut, true);
	}
 	
	private BufferedReader getReader(Socket socket)throws IOException {
		InputStream socketIn=socket.getInputStream();
		return new BufferedReader(new InputStreamReader(socketIn));
		
	}
	
	public void talk()throws IOException{
		try {
			BufferedReader br=getReader(socket);
			PrintWriter pw=getWriter(socket);
			BufferedReader localReader=new BufferedReader(new InputStreamReader(System.in));
			String msg=null;
			while ((msg=localReader.readLine())!=null) {
				pw.println(msg);
				System.out.println(br.readLine());
				
				if(msg.equals("bye")){
					break;
				}
			}
			
			
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				socket.close();
			} catch (IOException e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
	}
	
	

}

代码的链接地址:http://download.csdn.net/detail/u010343650/9448391

你可能感兴趣的:(后端)