首页 > 代码库 > 自己动手写简单的web应用服务器(3)—服务器从磁盘文件中读取文本,发送给客户端

自己动手写简单的web应用服务器(3)—服务器从磁盘文件中读取文本,发送给客户端

服务器:

 1 package serverAndClient; 2  3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.io.OutputStream;10 import java.io.OutputStreamWriter;11 import java.io.PrintWriter;12 import java.net.ServerSocket;13 import java.net.Socket;14 15 /**16  * 任务目的:当有客户端进入的时候,服务器就会从磁盘文件读取一段文本,17  *              将该文本信息发送给客户端。18  * 准备工作:预先在D盘建立一个txt文件。文件路径:D:\1.txt19  */20 public class Server {21     22     private ServerSocket ss;23     /**端口号*/24     private int port = 8081;25     /**利用构造方法,初始化ServerSocket*/26     public Server() {27         try {28             ss = new ServerSocket(port);29         } catch (IOException e) {30             e.printStackTrace();31         }32     }33     /**服务器启动,等待客户端进去,并产生Socket对象,将该对象交给线程去执行*/34     public void start(){35         while(true){36             try {37                 Socket s = ss.accept();//等待客户端进入38                 Thread thread = new Thread(new Handler(s));39                 thread.run();40             } catch (IOException e) {41                 e.printStackTrace();42             }43         }44     }45     46     private class Handler implements Runnable {47         private Socket s;48         public Handler(Socket s) {49             this.s = s;50         }51 52         @Override53         public void run() {54             try {55                 File file = new File("D:/1.txt");56                 InputStream is = new FileInputStream(file);57                 BufferedReader reader = new BufferedReader(58                         new InputStreamReader(is, "GBK"));59                 OutputStream os = s.getOutputStream();60                 PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));61                 String str =null;62                 while(true){63                     str = reader.readLine();//循环读取文本64                     if(str==null) break;//读取到文件尾部,跳出循环65                     writer.println(str);66                     writer.flush();67                 }68                 is.close();69                 s.close();70             } catch (Exception e) {71                 e.printStackTrace();72             }73         }74 75     }76     public static void main(String[] args) {77         Server s = new Server();78         s.start();79     }80 81 }

客户端:

 1 package serverAndClient; 2  3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.io.OutputStream; 8 import java.io.OutputStreamWriter; 9 import java.net.Socket;10 import java.net.UnknownHostException;11 12 import org.xml.sax.InputSource;13 /**客户端程序,接受从服务器发来的消息*/14 public class Client {15     private Socket s;16     17     /**客户端程序启动*/18     public void start(){19         try {20             s = new Socket("192.168.1.103", 8081);21             InputStream is = s.getInputStream();22             BufferedReader reader = new BufferedReader(new InputStreamReader(is,"GBK"));23             String str = null;24             while(true){25                 str = reader.readLine();26                 if(str==null){27                     break;28                 }29                 System.out.println(str);30             }31             s.close();32         } catch (UnknownHostException e) {33             e.printStackTrace();34         } catch (IOException e) {35             e.printStackTrace();36         }37     }38     public static void main(String[] args) {39         Client client = new Client();40         client.start();41     }42 }

 

自己动手写简单的web应用服务器(3)—服务器从磁盘文件中读取文本,发送给客户端