用ServerSocket類與Socket類實現客戶端與服務端的互動
阿新 • • 發佈:2019-02-18
ServerSocket類,此類實現服務端的套接字,Socket類,此類實現客戶端的套接字,而套接字就是兩臺機器間通訊的端點,所以就用ServerSocket類和Socket類實現客戶端與服務端的互動,下面先來檢視ServerSocket類的api
看到構造方法,首先想到要使用要先new一個,然後方法裡可以繫結dao特定埠,這裡以埠8888為例,建立一個服務端監聽8888埠,然後獲取連線著的ip地址,輸出連線的地址,程式碼如下
可以通過電腦自帶的命令提示符使用telnet命令連線測試,命令telnet加上ip加上埠號就可以連線了public static void test1() { try { ServerSocket ss = new ServerSocket(8888); Socket sk = ss.accept(); //獲取連線的ip地址 System.out.println(sk.getInetAddress()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
上面通過使用ServerSocket返回一個Socket,然後我們通過檢視Socket的api看到可以獲取一個輸入流,然後用Scanner包裝,獲取我們輸入的內容,然後用while迴圈,然後在控制檯輸出內容
然後還是跟上個方法一樣,連線上服務端,輸入telnet ip地址 埠然後回車public static void test2() { try { System.out.println("服務已啟動"); ServerSocket ss = new ServerSocket(8888); Socket s = ss.accept(); System.out.println(s.getInetAddress()); InputStream is = s.getInputStream(); Scanner sc = new Scanner(is); while (sc.hasNextLine()) { String str = sc.nextLine(); System.out.println(str); } s.close(); is.close(); sc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
會出現如下黑色介面,按“ctrl+]”
會出現如下介面,繼續按回車
然後出現如下介面,我們輸入的內容才能在命令提示符裡顯示出來
上面的程式碼方法使我們能在服務端發訊息給客戶端,而客戶端不能發訊息給服務端,下面介紹客戶端能發訊息給服務端,服務端不能發訊息給客戶端,我們new一個ServerSocket,然後監聽8888埠,然後用Socket獲取一個輸出流(把我們在控制檯寫的內容輸出到服務端),然後new一個scanner來包裝System.in(獲取鍵盤的輸入流),然後把鍵盤的輸入流輸出到客戶端
public static void test3() { try { System.out.println("服務啟動"); ServerSocket ss = new ServerSocket(8888); Socket socket = ss.accept(); PrintWriter pw = new PrintWriter(socket.getOutputStream()); Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ String str = sc.nextLine(); pw.println(str); pw.flush(); } socket.close(); sc.close(); pw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
可以看到只能控制檯傳送訊息,而服務端傳送訊息無效,接下來介紹在服務端能傳送接收訊息,客戶端也能接收發送訊息,新建兩個類
在ServerSocket(服務端)裡輸入以下程式碼,既獲取輸入流又獲取輸出流,然後讀取鍵盤輸入的內容
public static void test1() {
try {
ServerSocket ss = new ServerSocket(8888);
while(true){
Socket socket = ss.accept();
Scanner scanner = new Scanner(socket.getInputStream());
PrintWriter pw = new PrintWriter(socket.getOutputStream());
while(scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println(str);
Scanner s = new Scanner(System.in);
if (s.hasNextLine()) {
String msg = s.nextLine();
pw.println(msg);
pw.flush();
}
}
socket.close();
scanner.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
然後在Socket(客戶端)裡輸入以下程式碼,既獲取輸入流又獲取輸出流,然後獲取鍵盤輸入的內容
public static void test1(){
try {
Socket socket = new Socket("自己的ip地址xxx.xxx.xxx.xxx", 8888);
Scanner scanner = new Scanner(socket.getInputStream());
PrintWriter pw = new PrintWriter(socket.getOutputStream());
Scanner s = new Scanner(System.in);
while(s.hasNextLine()){
String str = s.nextLine();
if (str.equals("exit")) {
System.out.println("我要退出了");
socket.close();
s.close();
System.exit(0);
}
pw.println(str);
pw.flush();
String msg = scanner.nextLine();
System.out.println(msg);
}
s.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
在以上兩個類裡寫好後先啟動ServerSocket,然後再啟動Socket,就可以實現服務端與客戶端的一來一去對講功能