Socket IO Remember flush

Source: Internet
Author: User

  1. Public class Client {
  2. public static void Main (String args[]) throws Exception {
  3. //For the sake of simplicity, all exceptions are thrown directly out
  4. String host = "127.0.0.1"; //server IP address to connect to
  5. int port = 8899; //Listening port for the server to be connected
  6. //Establish a connection with the server
  7. Socket client = new socket (host, port);
  8. //Set up a connection to write data to the server
  9. Writer writer = New OutputStreamWriter (Client.getoutputstream ());
  10. Writer.write ("Hello Server.");
  11. Writer.flush (); Remember to flush after you finish writing .
  12. Writer.close ();
  13. Client.close ();
  14. }
  15. }

For the client to the socket output stream inside the write data to the server to note that if the program is not corresponding to the output stream shutdown, but other blocking operations (such as reading from the input stream), remember to flush, only so that the server can receive the data sent by the client , otherwise it may cause infinite waiting on both sides. I'll talk about it later when I read and write to both the client and the server.

BufferedReader's ReadLine method is to read one line at a time, this method is blocked, until it reads a line of data so that the program will continue to execute, then when will readline read a line? Until the program encounters a newline character or the Terminator ReadLine method of the corresponding stream, it will think that it has read a line before it ends its blocking and the program continues to execute. So when we use the BufferedReader readline to read the data, we must remember that in the corresponding output stream must write a newline character (after the end of the stream is automatically marked as the end, ReadLine can be recognized), After writing a newline character, remember to flush the output stream if it is not closed immediately, so that the data is actually written from the buffer.

Socket IO Remember flush

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.