02-04-2013 08:03 AM - edited 02-04-2013 08:16 AM
Hello guys,
This my first post, I hope you guys will solve my problem. I am creating a client-server application where data will exchange on Wifi. Every thing is working fine. But as I open and close the single connection( as my application demand this), after working continuously for long hours the IOException occur.
con = Connector.open(url,Connector.READ_WRITE); // Error
connection = (StreamConnectionNotifier)con;
sc = (StreamConnection) connection.acceptAndOpen();
I open this connection in different thread not in main.
Please help me.....
Solved! Go to Solution.
02-04-2013 08:53 AM
I would expect there to be the occasional glitch in WiFi service, is that all that is happening? Can you restart the processing once WiFi is back?
What is the URL you are using for the open?
02-04-2013 11:42 AM
Check your URL, and wifi connections.
If still the problem persists
drop the "stacktrace" for the IOException. It might help in debugging your problem.
suggestion: use try, catch blocks
02-04-2013 11:51 PM
I am using this URL
String url = "socket://:44444;deviceside=true;interface=wifi";
I am using the try and catch blocks...
02-04-2013 11:59 PM
02-05-2013 03:26 AM
I agree that the switching is likely to be causing your problem. I would you use your try/catch to catch the problem and then loop to restart the processing. I suggest you put a sleep in the loop otherwise you will kill the battery. You might also like to check WiFi status and not do the open when there is no WiFi.
02-05-2013 04:36 AM
Ok..Peter. I will apply the same and will get back to you.
02-07-2013 12:44 AM
Hello,
I am still getting the same exception...Actually on debugging I found the IOException occuring in following code then on restarting the app again it won't open Connector.open(). And the weired thing is, it doesn't happens all time. So please could you eloborate the optimal way to close the connection and all the stuffs.Right now, I am doing
try{
connection = (StreamConnectionNotifier)con;
sc = (StreamConnection) connection.acceptAndOpen();
}
catch(Exception e) // Unable to connect
{
System.out.println("Unable to connect222..................."+e.getMessage()); //
e.printStackTrace();
isConnected=false;
invokeAndWait( new Runnable()
{
/**
* @see java.lang.Runnable#run()
*/
public void run()
{
try {
if(SmsSenderReceiver.messconn!=null){
SmsSenderReceiver.messconn.close();}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(sc != null)
{
try{
sc.close();
} catch(IOException ioe)
{
ioe.printStackTrace();
}
}
if ( connection != null)
{
try
{
connection.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
System.exit(0);
}
});
}
02-07-2013 05:01 AM
The first thing to note is that this processing should be on a separate Thread. It looks like this code is in the class you have that extends UiApplication, so this might not be the case.
The second thing is that you shuld not close all the streams using an invokeLater. This Thread should be running in the background and so you should be able to just close the streams.
Finaly you will not get a stack trace in an Exceptin like this. So you can remove the e.printstacktrace().
Now here is pseudo code to describe how I would implement this:
boolean finished = false;
while ( !finished) {
try {
conn....
// real processing in here
} catch Exception e) {
System.out.prin....
if ( SMmsSender... ) {
....close();
}
// close all streams
Thread.sleep(5000);
// rest the connection for 5 seconds so thta it can get over the problem.
} // end of catch
} // end of while loop.
Hope this helps.
02-23-2013 05:01 AM
Thanks man...!! you are genious. You have solved my problem. Sorry for the late reply, hope you won't mind..!!![]()