Hello there,
How can I tell if my TCP/IP connection is CLOSED prior to Connection?
I am connecting to a fixed IP and Port using: SocketConnection, the first two sessions are fine, but the following sessions result in:
"Fatal: IO Error:SymbainOS error = -34 : General: System error"
I fell that the IP and port are not compleately closed following the previous connection. I have placed my disconnect routine into a thread, and so that I can wait for the disconnection to end before I attempt another connection.
If I attempt a re-connection after 1-2 minutes it appears to connect successfully.
Here is the code I am using to make a connection and disconnection:
I would be happy if you spot other issues too.Code:public class Connection extends ConnectionStatus implements Runnable { public static Connection instance = null; public byte ConnectionStatus = DISCONNECTED; /** String constant for the socket address */ private static final String SOCKET_ADDRESS = "socket://"; /** instance of SocketConnection */ private SocketConnection socketConnection = null; private InputStream InStream = null; private OutputStream OutStream = null; private ConnectionDetails connectionDetails; // Connect Discaonnect flags: private final byte CD_UNKNOWN = 0; private final byte CD_CONNECT = 1; private final byte CD_DISCONNECT = 2; private byte ConnectDisconnectFlag = CD_UNKNOWN; public static Connection getInstance(ConnectionDetails CD, Form form, Display disp) { if(instance == null) { instance = new Connection(CD, form, disp); } // else an instance is already defined. Return this instance: return instance; } public Connection(ConnectionDetails CD, Form form, Display disp) { // Init debug log: if(debug) { // Init debug log: InitAppendLog(form, disp); } if ( CD.ValidAddress() ) { connectionDetails = CD; // Connection details. (IP, port etc.) ConnectDisconnectFlag = CD_CONNECT; // Use Connect routine. Thread t = new Thread(this); t.start(); // Wait until thread ends: try { t.join(); } catch (InterruptedException ie) { // Thread has ended. } } else { LogEvent("incorrect url, possibly network type not supported"); } } public boolean ReConnect(ConnectionDetails CD) { // Use this function after a connected has been disconnected. connectionDetails = CD; // Connection details. (IP, port etc.) ConnectDisconnectFlag = CD_CONNECT; // Use Connect routine. Thread t = new Thread(this); t.start(); // Wait until thread ends: try { t.join(); } catch (InterruptedException ie) { // Thread has ended. } return (ConnectionStatus == CONNECTED); } public boolean CloseConnection() { ConnectDisconnectFlag = CD_DISCONNECT; // Use Disconnect routine. Thread t = new Thread(this); t.start(); // Wait until thread ends: try { t.join(); } catch (InterruptedException ie) { // Thread has ended. } LogEvent("CloseConnection", ConnectionStatus); return (ConnectionStatus == DISCONNECTED); } public void run() { // Attempt Connection: if ( ConnectDisconnectFlag == CD_CONNECT) { if ( (socketConnection == null) || (ConnectionStatus==DISCONNECTED) ) { if ( connectionDetails.ValidAddress() ) { try { String url = SOCKET_ADDRESS + connectionDetails.IpAddr + ":" + connectionDetails.IpPort; LogEvent( url ); ConnectionStatus = CONNECTING; socketConnection = (SocketConnection)Connector.open(url); socketConnection.setSocketOption(socketConnection.RCVBUF, 1000); socketConnection.setSocketOption(socketConnection.SNDBUF, 1000); socketConnection.setSocketOption(socketConnection.KEEPALIVE, 10000); } catch (IllegalArgumentException iae) { //iae.printStackTrace(); LogEvent("Thread Connection (1)>"); LogEvent("incorrect url, check and try again"); ConnectionStatus = INCONNECT_URL; } catch (ConnectionNotFoundException cnfe) { //cnfe.printStackTrace(); LogEvent("Thread Connection (2)>"); LogEvent("no such address registered, check and try again"); ConnectionStatus = ADDRESS_NOT_FOUND; } catch (IOException io) { io.printStackTrace(); ConnectionStatus = IO_ERROR; LogEvent("Thread Connection (3)>"); LogEvent(ConnectionStatus, io.getMessage()); } catch (SecurityException se) { se.printStackTrace(); ConnectionStatus = CANNOT_ACCESS_PROTOCOL_HANDLER; LogEvent("Thread Connection (4)>"); LogEvent(ConnectionStatus, se.getMessage()); } catch (Exception e) { e.printStackTrace(); ConnectionStatus = NO_CONNECT; LogEvent("Thread Connection (5)>"); LogEvent(ConnectionStatus, e.getMessage()); } finally { if (socketConnection != null) { try { } catch (Exception e) { LogEvent("Thread Connection (6)>"); LogEvent(ConnectionStatus, e.getMessage()); } } } if( (socketConnection!=null) && (ConnectionStatus==CONNECTING) ) { // Now attempt to open data stream: try { // Open Input and output streams: InStream = socketConnection.openInputStream(); OutStream = socketConnection.openOutputStream(); ConnectionStatus = CONNECTED; LogEvent(ConnectionStatus); } catch (Exception e) { // Catch exception: ConnectionStatus = IO_ERROR; LogEvent("Thread Connection (7)>"); LogEvent(ConnectionStatus, e.getMessage()); } } else { if(socketConnection != null) { LogEvent("socketConnection = NULL"); } LogEvent("ConnectionStatus:"+CONNECTING); } } else { LogEvent("incorrect url, possibly network type not supported"); } } else { LogEvent("socketConnection NOT NULL:" + ConnectionStatus + "\n"); } } else if ( ConnectDisconnectFlag == CD_DISCONNECT) { if( socketConnection != null ) { try { socketConnection.close(); ConnectionStatus = DISCONNECTED; } catch (IOException e) { ConnectionStatus = FAIL_TO_DISCONNECT; LogEvent("Thread Connection (7)>"); LogEvent(ConnectionStatus, e.getMessage()); } // LogEvent("CloseConnection", ConnectionStatus); //instance.destroyApp(true); destroyApp(true); } } // else should not reach here } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
(Sorry about my coding style it does not comply to java programming style, )

Reply With Quote




