Java Socket 讀取伺服器端返回資料
阿新 • • 發佈:2019-02-04
//客戶端 //1、建立客戶端Socket,指定伺服器地址和埠 Socket socket =newSocket("localhost",10086); //2、獲取輸出流,向伺服器端傳送資訊 OutputStream os = socket.getOutputStream();//位元組輸出流 PrintWriter pw =newPrintWriter(os);//將輸出流包裝成列印流 pw.write("使用者名稱:admin;密碼:123"); pw.flush(); socket.shutdownOutput(); //3、獲取輸入流,並讀取伺服器端的響應資訊 InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String info = null; while((info=br.readLine())!null){ System.out.println("我是客戶端,伺服器說:"+info); } //4、關閉資源 br.close(); is.close(); pw.close(); os.close(); socket.close();
原文章:http://www.cnblogs.com/rocomp/p/4790340.html