fork(1) download
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class Server {
  5. public static void main(String[] args) {
  6. try {
  7. int port = Integer.parseInt(args[0]);
  8. System.out.println("簡易檔案接收...");
  9. System.out.printf("將接收檔案於連接埠: %d%n", port);
  10.  
  11. ServerSocket serverSkt = new ServerSocket(port);
  12.  
  13. while(true) {
  14. System.out.println("傾聽中....");
  15.  
  16. Socket clientSkt = serverSkt.accept();
  17.  
  18. System.out.printf("與 %s 建立連線%n",
  19. clientSkt.getInetAddress().toString());
  20.  
  21. // 取得檔案名稱
  22. String fileName = new BufferedReader(
  23. clientSkt.getInputStream())).readLine();
  24.  
  25. System.out.printf("接收檔案 %s ...", fileName);
  26.  
  27. BufferedInputStream inputStream =
  28. new BufferedInputStream(clientSkt.getInputStream());
  29. BufferedOutputStream outputStream =
  30.  
  31. int readin;
  32. while((readin = inputStream.read()) != -1) {
  33. outputStream.write(readin);
  34. Thread.yield();
  35. }
  36.  
  37. outputStream.flush();
  38. outputStream.close();
  39. inputStream.close();
  40.  
  41. clientSkt.close();
  42.  
  43. System.out.println("\n檔案接收完畢!");
  44. }
  45. }
  46. catch(Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class Server is public, should be declared in a file named Server.java
public class Server {
       ^
1 error
stdout
Standard output is empty