下面贴出我的代码:
客户端:
package com.lip; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class Client { /** * Author: Lip * 客户端 */ public static void main(String[] args) { Socket socket = null; BufferedReader br = null; PrintWriter pw = null; Scanner scanner = new Scanner(System.in);// 从键盘读取 try { // 创建客户端socket socket = new Socket(Server.HOST, Server.PORT); // 读取从客户端发来的消息 br = new BufferedReader(new InputStreamReader( socket.getInputStream())); // 写入信息到服务器端 pw = new PrintWriter( new BufferedWriter(new OutputStreamWriter( socket.getOutputStream()))); new ReadServerMessage(br);// 从服务器读取消息 while (true) { String temp = scanner.nextLine();// 从键盘读取一行 pw.println(temp);// 写到服务器 pw.flush(); if (temp.equals("q")) break; } } catch (Exception e) { e.printStackTrace(); } finally { try { System.out.println("close......"); br.close(); pw.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } class ReadServerMessage extends Thread//从服务器读取消息 { BufferedReader bReader; Socket socket; public ReadServerMessage(BufferedReader br) { this.bReader = br; start(); } public void run() { String str = ""; while (true)//一直等待着服务器的消息 { try { str = bReader.readLine(); if (str.equals("q")) { bReader.close(); socket.close(); break; } } catch (IOException e) { e.printStackTrace(); } System.out.println("Server Message:" + str); } } }
package com.lip; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { /** * Author:Lip Desc:SSD8_exercise1 */ public static int PORT = 8000; public static String HOST = "127.0.0.1"; public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(PORT); // 等待请求,无请求一直等待 while (true) { System.out.println("Waiting Client"); socket = serverSocket.accept();// 接受请求 System.out.println("Client Conect!"); new ThreadedServer(socket); } } catch (Exception e) { try { socket.close(); } catch (IOException e1) { e1.printStackTrace(); } } finally { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package com.lip; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class ThreadedServer extends Thread { private Socket socket = null; private BufferedReader br = null; private PrintWriter pw = null; private Scanner scanner = null; public ThreadedServer(Socket s) { socket = s; try { br = new BufferedReader(new InputStreamReader( socket.getInputStream())); pw = new PrintWriter( new BufferedWriter(new OutputStreamWriter( socket.getOutputStream())), true); scanner = new Scanner(System.in);// 从键盘读取数据 start(); } catch (Exception e) { e.printStackTrace(); } } public void run() { new ReadClientMessage(br, socket);//读取客户端数据 while (true)//向客户端发送数据 { try { pw.println(scanner.nextLine()); pw.flush(); } catch (Exception e) { try { br.close(); pw.close(); socket.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } } class ReadClientMessage extends Thread { BufferedReader bReader; Socket socket; public ReadClientMessage(BufferedReader br,Socket s) { this.bReader = br; this.socket=s; start(); } public void run() { String str = ""; while (true) { try { str = bReader.readLine(); if (str.equals("q")) { bReader.close(); socket.close(); break; } } catch (IOException e) { e.printStackTrace(); } System.out.println("Client Message:" + str); } } }