Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 7
Registered: ‎05-07-2010
My Device: none
My Carrier: none

IOException: Port in use

Hi All,

I've following problem:

I'm trying to develop server application on the device side, which will be listening for the incoming connection on TCP. I'm creating ServerSocketConnection object, listening for the connection, after connection request, I'm using accepted socket, and closing listening socket, because this socket don't needed for me anymore. But if session is closed, I'm trying to open listening socket again, and it cause IOException: Port in use. I've written small piece of code, to test this issue, and it constantly throws an exception:

 

try{
ServerSocketConnection connection = (ServerSocketConnection)Connector.open(socket://:5555;interface=wifi, Connector.READ_WRITE, false);

StreamConnection acceptedConnection = (StreamConnection)connection.acceptAndOpen();

if (null != acceptedConnection) {				   acceptedConnection.close();				connection.close();
}

//Just for test, trying to open it again
connection = (ServerSocketConnection)Connector.open(socket://:5555;interface=wifi, Connector.READ_WRITE, false);
}
catch(IOException e) {
    //ERROR
}

 

 Does it mean that ServerSocketConnection.close is not synchronous? Or I'm doing something wrong?

I understand that one of the solutions it to open listening socket once, and keep it opened, but I'm interested in root cause of problem.

 

If somebody know the answer, or had the similar issues - I will be very thankful for any help.

 

Environment:

JRE 6.0.0

Device - Torch 9800

6.0 Bundle 695 (v. 6.0.0.246. Platform 6.4.0.105)

 

Highlighted
New Contributor
Posts: 9
Registered: ‎03-30-2011
My Device: Bold 9700
My Carrier: Rogers

Re: IOException: Port in use

[ Edited ]

I receive the same issue with socket and udp connections.

And my process for closing is close inputstream, close outputstream, close connection:

 

			// Clear out this connection
			if (SocketInput != null)
			{
				try
				{
					SocketInput.close();
				}
				catch (Throwable t)
				{
					// Do nothing
				}
			}
			if (SocketOutput != null)
			{
				try
				{
					SocketOutput.close();
				}
				catch (Throwable t)
				{
					// Do nothing
				}
			}
			if (Connection != null)
			{
				try
				{
					Connection.close();
				}
				catch (Throwable t)
				{
					// Do Nothing
				}
			}
			Connection = null;