android - Java SSL connection returns previous reply -
i'm using ssl connection connect server (which have no command on , no access it's code, it's fault, wanna sure), when send info (a byte array) first time right response, in subsequent sends response expected previous send. example, let's if send x, expect server reply x, y y, z z, etc...
when app starts, phone call x, , x. phone call y , x, phone call z , y, phone call x , z, etc... here's generic code implemented each command send , receive (bytes initiated predetermined set of bytes simulate, say, command x)
byte[] bytes = new byte[6]; if(socket == null || !socket.isconnected() || socket.isclosed()) seek { getsocket(localip); } grab (ioexception e1) { e1.printstacktrace(); } if (socket == null || !socket.isconnected()) { seek { getsocket(globalip); } grab (ioexception e1) { e1.printstacktrace(); homecoming null; } } byte[] recievedbytes = null; string sentbstring = "sendgetconfig: "; (int = 0; < bytes.length; i++) { sentbstring += string.valueof(bytes[i]) + ", "; } system.out.println(sentbstring); if (socket != null){ seek { dataoutputstream os = new dataoutputstream(socket.getoutputstream()); os.write(bytes); datainputstream = new datainputstream(new bufferedinputstream(socket.getinputstream())); int tries = 0; while (tries < 20 && (recievedbytes == null || recievedbytes.length == 0)) { if (is.marksupported()) { is.mark(2048); } bytearrayoutputstream buffer = new bytearrayoutputstream(); int nread; byte[] info = new byte[1024]; seek { nread = is.read(data, 0, data.length); buffer.write(data, 0, nread); } grab (exception e) { } buffer.flush(); recievedbytes = buffer.tobytearray(); if (recievedbytes.length == 0) is.reset(); } is.close(); os.close(); socket.close(); } } i know implementation of read not perfect, result of workaround did because server not send end of stream indication , read command implemented in loop results in timeout exception
any help appreciated
the server not send end of stream indication
of course of study server sends eos indication. problem you're ignoring it. when peer has closed connection , there no more pending info read, read() returns -1.
and read command implemented in loop results in timeout exception
nonsense.
the right form of loop follows:
while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } substituting own variable names required.
the reason maintain reading same info because maintain resetting stream same point. throw mark() , reset() calls away.
java android datainputstream dataoutputstream
No comments:
Post a Comment