What is the default timeout value for java.net.Socket in android?

I am developing a mobile application for Android. There I create a socket and transfer data between Android mobile phones and windows (which run on a PC, laptop). I am using Android 2.3

Samsung Mobile Galaxy Testing
PCs and mobile phones are connected via USB titration.
Correct data transfer.
But one of the problems is in the middle of the application, which is canceled by disconnecting the mobile USB drive from the system, and then I throw an exception.

But sometimes this does not happen to any exception, it just waits, Socket is also not closed waiting for data to be read
So please send default socket timeout in android


My code examples are below

Socket socket=null; DataOutputStream dos=null; OutputStream os=null; InputStream is=null; DataInputStream dis=null; try { Log.i(tagName, "b4 creating socket"); socket=new Socket(this.getIpAddress(),this.getPort_number()); is=socket.getInputStream(); dos=new DataOutputStream(os); dis=new DataInputStream(is); } catch(UnknownHostException unknown_ex) { Log.i(tagName, "Exception host unknown : "+unknown_ex.toString()); unknown_ex.printStackTrace(); } catch(IOException ioe_ex) { Log.i(tagName, "ioe Exception : "+ioe_ex.toString()); ioe_ex.printStackTrace(); } catch(Exception ex) { Log.i(tagName, "Exception : "+ex.toString()); ex.printStackTrace(); } if(dos!=null) { try { dos.close(); } catch(Exception ex) { } } if(dis!=null) { try { dis.close(); } catch(Exception ex){} } if(socket!=null) { try { socket.close(); } catch(Exception ex) { } } socket=null;dos=null;dis=null; Log.i(tagName, "MySocketConnection.connectMe() - end"); 


When i use code

 Socket.getSoTimeout() 


Then it returns 0.


Please give everyone your ideas.

+8
java android android-networking sockets socket-timeout-exception
source share
2 answers

The socket read timeout is infinite by default, as it says in Javadoc : "The zero timeout is interpreted as an infinite timeout." If you want to get the final value, call Socket.setSoTimeout().

+5
source share

I do not know how to use the default timeout this.socket.setSoTimeout(5000); but you can try setting a higher timeout value. Here 5000 milliseconds means 5 seconds. Hope this fixes your issue.

0
source share

All Articles