Java Network programming TCP Transport-stream operations-server feedback and client reception

Source: Internet
Author: User

After reading the stream, the server returns some data to the client, telling the client that it has finished writing.

Similar to the "stream operation-write action after getting the source", the client also reads byte and buffered two buffers as an example, and. I hope you will give us a supplement.

1. Using OutputStream to write back to the client, the client uses byte as the buffer to receive

Write back to the client:

1 // get the socket output stream and upload the successful word to the client 2         OutputStream out=s.getoutputstream (); 3         Out.write ("Upload succeeded". GetBytes ());

Client receives:

 1  //  2  inputstream in= 3  byte  [] bufin=new  byte  [1024< Span style= "color: #000000;" >]; 5  int  lenin=in.read (BUF);  7  8  String text=new  String (buf,0,lenin);  9  System.out.println (text); 

2. Using PrintWriter to write back to the client, the client uses buffered as the buffer to receive

Write back to the client:

1 // returns data to the client, using PrintWriter 2         PrintWriter out =new printwriter (S.getoutputstream (),true); 3         4         Out.println ("Upload success");

Client receives

1 // reads the object returned by the service side 2         BufferedReader Bufrin =new bufferedreader (new  InputStreamReader (S.getinputstream ())); 3         String str=bufrin.readline (); 4         System.out.println (str);

Difference:

1.PrintWriter as a convenient conversion between the word stream and the character stream tool, has encapsulated the conversion method, directly use it to write back, no longer use GetBytes () to convert to a byte stream.

2. When the data is received, if it is received in a byte array, the resulting byte stream is written to the array, it must be converted into a string object, with a string (array name, first index, length), and the buffer stream to receive, the need to use inputstreamreader conversion, But it would be nice to assign the value directly to a variable of type string. In general, the second brother method is more convenient.

  

Java Network Programming TCP Transport-stream operations-server feedback and client reception

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.