1.客户端
package Thread; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Click { public static void main(String[] args) throws UnknownHostException, IOException { Click click=new Click(); click.start(); } private Socket socket; public Click() throws UnknownHostException, IOException { socket=new Socket("localhost", 8088);//连接指定的地址和端口 } public void start() throws IOException { System.out.println("欢迎你,"+socket.getInetAddress()); PrintWriter pw=null; //创建一条线程读取 服务器端发送过来的信息。 Thread t=new Thread(new ServerHandler()); t.setDaemon(true);//守护线程 t.start(); try { // 输出流,将客户端的消息发送出去 pw=new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"utf-8"),true); Scanner scan=new Scanner(System.in); while(true){ pw.println(scan.nextLine()); } } catch (UnsupportedEncodingException e) { System.out.println("io流异常"); } finally { if(socket!=null) { socket.close(); } } } //读取服务端发送的消息 class ServerHandler implements Runnable{ public void run() { BufferedReader br=null; try { br=new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")); System.out.println(br.readLine()); } catch (IOException e) { System.out.println("IOException"); } } } }
2.服务端
package Thread; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Server { public static void main(String[] args) throws IOException { Server server=new Server(); server.start(); } //成员变量 private ExecutorService threadPool; private ServerSocket serverSocket; private List<PrintWriter>allOut; public Server() throws IOException {//初始化变量 serverSocket=new ServerSocket(8088); threadPool=Executors.newFixedThreadPool(3); allOut=new ArrayList<PrintWriter>(); } public void start() { try { while(true) { System.out.println("等待客户端连接"); Socket socket=serverSocket.accept();//监听客户端的连接 //每连接一条线程 threadPool.execute(new ClickHandler(socket)); } } catch (IOException e) { System.out.println("socket Exection"); } } class ClickHandler implements Runnable{ private Socket socket; public ClickHandler(Socket socket) { this.socket=socket; } //线程同步 public synchronized void addOut(PrintWriter pw) { allOut.add(pw); } public synchronized void pwClose(PrintWriter pw) { pw.close(); } public void run() { BufferedReader br; PrintWriter pw=null; try { System.out.println(socket.getInetAddress()+"连接成功"); br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8")); //使用输出流将信息发到客户端 pw=new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"utf-8"), true); addOut(pw); String message=null; while((message=br.readLine())!=null) { for(PrintWriter p:allOut) { if(p!=pw) { p.println(socket.getInetAddress()+":"+message); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { pwClose(pw); if(socket!=null) { try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } }
end