No data is read in Android using Bufferedreader.readline blocking, but ready returns true

Source: Internet
Author: User

By sending a message on the computer via the Socket Test tool, the Android real machine can receive a response of Bufferedreader.ready () to return true, but ReadLine has been blocked.

Cause: ReadLine () only ends when a newline character is encountered, because a newline character is added when the message is sent.

Test tool: Http://files.cnblogs.com/files/feijian/SocketTool.rar

Attach my socket client code:

 Public classQpushclientImplementsRunnable {protected Staticqpushclient minstance; protectedHandler Mhandler; protectedinetsocketaddress maddress; protectedString TAG = "Qpushclient"; Private Final intTime_out = 5 * 1000; //Patrol Cycle    Private Final intCheck_period = 2 * 1000; //connection attempt interval time    Private Final intConnect_period = 30 * 1000; Private Final intHeartbeart_period = 10 * 1000; //If the connection fails or the response fails, the number of attempts is 9, and if it is still invalid, try again    Private Final intConnect_try_times = 9; //number of connection attempts    Private intMconnectcount;    Socket Mclientsocket;    String Mhost; intmport; //set whether to read data    BooleanIsstartrecievemsg =false; //Turn on heartbeat detection    BooleanIskeepheartbeat =false; Privateqpushclient (Handler Handler) {Mhandler=handler; }     Public Staticqpushclient getinstance (Handler Handler) {if(Minstance = =NULL) {minstance=Newqpushclient (handler); }        returnminstance; }     Public voidInit (String host,intPort) {Mhost=host; Mport=Port; NewThread ( This). Start (); Isstartrecievemsg=true; Iskeepheartbeat=true; } @Override Public voidrun () {maddress=Newinetsocketaddress (Mhost, mport); if(Mclientsocket = =NULL) {Mclientsocket=NewSocket (); }        //attempt to connect, if not connected, set the number of attempts         while(!mclientsocket.isconnected () && Mconnectcount <connect_try_times)            {Connect (); if(!mclientsocket.isconnected ()) {Mconnectcount++;            Sleep (Connect_period); } Else{Mconnectcount= 0;//connection, then restore 0            }        }        if(mclientsocket.isconnected ()) {//Start LandingSendmsg ("Login");            Recvmsg ();        Keepheartbeat (); }    }    Private voidConnect () {Try{mclientsocket.connect (maddress); } Catch(IOException e) {e.printstacktrace (); LOG.E (TAG,"Mclientsocket.connect fail" +e.getmessage ()); }    }    /*** Heartbeat Maintenance*/    Private voidkeepheartbeat () {//set the heartbeat rate and start the heartbeat         while(iskeepheartbeat) {sendmsg ("I'm a heartbeat pack.");        Sleep (Heartbeart_period);    }} BufferedWriter Mwriter;    BufferedReader Mreader; /*** Constantly detect if there is data pushed by the server .*/     Public voidrecvmsg () { while(Mclientsocket! =NULL&& mclientsocket.isconnected () &&!mclientsocket.isclosed ()) {            Try{Mreader=NewBufferedReader (NewInputStreamReader (Mclientsocket.getinputstream (), "Utf-8"));  while(isstartrecievemsg) {log.e (TAG,"RecvMsg5"); if(Mreader.ready ()) {LOG.E (TAG,"RecvMsg6"); /*reads a line of string, reads the content from the client Reader.readline () method is a blocking method, starting from calling this method, the thread will always is blocked until a new message is received and the code goes down*/String Data=Mreader.readline (); LOG.E (TAG,"Isstartrecievemsg data=" +data); //handler sends a message and receives it in the Handlemessage () methodhandlermsg (data); } thread.sleep (200); }            } Catch(Interruptedexception ex) {ex.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Catch(Exception ex) {ex.printstacktrace (); } Sleep (200); }        if(!mclientsocket.isconnected ())            {Connect ();        Recvmsg ();    } sleep (Check_period); }    Private voidSleepLongsleeptime) {        Try{thread.sleep (sleeptime); } Catch(interruptedexception e) {e.printstacktrace (); }    }    /*** Destroy Socket*/     Public voidondestory () {if(Mclientsocket! =NULL) {            Try{mclientsocket.close (); } Catch(IOException e) {e.printstacktrace (); } mclientsocket=NULL; }    }     Public voidsendmsg (String message) {PrintWriter writer; Try{writer=NewPrintWriter (NewOutputStreamWriter (Mclientsocket.getoutputstream ()),true);        WRITER.PRINTLN (message); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }    /** Ready to use . */     Public voidClose () {Try {            if(Mclientsocket! =NULL&&!mclientsocket.isclosed ()) mclientsocket.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }    /*** Processing the message returned by the server *@paramData*/    Private voidhandlermsg (String data) {//protobuf parsing of data//message Type: 1 = logon success, 2 = Heartbeat detection, 3 = Push message        intMsgtype=1; Switch(msgtype) { Case1: sendmsg ("Success");  Break;  Case2: sendmsg ("Success");  Break;  Case3://need to notify serviceSendmsg ("Success");                Mhandler.obtainmessage (Qpushservice.push_type_data, DATA). Sendtotarget ();  Break; }    }}

No data is read in Android using Bufferedreader.readline blocking, but ready returns true

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.