Uploading text files and small problems with TCP in Java network programming

Source: Internet
Author: User

I just learned the Java network programming in the TCP transmission, to use TCP to transfer files, I also encountered some problems, take the time to tidy up a bit for their own reference later use.

First in this program, I use a client, a server, upload a text file from the client to the server, the server to receive data and display "upload success" to the client.

Client:

1 ImportJava.io.BufferedReader;2 ImportJava.io.FileReader;3 Importjava.io.IOException;4 ImportJava.io.InputStreamReader;5 ImportJava.io.PrintWriter;6 ImportJava.net.Socket;7 8  Public classuploadclient {9 Ten     /** One      * @paramargs A      * @throwsIOException -      */ -      Public Static voidMain (string[] args)throwsIOException { the         //1. Create the client socket object -Socket s=NewSocket ("192.168.1.127", 10000); -          -         //2. Put the file in the buffer and upload +BufferedReader bufr= -                 NewBufferedReader (NewFileReader ("Client.txt")); +          A         //by using PrintWriter to process the byte stream and the character stream, take over the stream atPrintWriter out =NewPrintWriter (S.getoutputstream (),true); -          -         //start reading, upload to service side -String line=NULL; -          while((Line=bufr.readline ())! =NULL){ - out.println (line); in         } -          to         //Here you have to give a sign for the end, or the server will always wait +Out.println ("Over"); -          the         //3. Read the object returned by the server *BufferedReader Bufrin =NewBufferedReader (NewInputStreamReader (S.getinputstream ())); $String str=bufrin.readline ();Panax Notoginseng System.out.println (str); -          the         //4. Close the current + bufr.close (); A s.close (); the     } +  -}

Service side:

ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classUploadserver {/**     * @paramargs *@throwsIOException*/     Public Static voidMain (string[] args)throwsIOException {//1. Set up the socket object on the server sideServerSocket ss=NewServerSocket (10000); //2. Receiving data from the serverSocket s=ss.accept (); //put the accepted data into the streamBufferedReader bufin=NewBufferedReader (NewInputStreamReader (S.getinputstream ())); //specifies the file to be written toBufferedWriter bufw=NewBufferedWriter (NewFileWriter ("Server.txt")); //start reading//The default size of the buffer is 8k, one to 8k will automatically refresh, if there is no end flag, the resulting file size must be a multiple of 8String line=NULL;  while((Line=bufin.readline ())! =NULL){            if("Over". Equals (line)) Break;            Bufw.write (line);            Bufw.newline (); //Write your own refreshBufw.flush (); }                //returns data to the client, using PrintWriterPrintWriter out =NewPrintWriter (S.getoutputstream (),true); Out.println ("Upload succeeded"); //off-StreamBufw.close (); //disconnecting from client off serverS.close ();    Ss.close (); }}

Uploading text files and small problems with TCP in Java network programming

Related Article

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.