import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * <p> * 基於socket通信-服務端 * <p> * * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a> * @version * @since 2017年1月12日 */ public class Server { private static ServerSocket SERVER_SOCKET =null;; static{ try { SERVER_SOCKET = new ServerSocket(9090); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { try { System.out.println("******服務器已啓動,等待客戶端鏈接*****"); Socket socket = null; while(true){ //循環監聽客戶端的鏈接 socket = SERVER_SOCKET.accept(); //新建一個線程ServerSocket,並開啓 new ServerSocketThread(socket).start(); } } catch (IOException e) { e.printStackTrace(); } } }
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; /** * <p> * Socket線程服務類 * <p> * * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a> * @version * @since 2017年1月13日 */ public class ServerSocketThread extends Thread { private Socket socket; public ServerSocketThread(Socket socket) { this.socket = socket; } @Override public void run() { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; OutputStream os = null; PrintWriter pw = null; try { // socket獲取字節輸入流 is = socket.getInputStream(); // 將字節輸入流轉字符輸入流 isr = new InputStreamReader(is); // 將字符輸入流轉行輸入流 br = new BufferedReader(isr); // String message = null; while ((message = br.readLine()) != null) { System.out.println("客戶端發來消息:" + message); } // 必須先關閉輸入流才能獲取下面的輸出流 socket.shutdownInput(); // 獲取輸出流 os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("歡飲您進入socket"); pw.flush(); // 關閉輸入流 socket.shutdownOutput(); } catch (Exception e) { e.printStackTrace(); } finally { // 關閉資源 if (pw != null) { pw.close(); } try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (isr != null) { isr.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (isr != null) { isr.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
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; import java.net.UnknownHostException; /** * <p> * 基於socket通信-客戶端 * <p> * * @author <a href="mailto:yangkj@corp.21cn.com">yangkj</a> * @version * @since 2017年1月12日 */ public class Client { public static void main(String[] args) { Socket socket = null; OutputStream os = null; PrintWriter pw = null; InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; try { socket = new Socket("127.0.0.1", 9090); // 獲取輸出流向服務端寫入數據 os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("用戶名:admin 密碼:123"); pw.flush(); socket.shutdownOutput(); // 獲取輸入流接受服務端返回的信息 is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String message = null; while ((message = br.readLine()) != null) { System.out.println("服務器說:" + message); } socket.shutdownInput(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (isr != null) { isr.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } if (pw != null) { pw.close(); } try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } }