int SOCKET_TIMEOUT = 10000; //10 sec InetSocketAddress myAddRess = new InetSocketAddress(url_connect,port ); //InetSocketAddress(InetAddress address, int port) try { //1. creating a socket to connect to the server Log.i("connecting to :"+url_connect+"]"," at port ["+port+"]"); //requestSocket.setSoTimeout(SOCKET_TIMEOUT); //set time out for this scoket //if(myDebug.debug_Log)Log.i("SetSocketTimeOut","---WHILE CONNECTING SET TO 10 sec"); requestSocket = new Socket();//(url_connect, port); try { Log.i("Try","To connect in 10 sec"); requestSocket.connect(myAddRess, SOCKET_TIMEOUT); } catch (SocketTimeoutException SE) { Log.i("SocketTimeoutException","-----------"+SE.toString()); requestSocket.close(); requestSocket = null; }
} catch(Exception e) { Log.i("Exception","--is"+e.tostring()); } This will try to connect with in 10 sec if not connected the timeout exception will raise On Tue, Mar 24, 2009 at 12:55 PM, guishenl...@gmail.com < guishenl...@gmail.com> wrote: > > Hi all, > I'm working at an application about network and faced some > problem.I did a client which will send some data to the server.But, if > the server is not open, my client will in trouble. Here is the code > and exact state: > try{ > Socket netSocket = new Socket("10.0.2.2", 9001); / > *Problem is in this line*/ > /* > At this line, when I try to create a socket binding to the local PC, > the program will pause for very long time(almost 5 mins) and then go > to IOExcption handling code where shows "The operation timed > out.".When the server in my local PC is running, everything is OK.I > need the program can response immediately, can anyone help me? > */ > ...... > } > catch (UnknownHostException e) > { > Log.d("SoControl", "UnknownHostException : " + > e.getLocalizedMessage()); > } > catch (IOException e) > { > Log.d("SoControl", "IOException : " + > e.getLocalizedMessage()); > } > > > > -- Dilli Rao. M --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---