服務端
java
package socket; 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.ServerSocket; import java.net.Socket; /** *socket服務端 * */ public class TCPServer { public static void main(String[] args) { try { // 建立一個服務端socket實例,並綁定指定端口,同時監聽該端口 ServerSocket serversocket = new ServerSocket(7777); Socket socket = null; System.out.println("服務端已啓動,正在監聽......"); int count = 0;//記錄客戶端的數量 //循環創建客戶端監聽 while(true){ //調用accept()啓動端口監聽,等待客戶端請求 socket = serversocket.accept();//監聽7777端口 //建立一個新線程 ServerThread st = new ServerThread(socket); //啓動線程 st.start(); count++; System.out.println("客戶端數量"+count); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
服務端已啓動,正在監聽......緩存
客戶端數量1socket
服務端捕獲到客戶端信息:######用戶名:admin 密碼:1234ide
客戶端數量2this
服務端捕獲到客戶端信息:######用戶名:admin 密碼:1234用戶名:user 密碼:567.net
客戶端一線程
package socket; 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.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; public class TCPClient { public static void main(String[] args) { try { //建立客戶端socket,指定服務端地址和端口 Socket socket = new Socket("localhost", 7777); //創建一個輸出流,用於向服務端發送信息 OutputStream os = socket.getOutputStream(); //建立一個打印流幷包裝輸出流 PrintWriter pw = new PrintWriter(os); pw.write("用戶名:admin 密碼:1234"); pw.flush();//刷新緩存 //關閉資源 socket.shutdownOutput(); //建立輸入流,用於讀取服務端響應信息 InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String serinfo = br.readLine(); while(!"".equals(serinfo) && serinfo != null){ System.out.println("客戶端獲取服務端響應信息:*****"+serinfo); serinfo = br.readLine(); } socket.shutdownInput(); br.close(); isr.close(); is.close(); pw.close(); os.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
客戶端二code
package socket; 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.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; public class TCPClient { public static void main(String[] args) { try { //建立客戶端socket,指定服務端地址和端口 Socket socket = new Socket("localhost", 7777); //創建一個輸出流,用於向服務端發送信息 OutputStream os = socket.getOutputStream(); //建立一個打印流幷包裝輸出流 PrintWriter pw = new PrintWriter(os); pw.write("用戶名:admin 密碼:1234"); pw.write("用戶名:user 密碼:567"); pw.flush();//刷新緩存 //關閉資源 socket.shutdownOutput(); //建立輸入流,用於讀取服務端響應信息 InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String serinfo = br.readLine(); while(!"".equals(serinfo) && serinfo != null){ System.out.println("客戶端獲取服務端響應信息:*****"+serinfo); serinfo = br.readLine(); } socket.shutdownInput(); br.close(); isr.close(); is.close(); pw.close(); os.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
服務端線程處理類server
package socket; 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 ServerThread extends Thread { // 與本線程相關的Socket Socket socket = null; public ServerThread(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 { is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String info = br.readLine(); while(!"".equals(info) && info != null){ System.out.println("服務端捕獲到客戶端信息:######"+info); info = br.readLine(); } //關閉相關資源 socket.shutdownInput(); //服務端建立輸出流,響應客戶端請求 os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("用戶名密碼正確,歡迎登錄"); pw.flush(); } catch (IOException e) { e.printStackTrace(); }finally{ try { //關閉相關資源 socket.shutdownOutput(); //關閉其餘資源 if(pw != null) pw.close(); if(os != null) os.close(); if(br != null) br.close(); if(isr != null) isr.close(); if(is != null) is.close(); if(socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { } }