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