创建一个客户端线程:
package cn.com.chat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * 客户端线程,用于向服务端发送数据 * * @author Administrator * */ public class ClientThread extends Thread { Socket socket = null; BufferedReader br = null; PrintWriter pw = null; public ClientThread(Socket socket) { this.socket = socket; br = new BufferedReader(new InputStreamReader(System.in)); try { pw = new PrintWriter(socket.getOutputStream(), true); } catch (IOException e) { e.printStackTrace(); } } public void run() { try { while (true) { String str = br.readLine();// 从控制台输入 pw.println(str);// 向服务端发送数据 } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (pw != null) { pw.close(); } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
创建一个服务器线程:
package cn.com.chat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * f服务端的线程,用来向客户端发送信息 * * @author Administrator * */ public class ServerThread extends Thread { Socket socket = null; BufferedReader br = null; PrintWriter pw = null; public ServerThread(Socket socket) { this.socket = socket; br = new BufferedReader(new InputStreamReader(System.in)); try { pw = new PrintWriter(socket.getOutputStream(), true); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { try { while (true) { String str = br.readLine();// 从控制台输入 pw.println(str);// 向客户端发送信息 } } catch (IOException e) { e.printStackTrace(); }finally { if (pw != null) { pw.close(); } if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
客户端启动:
package cn.com.chat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; /** * 客户端的主线程,用来接收服务端信息 * * @author Administrator * */ public class Client { public static void main(String[] args) { Socket socket = null; BufferedReader br = null; boolean flag=true; try { socket = new Socket("localhost", 9002); new ClientThread(socket).start();// 启动Client线程 br = new BufferedReader(new InputStreamReader( socket.getInputStream())); // while (true) // { // String str = br.readLine(); // if (str.equals("bye")) // { // break; // } // System.out.println("服务端说:" + str); // // } while(flag){ String str = br.readLine(); if (str.equals("bye")) { flag=false; } System.out.println("服务端说:" + str); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
服务器端启动:
package cn.com.chat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; /** * 服务器的主线程,用于接收客户端的信息 * * @author Administrator * */ public class Server { public static void main(String[] args) { ServerSocket server = null; Socket socket = null; BufferedReader br = null; boolean flag=true; try { server = new ServerSocket(9002); socket = server.accept(); new ServerThread(socket).start();// 启动server线程 br = new BufferedReader(new InputStreamReader( socket.getInputStream())); // String str = br.readLine(); // while (!str.equals("bye")) // { // System.out.println("客户端说:" + str); // str = br.readLine(); // // } while(flag){ String str = br.readLine(); if(str.equals("bye")){ flag=false; } System.out.println("客户端说:"+str); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (server != null) { try { server.close(); } catch (IOException e) { e.printStackTrace(); } } } } }