Data blocking caused by Readline features

Source: Internet
Author: User

From a forum post: http://topic.csdn.net/u/20081231/06/6a1e156c-3015-4e93-9bcc-19ee2b32b13b.html

I modified the Code to facilitate testing.
1 server. Java Server

  1. Import java. Io. bufferedreader;
  2. Import java. Io. ioexception;
  3. Import java. Io. inputstreamreader;
  4. Import java. Io. printwriter;
  5. Import java.net. serversocket;
  6. Import java.net. Socket;
  7. Public class server {
  8. /**
  9. * @ Param ARGs
  10. * The command line arguments
  11. */
  12. Public static void main (string [] ARGs) throws ioexception {
  13. String data = "toobie ornaught toobie ";
  14. Try {
  15. Serversocket srvr = new serversocket (8085 );
  16. Socket SKT = srvr. Accept ();
  17. System. Out. Print ("server has connected! /N ");
  18. Bufferedreader in = new bufferedreader (New inputstreamreader (SKT
  19. . Getinputstream ()));
  20. Printwriter out = new printwriter (SKT. getoutputstream (), true );
  21. System. Out. Print ("sending string: '" + Data + "'/N ");
  22. Out. print (data ); // ############ the problem appears here ##################
  23. System. Out. Print ("sending string: '" + Data + "'/N ");
  24. While (! In. Ready ()){
  25. }
  26. System. Out. println (in. Readline ());
  27. Out. Close ();
  28. In. Close ();
  29. SKT. Close ();
  30. Srvr. Close ();
  31. } Catch (exception e ){
  32. System. Out. Print ("whoops! It didn't work! /N ");
  33. }
  34. }
  35. }

2 client. Java client

  1. Import java. Io. bufferedreader;
  2. Import java. Io. ioexception;
  3. Import java. Io. inputstreamreader;
  4. Import java. Io. printwriter;
  5. Import java.net. Socket;
  6. Public class client {
  7. /**
  8. * @ Param ARGs
  9. * The command line arguments
  10. */
  11. Public static void main (string [] ARGs) throws ioexception {
  12. Try {
  13. Socket SKT = new socket ("localhost", 8085 );
  14. Bufferedreader in = new bufferedreader (New inputstreamreader (SKT
  15. . Getinputstream ()));
  16. Printwriter out = new printwriter (SKT. getoutputstream (), true );
  17. String inputline, outputline;
  18. While (! In. Ready ()){
  19. }
  20. System. Out. println (in. Readline ());
  21. Out. Print ("AAA ");
  22. Out. Close ();
  23. In. Close ();
  24. SKT. Close ();
  25. } Catch (exception e ){
  26. System. Out. Print ("whoops! It didn't work! /N ");
  27. }
  28. }
  29. }

After the system is running, the system stops responding and is waiting. After checking, the out. Print is the root of the problem.
Change
Out. println (data );

You can:

The reason is that Readline is used, and this is only two possibilities that will continue.
1 encountered a line break
2. The end of the data

And you only sent
Out. Print (data );

The opposite side does not know that you have sent the data, so keep waiting for your line feed ....

Of course, this problem will not occur if you use read to read bytes instead of Readline on the client. This is also the special feature of Readline.

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.