fork download
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.InputStreamReader;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. public class TCPServer implements Runnable{
  11. public static final int serverPort = 80;
  12. public void run(){
  13. try{
  14. System.out.println("대기중..");
  15. ServerSocket serverSocket = new ServerSocket(serverPort);
  16.  
  17. while(true)
  18. {
  19. Socket sock = serverSocket.accept();
  20. System.out.println("수신중....");
  21. try{
  22. // 클라이언트에서 보낸 데이터를 서버에서 수신함
  23. BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  24. String str = in.readLine();
  25. System.out.println("수신중인 파일 이름 : " + str);
  26. File f = new File("C:\\Users\\Administrator\\workspace\\download", str+".txt");
  27. byte[] buf = new byte[1024];
  28. while(sock.getInputStream().read(buf)>0)
  29. {
  30. output.write(buf);
  31. output.flush();
  32. }
  33. in.close();
  34. output.close();
  35. System.out.println(str+".txt 수신완료");
  36.  
  37.  
  38. // *************** 새로 추가한 코드 입니다
  39. String message = "Hello from Server";
  40. try {
  41. System.out.println("Server : Sending: '" + message + "'");
  42. PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(sock.getOutputStream())),true);
  43.  
  44. out.println(message);
  45. System.out.println("Server : Sent.");
  46. System.out.println("Server : Done.");
  47.  
  48. } catch(Exception e) {
  49. System.out.println("S: Error");
  50. }
  51. // ***********************************************
  52. }
  53. catch(Exception e){
  54. System.out.println("서버 에러!!");
  55. e.printStackTrace();
  56. }
  57. finally{
  58. sock.close();
  59. }
  60. }
  61. }
  62. catch(Exception e){
  63. e.printStackTrace();
  64. }
  65. }
  66. public static void main(String[] argv){
  67. Thread doit = new Thread(new TCPServer());
  68. doit.start();
  69. }
  70. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:10: error: class TCPServer is public, should be declared in a file named TCPServer.java
public class TCPServer implements Runnable{ 
       ^
1 error
stdout
Standard output is empty