public class test1_server { /* * 網絡編程(練習) * 客戶端向服務器寫字符串(鍵盤錄入),服務器(多線程)將字符串反轉後寫回,客戶端再次讀取到是反轉後的字符串 */ public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(54321); System.out.println("服務器啓動,綁定端口"); while (true) { final Socket socket = server.accept(); new Thread(){ public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream ps = new PrintStream(socket.getOutputStream()); String line = br.readLine(); line = new StringBuilder(line).reverse().toString(); //字符串反轉 鏈式編程 ps.println(line); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } } }
public class test1_client { /* * 網絡編程(練習) * 客戶端向服務器寫字符串(鍵盤錄入),服務器(多線程)將字符串反轉後寫回,客戶端再次讀取到是反轉後的字符串 */ public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); Socket socket = new Socket("localhost", 54321); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream ps = new PrintStream(socket.getOutputStream()); ps.println(sc.nextLine()); System.out.println(br.readLine()); socket.close(); } }
import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class test2_server { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(12333); System.out.println("服務器啓動"); while (true) { final Socket socket = server.accept(); new Thread(){ public void run() { try { InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); PrintStream ps = new PrintStream(socket.getOutputStream()); String fileName = br.readLine(); // 讀取文件名 File dir = new File("update"); dir.mkdir(); // 建立文件夾 File file = new File(dir, fileName); // 父及目錄 filename if (file.exists()) { ps.println("存在"); socket.close(); return; } else { ps.println("不存在"); } FileOutputStream fos = new FileOutputStream(file); byte[] arr =new byte[8192]; int len; while ((len = is.read(arr))!= -1) { fos.write(arr, 0, len); } fos.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } } }
public class test2_client { public static void main(String[] args) throws Exception { File file = getFile(); Socket socket = new Socket("localhost",12333); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream ps = new PrintStream(socket.getOutputStream()); ps.println(file.getName()); // 將文件名發送給服務器 String result = br.readLine(); if ("存在".equals(result)) { System.out.println("您上傳的文件已存在!請不要重複上傳"); socket.close(); return; } else { FileInputStream fis = new FileInputStream(file); byte[] arr = new byte[8192]; int len; while ((len =fis.read(arr)) != -1) { ps.write(arr,0,len); } fis.close(); socket.close(); } } private static File getFile() { Scanner sc = new Scanner(System.in); System.out.println("請輸入一個文件路徑:"); while (true) { String line = sc.nextLine(); File file = new File(line); if (file.exists()) { if (file.isDirectory()) { System.out.println("你輸入的是文件夾,請從新輸入"); } else { return file; } }else { System.out.println("你輸入的文件路徑不存在,請從新輸入"); } } } }