android - Proper way of closing Streams in Java Sockets -


i saw posts still can't find answer.

this how server interacts client:

public void run () {     try {         //read client request         inputstream = server.getinputstream();         byte[] buff = new byte[1024];         int i;         bytearrayoutputstream bos = new bytearrayoutputstream();         while ((i = is.read(buff, 0, buff.length)) != -1) {             bos.write(buff, 0, i);             system.out.println(i + " bytes readed ("+bos.size()+")");         }         is.close();         = null;         //do client request          //write response         outputstream os = server.getoutputstream();         os.write("server response".getbytes());         os.flush();         os.close();         os = null;      } catch (ioexception ioe) {         ioe.printstacktrace();     } } 

and client side:

public void run() {         try {             inetaddress serveraddr = null;             serveraddr = inetaddress.getbyname("10.0.2.2");             socket = new socket(serveraddr, 5000);              //send request server             outputstream os = socket.getoutputstream();             os.write(jsonrequest.tostring().getbytes("utf-8"));             os.flush();             os.close();             os = null;              //read server response             inputstream = socket.getinputstream();             byte[] buff = new byte[1024];             int i;             bytearrayoutputstream bos = new bytearrayoutputstream();             while ((i = is.read(buff, 0, buff.length)) != -1) {                 bos.write(buff, 0, i);                 system.out.println(i + " bytes readed ("+bos.size()+")");             }             is.close();             = null;              //do server response          } catch (unknownhostexception uhe) {             sendcallbackerror(uhe);         } catch (ioexception ioe) {             sendcallbackerror(ioe);         }     } 

as can see, client connects , send request. server read request writes response client read.

the problem code outputstream.close() in client , inputstream.close() in server. stated in javadocs, closing stream close socket. result when client tries read server response, socket closed.

i've managed overcome calling socket.shutdowninput , socket.shutdownoutput instead. still thinking whether proper way of doing it

as note, closing streams close() when server writes response or when client reads doesn't create problems (i guess closing synchronized between client , server).

so questions are:

  1. is using socket shutdown methods proper way?
  2. can keep closing last streams close() (when sending , reading response server)
  3. could happen closing shutdown keep data in buffer , wouldn't sent?

you can following:

try{ }catch(){ }finally{ if(is!=null){ is.close(); } if(os!=null){ os.close(); } } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -