Read blocking issues in Java Network programming

Source: Internet
Author: User

The writable and readable state of the socket. When the output buffer is not full, the socket is writable (note that it is not possible to write locally after the recipient has enabled the receive operation, which is an erroneous understanding), so when the socket is established, it is in a writable state. For readable, it refers to the data that is received in the buffer, and the data is not processed. When the socket is created, it is not in a readable state, and the socket can be in a readable state only if the other side of the connection writes data to the channel of the socket (note that if the other socket is closed, the local socket will be in a readable state and, after each call to read, the return is 1).

    • Server
Importjava.net.*;Importjava.io.*; Public  classServer{       Public Static void Main(string[] args)throwsException {ServerSocket Server =NewServerSocket (8888); System.out.println ("Wait for connection ...");        Socket socket = server.accept (); Bufferedinputstream is =NewBufferedinputstream (Socket.getinputstream ());byte[] Buff =New byte[1024x768];intSize//When the client's output stream does not have close, the server does not receive-1, and will remain asleep in the while,         while(size = Is.read (buff))! =-1) {String str =NewString (Buff,0, size); System.out.println (str);/****************************************************/            if(Str.indexof (' \ n ') >0){//Presence message End flag                 Break; }/*****************************************************/}//is.close ();System.out.println ("Received"); System.out.println ("Sending ..."); Bufferedoutputstream out =NewBufferedoutputstream (Socket.getoutputstream ()); Out.write ("Hello, I am the server". GetBytes ());        Out.flush ();        Out.close ();        Is.close ();        Socket.close (); System.out.println ("Send over!"); }}
    • Client
Importjava.net.*;Importjava.io.*; Public  classClient{       Public Static void Main(string[] args)throwsException {Socket socket =NewSocket ("localhost",8888); Bufferedoutputstream out =NewBufferedoutputstream (Socket.getoutputstream ());/******************** means send end *****************************/Out.write ("Hello, I'm the client \ n". GetBytes ()); Out.flush ();//out.close (); With this phrase, the server can read a 1System.out.println ("Send over!"); System.out.println ("Receiving ..."); Bufferedinputstream is =NewBufferedinputstream (Socket.getinputstream ());byte[] Buff =New byte[1024x768];intSize while(size = Is.read (buff))! =-1) {System.out.println (NewString (Buff,0, size));        } is.close ();        Out.close ();        Socket.close (); System.out.println ("received!"); }}

1. Server Listening
2, the client sends the connection request, then sends the data (does not close the connection);
3, the server received data, but not read-1, fell into sleep (client tail closed connection, the server could not read-1)
4, the client can not receive data, also sleep in read inside
5, because both of the sleep, into the cycle of death

Workaround:
1, when the client sends the data, add a tail flag, the server side detection of this flag
2. Using Dataxxxstream to encapsulate the data stream

Read blocking issues in Java Network programming

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.