Write your own simple Web application Server (3)-the server reads the text from the disk file and sends it to the client

Source: Internet
Author: User

Server:

1  Packageserverandclient;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.File;5 ImportJava.io.FileInputStream;6 Importjava.io.IOException;7 ImportJava.io.InputStream;8 ImportJava.io.InputStreamReader;9 ImportJava.io.OutputStream;Ten ImportJava.io.OutputStreamWriter; One ImportJava.io.PrintWriter; A ImportJava.net.ServerSocket; - ImportJava.net.Socket; -  the /** - * Mission Purpose: When a client enters, the server reads a text from the disk file, - * Send this text message to the client.  - * Preparation: Pre-set up a TXT file in D disk. File path: D:\1.txt +  */ -  Public classServer { +      A     PrivateServerSocket SS; at     /**Port number*/ -     Private intPort = 8081; -     /**using the construction method, initialize the ServerSocket*/ -      PublicServer () { -         Try { -SS =NewServerSocket (port); in}Catch(IOException e) { - e.printstacktrace (); to         } +     } -     /**The server starts, waits for the client to go in, generates the socket object, and hands the object to the thread to execute*/ the      Public voidstart () { *          while(true){ $             Try {Panax NotoginsengSocket s = ss.accept ();//wait for the client to enter -Thread thread =NewThread (NewHandler (s)); the Thread.run (); +}Catch(IOException e) { A e.printstacktrace (); the             } +         } -     } $      $     Private classHandlerImplementsRunnable { -         PrivateSocket S; -          PublicHandler (Socket s) { the              This. S =s; -         }Wuyi  the @Override -          Public voidrun () { Wu             Try { -File File =NewFile ("D:/1.txt"); AboutInputStream is =Newfileinputstream (file); $BufferedReader reader =NewBufferedReader ( -                         NewInputStreamReader (IS, "GBK")); -OutputStream OS =S.getoutputstream (); -PrintWriter writer =NewPrintWriter (Newoutputstreamwriter (OS)); AString str =NULL; +                  while(true){ thestr = Reader.readline ();//Looping through text -                     if(str==NULL) Break;//read the end of the file, jump out of the loop $ writer.println (str); the Writer.flush (); the                 } the is.close (); the s.close (); -}Catch(Exception e) { in e.printstacktrace (); the             } the         } About  the     } the      Public Static voidMain (string[] args) { theServer s =NewServer (); + S.start (); -     } the Bayi}

Client:

1  Packageserverandclient;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStream;6 ImportJava.io.InputStreamReader;7 ImportJava.io.OutputStream;8 ImportJava.io.OutputStreamWriter;9 ImportJava.net.Socket;Ten Importjava.net.UnknownHostException; One  A ImportOrg.xml.sax.InputSource; - /**client programs that accept messages from the server*/ -  Public classClient { the     PrivateSocket S; -      -     /**Client program Startup*/ -      Public voidstart () { +         Try { -s =NewSocket ("192.168.1.103", 8081); +InputStream is =S.getinputstream (); ABufferedReader reader =NewBufferedReader (NewInputStreamReader (IS, "GBK")); atString str =NULL; -              while(true){ -str =reader.readline (); -                 if(str==NULL){ -                      Break; -                 } in System.out.println (str); -             } to s.close (); +}Catch(unknownhostexception e) { - e.printstacktrace (); the}Catch(IOException e) { * e.printstacktrace (); $         }Panax Notoginseng     } -      Public Static voidMain (string[] args) { theClient client =NewClient (); + Client.start (); A     } the}

Write your own simple Web application Server (3)-the server reads the text from the disk file and sends it to the client

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.