import java.io.*; import java.net.Socket; import java.util.Scanner; /** * Created by Administrator on 2017/6/27. * 客戶端 */ public class Client { public static void main(String[] args) { Socket socket = null; OutputStream os = null; //PrintWriter pw = null; Scanner scanner = null; InputStream is = null; BufferedReader br = null; try { //建立客戶端Socket,指定服務器地址和端口 socket = new Socket("localhost", 8888); //獲取輸出流,向服務器發送信息 os = socket.getOutputStream();//字節輸出流 // pw = new PrintWriter(os);//將輸出流包裝爲打印流 // pw.write("用戶名:cw,密碼:0813"); // pw.flush(); System.out.println("客戶端,請輸入多個字符:"); scanner = new Scanner(System.in); String str = scanner.next(); os.write(str.getBytes()); os.flush(); //socket.shutdownInput(); //獲取輸入流,並讀取服務器端的響應信息 // is = socket.getInputStream(); // br = new BufferedReader(new InputStreamReader(is)); // String info = null; // while ((info = br.readLine()) != null) { // System.out.println("服務器:" + info); // info=br.readLine(); // } } catch (Exception e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (scanner != null) { scanner.close(); } /* if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } }*/ } } }
Server服務器端:java
import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * Created by Administrator on 2017/6/27. * 基於TCP協議的Socket通訊,實現用戶登錄 * 服務器端 */ public class Server { public static void main(String[] args) { try{ //建立一個服務器端Socket,指定並監聽端口 ServerSocket serverSocket=new ServerSocket(8888); Socket socket=null; int count =0; System.out.println("*****服務器即將啓動,等待客戶端的鏈接*****"); //循環監聽等待客戶端的鏈接 while(true){ //調用accept()方法開始監聽,等待客戶端的鏈接 socket = serverSocket.accept(); //建立一個線程 ServerThread serverThread=new ServerThread(socket); //啓動線程 serverThread.start(); count++; System.out.println("客戶端的數量:"+count); InetAddress address = socket.getInetAddress(); System.out.println("當前客戶端的IP:"+address.getHostAddress()); } }catch (Exception e){ e.printStackTrace(); } } }
鏈接過程: 根據鏈接啓動的方式以及本地套接字要鏈接的目標,套接字之間的鏈接過程能夠分爲三個步驟:服務器監聽,客戶端請求,鏈接確認。 (1)服務器監聽:是服務器端套接字並不定位具體的客戶端套接字,而是處於等待鏈接的狀態,實時監控網絡狀態。 (2)客戶端請求:是指由客戶端的套接字提出鏈接請求,要鏈接的目標是服務器端的套接字。爲此,客戶端的套接字必須首先描述它要鏈接的服務器的套接字,指出服務器端套接字的地址和端口號,而後就向服務器端套接字提出鏈接請求。 (3)鏈接確認:是指當服務器端套接字監聽到或者說接收到客戶端套接字的鏈接請求,它就響應客戶端套接字的請求,創建一個新的線程,把服務器端套接字的描述發給客戶端,一旦客戶端確認了此描述,鏈接就創建好了。而服務器端套接字繼續處於監聽狀態,繼續接收其餘客戶端套接字的鏈接請求。 socket鏈接過程圖解 demo: Client客戶端: import java.io.*; import java.net.Socket; import java.util.Scanner; /** * Created by Administrator on 2017/6/27. * 客戶端 */ public class Client { public static void main(String[] args) { Socket socket = null; OutputStream os = null; //PrintWriter pw = null; Scanner scanner = null; InputStream is = null; BufferedReader br = null; try { //建立客戶端Socket,指定服務器地址和端口 socket = new Socket("localhost", 8888); //獲取輸出流,向服務器發送信息 os = socket.getOutputStream();//字節輸出流 // pw = new PrintWriter(os);//將輸出流包裝爲打印流 // pw.write("用戶名:cw,密碼:0813"); // pw.flush(); System.out.println("客戶端,請輸入多個字符:"); scanner = new Scanner(System.in); String str = scanner.next(); os.write(str.getBytes()); os.flush(); //socket.shutdownInput(); //獲取輸入流,並讀取服務器端的響應信息 // is = socket.getInputStream(); // br = new BufferedReader(new InputStreamReader(is)); // String info = null; // while ((info = br.readLine()) != null) { // System.out.println("服務器:" + info); // info=br.readLine(); // } } catch (Exception e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (scanner != null) { scanner.close(); } /* if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } }*/ } } } Server服務器端: import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * Created by Administrator on 2017/6/27. * 基於TCP協議的Socket通訊,實現用戶登錄 * 服務器端 */ public class Server { public static void main(String[] args) { try{ //建立一個服務器端Socket,指定並監聽端口 ServerSocket serverSocket=new ServerSocket(8888); Socket socket=null; int count =0; System.out.println("*****服務器即將啓動,等待客戶端的鏈接*****"); //循環監聽等待客戶端的鏈接 while(true){ //調用accept()方法開始監聽,等待客戶端的鏈接 socket = serverSocket.accept(); //建立一個線程 ServerThread serverThread=new ServerThread(socket); //啓動線程 serverThread.start(); count++; System.out.println("客戶端的數量:"+count); InetAddress address = socket.getInetAddress(); System.out.println("當前客戶端的IP:"+address.getHostAddress()); } }catch (Exception e){ e.printStackTrace(); } } } ServerThread線程: import java.io.*; import java.net.Socket; import java.util.Scanner; /** * Created by Administrator on 2017/6/27. * 服務器線程處理類 */ public class ServerThread extends Thread { //和本線程相關的Socket Socket socket = null; public ServerThread(Socket socket) { this.socket = socket; } //線程執行的操做,響應客戶端的請求2 @Override public void run() { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; OutputStream os = null; //PrintWriter pw = null; Scanner scanner = null; try { System.out.println("*****線程****"); //獲取輸入流,並讀取客戶端信息 is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String info = null; while ((info = br.readLine()) != null) {//循環讀取客戶端信息 System.out.println("來自客戶端:" + info); br.readLine(); } // socket.shutdownInput(); //socket.shutdownInput(); //獲取輸出流,響應服務端的請求 os = socket.getOutputStream(); System.out.println("服務器,請輸入多個字符:"); scanner = new Scanner(System.in); String str = scanner.next(); os.write(str.getBytes()); // os.flush(); // pw = new PrintWriter(os); //pw.write("elcome!"); // pw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (isr != null) { try { isr.close(); } catch (IOException e) { e.printStackTrace(); } } if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (scanner != null) { scanner.close(); } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }