java學習之路 之 網路程式設計-練習題
阿新 • • 發佈:2019-01-24
package com.atguigu.javase.net; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; // 客戶端給伺服器傳送一個字串,伺服器收到後給客戶端回覆訊息確認 public class NetTest { @Test public void server() { ServerSocket server = null; // 服務套接字 Socket socket1 = null; InputStream is = null; // 網路輸入流 InputStreamReader isr = null; // 轉換流 BufferedReader bufReader = null; // 緩衝流 OutputStream os = null; // 網路輸出流 OutputStreamWriter osw = null; // 轉換流 BufferedWriter bufWriter = null; // 緩衝流 try { server = new ServerSocket(9999); // 伺服器繫結在埠號9999上 socket1 = server.accept(); // accept方法會導致程式阻塞, 等待客戶端的連線請求, 一旦接受就返回一個Socket物件 System.out.println(socket1); is = socket1.getInputStream(); isr = new InputStreamReader(is); bufReader = new BufferedReader(isr); String line = bufReader.readLine(); System.out.println(line); os = socket1.getOutputStream(); osw = new OutputStreamWriter(os); bufWriter = new BufferedWriter(osw); bufWriter.write("我收到了你的訊息,請放心!!"); bufWriter.newLine(); bufWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { bufWriter.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } try { server.close(); } catch (Exception e2) { } } } @Test public void client() { Socket socket2 = null; OutputStream os = null; OutputStreamWriter osw = null; BufferedWriter bufWriter = null; InputStream is = null; InputStreamReader isr = null; BufferedReader bufReader = null; try { socket2 = new Socket("127.0.0.1", 9999); // 建立Socket物件的過程,就是發起請求的過程, 連線到伺服器需要的資訊是主機(IP)和埠Port System.out.println(socket2); os = socket2.getOutputStream(); // 獲取輸出流物件 osw = new OutputStreamWriter(os); bufWriter = new BufferedWriter(osw); bufWriter.write("你好伺服器, 我是客戶端 !!"); bufWriter.newLine(); bufWriter.flush(); // 把資料刷入網線 is = socket2.getInputStream(); isr = new InputStreamReader(is); bufReader = new BufferedReader(isr); String line = bufReader.readLine(); System.out.println(line); } catch (Exception e) { e.printStackTrace(); } finally { try { bufWriter.close(); } catch (Exception e2) { } try { bufReader.close(); } catch (Exception e2) { } try { socket2.close(); } catch (Exception e2) { } } } } class ExerServer { // 伺服器主動給客戶端 傳送一個UTF8字串, 客戶端接收到這個字串以反向處理再返回給伺服器 public static void main(String[] args) { ServerSocket server = null; Socket socket1 = null; BufferedWriter bufWriter = null; BufferedReader bufReader = null; try { server = new ServerSocket(8888);// 物件一旦建立成功, 標誌此程式和埠8888繫結 while (true) { System.out.println("伺服器正在8888埠監聽........"); try { socket1 = server.accept(); bufWriter = new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream(), "UTF8")); bufReader = new BufferedReader(new InputStreamReader(socket1.getInputStream(), "UTF8")); bufWriter.write("隨便寫個字串, 傳送給客戶端, 我是伺服器"); bufWriter.newLine(); bufWriter.flush(); String line = bufReader.readLine(); System.out.println(line); } catch (Exception e) { } finally { try { bufReader.close(); } catch (Exception e2) { } try { bufWriter.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } } } } catch (Exception e) { e.printStackTrace(); } finally { try { server.close(); } catch (Exception e2) { } } } } class ExerClient { // 客戶端接收到這個字串以反向處理再返回給伺服器 public static void main(String[] args) { Socket socket2 = null; BufferedReader bufReader = null; BufferedWriter bufWriter = null; try { socket2 = new Socket("localhost", 8888); bufReader = new BufferedReader(new InputStreamReader(socket2.getInputStream(), "UTF8")); bufWriter = new BufferedWriter(new OutputStreamWriter(socket2.getOutputStream(), "UTF8")); String line = bufReader.readLine(); System.out.println(line); line = new StringBuilder(line).reverse().toString(); bufWriter.write(line); bufWriter.newLine(); bufWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { socket2.close(); } catch (Exception e2) { } } } } package com.atguigu.javase.net; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; class TestServer2 { // 接收從客戶端傳送的檔案, 並儲存在本地, 處理完後,給客戶端反饋 public static void main(String[] args) { ServerSocket server = null; Socket socket1 = null; InputStream is = null; // 網路輸入流 FileOutputStream fos = null; // 檔案輸出流 OutputStream os = null; // 網路輸出流 try { server = new ServerSocket(6666); socket1 = server.accept(); is = socket1.getInputStream(); os = socket1.getOutputStream(); fos = new FileOutputStream("girl2.jpg"); byte[] buf = new byte[8192]; int realCount = is.read(buf); // 從網路輸入流中讀取資料 while (realCount != -1) { fos.write(buf, 0, realCount); // 把讀到的資料寫入本地檔案中 realCount = is.read(buf); // 網路的另一端關閉了輸出 返回-1 } os.write("接收完畢".getBytes()); // 通過網路輸出流反饋一個字串 os.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (Exception e2) { } try { is.close(); } catch (Exception e2) { } try { os.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } try { server.close(); } catch (Exception e2) { } } } } class TestClient2 { // 傳送一個本檔案到伺服器端 , 再接收伺服器反饋 public static void main(String[] args) { Socket socket2 = null; FileInputStream fis = null; // 讀本地檔案的輸入流 OutputStream os = null; // 傳送檔案,網路輸出流 InputStream is = null; // 網路輸入流 try { String host = "localhost"; int port = 6666; socket2 = new Socket(host, port); os = socket2.getOutputStream(); is = socket2.getInputStream(); fis = new FileInputStream("girl.jpg"); byte[] buf = new byte[8192]; int realCount = fis.read(buf); //從檔案輸入流中獲取資料 while (realCount != -1) { // 1) 處理 os.write(buf, 0, realCount); // 把讀到的資料直接寫入網路輸出流 // 2) 繼續 realCount = fis.read(buf); } socket2.shutdownOutput(); // 關閉輸出流, 傳送一個終止訊號給網路另一端 realCount = is.read(buf); // 從網路輸入流中獲取反饋資訊,是一個字串, 所以需要解碼 String line = new String(buf, 0, realCount); System.out.println(line); } catch (Exception e) { e.printStackTrace(); } finally { try { fis.close(); } catch (Exception e2) { } try { os.close(); } catch (Exception e2) { } try { is.close(); } catch (Exception e2) { } try { socket2.close(); } catch (Exception e2) { } } } } //使用UDP傳送一個字串 package com.atguigu.javase.net; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import org.junit.Test; public class NetTest3 { @Test public void udpSender() throws Exception { DatagramSocket ds = new DatagramSocket(); byte[] data = "來一個數據,是一個字串".getBytes(); DatagramPacket dp = new DatagramPacket(data, 0, data.length, InetAddress.getByName("127.0.0.1"), 10000); ds.send(dp); // 把資料包傳送給硬體 ds.close(); } @Test public void udpReceiver() throws Exception { DatagramSocket ds = new DatagramSocket(10000); // 繫結埠 byte[] buf = new byte[8192]; DatagramPacket dp = new DatagramPacket(buf, buf.length); // 接收資料 ds.receive(dp); // 接收到的資料儲存在DatagramPacket物件中 String string = new String(buf, 0, dp.getLength()); System.out.println(string); ds.close(); } } package com.atguigu.javase.net; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import org.junit.Test; // 建立一個伺服器可以接收從客戶端傳來的資訊,並在控制檯上列印輸出, // 客戶端接收鍵盤輸入的字串,併發送到伺服器,這裡需要建立多執行緒,以防止多個客戶端連線伺服器時,其中一個伺服器發生// 故障時,伺服器仍能執行 class TcpService implements Runnable { private Socket socket1; public TcpService(Socket socket1) { this.socket1 = socket1; } @Override public void run() { BufferedReader bufReader = null; try { bufReader = new BufferedReader(new InputStreamReader(socket1.getInputStream())); String line = bufReader.readLine(); while (line != null) { System.out.println(line); line = bufReader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } } } } class TestServer1 { public static void main(String[] args) { ServerSocket server = null; try { server = new ServerSocket(7777); while (true) { System.out.println("伺服器在埠7777監聽..."); Socket socket1 = server.accept(); new Thread(new TcpService(socket1)).start(); } } catch (Exception e) { e.printStackTrace(); } finally { try { server.close(); } catch (IOException e) { e.printStackTrace(); } } } } class TestClient1 { public static void main(String[] args) { Socket socket2 = null; BufferedReader bufReader = null; BufferedWriter bufWriter = null; try { socket2 = new Socket("localhost", 7777); bufReader = new BufferedReader(new InputStreamReader(System.in)); bufWriter = new BufferedWriter(new OutputStreamWriter(socket2.getOutputStream())); String line = bufReader.readLine(); while (line != null) { bufWriter.write(line); bufWriter.newLine(); bufWriter.flush(); line = bufReader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { bufWriter.close(); } catch (Exception e2) { } try { socket2.close(); } catch (Exception e2) { } } } } package com.atguigu.javase.net; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; /* * 建立伺服器和客戶端,使得伺服器和客戶端可以互相打字進行交流 */ class Tcp implements Runnable { // 接收從網路獲取到的資訊,並在控制檯上,列印輸出 private Socket socket; public Tcp(Socket socket) { this.socket = socket; } @Override public void run() { BufferedReader bufReader = null; try { bufReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = bufReader.readLine(); while (line != null) { System.out.println(line); line = bufReader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { bufReader.close(); } catch (Exception e2) { } try { socket1.close(); } catch (Exception e2) { } } } } class TestServer3 { public static void main(String[] args) { // 建立客戶端 ServerSocket server = null; Socket socket1 = null; BufferedReader br = null; // 鍵盤輸入流 BufferedReader br1 = null; // 網路輸入流 BufferedWriter bw =null; // 網路輸出流 try { server = new ServerSocket(8888); System.out.println("伺服器正在埠8888監聽......"); socket1 = server.accept(); br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream())); br1 = new BufferedReader(new InputStreamReader(socket1.getInputStream())); // 建立執行緒,並執行 new Thread(new Tcp(socket1)).start(); // 鍵盤輸入的字串並傳給客戶端 String line = br.readLine(); while (line != null) { bw.write(line); bw.newLine(); bw.flush(); line = br.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { br1.close(); } catch (IOException e) { } try { bw.close(); } catch (IOException e) { } try { socket1.close(); } catch (IOException e) { } try { server.close(); } catch (IOException e) { } } } } class TestClient3 { public static void main(String[] args) { Socket socket2 = null; BufferedReader br = null; // 網路輸入流 BufferedReader br2 = null; // 鍵盤輸入流 BufferedWriter bw = null; // 網路輸出流 try { socket2 = new Socket("127.0.0.1" ,8888); br = new BufferedReader(new InputStreamReader(socket2.getInputStream())); br2 = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(socket2.getOutputStream())); // 建立執行緒並執行 new Thread(new Tcp(socket2)).start(); // 接收鍵盤傳入的字串,併發送到伺服器端 String line2 = br2.readLine(); while (line2 != null) { bw.write(line2); bw.newLine(); bw.flush(); line2 = br2.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { try { bw.close(); } catch (IOException e) { } try { br2.close(); } catch (IOException e) { } try { br.close(); } catch (IOException e) { } try { socket2.close(); } catch (IOException e) { } } } }