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
Developer
Posts: 67
Registered: ‎01-13-2009
My Device: Not Specified

Opening Multiple Ports

Hi all.

 

Once again I am making some adjustments to the socketDemo.  This time I have created a class which runs the program, and the file that was once SimpleSocketServer is now a thread that receives the port number, which then creates the socket.

 

I try and run the program attempting to create two sockets using the following code:

 

Thread recordPort = new SimpleSocketServer(4444); // receive strings of specific information from a file

recordPort.start();

 

Thread filePort = new SimpleSocketServer(2222); // receive an entire file

filePort.start();

 

I get the exception  java.net.BindException:  Address already in use:  JVM_Bind- port:2222. 

The first socket works just fine. 

 

I have tried many different combinations of port numbers in a variety of orders and I have only gotten it to work once, after shutting down eclipse and restarting.  However I haven't been able to reproduce that so I am sure it was a fluke. 

 

Since I am creating a new instance of the Socket in a thread that is essentially still running is it possible that I don't have a totally new instance and I am attempting to overwrite the socket currently in use even though the PORT number has changed?  Any recommendations for setting up two sockets?

BlackBerry Development Advisor
Posts: 15,700
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Opening Multiple Ports

I don't see any issues with the code you have posted.  Can you post a larger sample?  Are there any other applications running in the BlackBerry Simulator that may also be using the same port (other samples, etc...)?  You can reset the BlackBerry Simulator to its default state and remove all third party applications by running the clean.bat file located in the simulator directory:  Eclipse\plugins\net.rim.eide.componentpack#.#.#_#.#.#.##\components\simulator

 

What BlackBerry Smartphone model and BlackBerry handheld software version are you testing on?  You can find this under Options, About on the BlackBerry Smartphone.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 67
Registered: ‎01-13-2009
My Device: Not Specified

Re: Opening Multiple Ports

Hi Mark,

Thanks for the reply.  Sorry there was such a large delay in providing a larger code sample. 

 

There are no other applications running.  I get this error when starting up the server application on the computer. The simulator isn't running.   Reseting the simulator with the clean.bat had no effect.  It doesn't matter what order the port numbers are, its always the second port that fails. 

 

Here is the complete code for the main method:

package ServerSetup; import java.awt.AWTException; import java.awt.SystemTray; public class LaunchServer { public static void main(String[] args) throws InterruptedException, AWTException { if(SystemTray.isSupported()) { SetupSystemTray st = new SetupSystemTray(); //create new system tray instance } Thread recordPort = new SimpleSocketServer(4444); //new SimpleSocketServer().run(); recordPort.start(); //Thread picturePort = new SimpleSocketServer(4440); //picturePort.start(); } }

 

Here is the Thread that creates the first port and fails during the creation of the second:

 

package ServerSetup; import java.net.*; import java.io.*; import java.util.*; public class SimpleSocketServer extends Thread implements Runnable { //constants --------------------------------------------------------------- private static int PORT;// = 4444; // if this class was looped then I could use a get [i] option //statics ----------------------------------------------------------------- private static final String RESOURCE_PATH = "resources"; private static ResourceBundle _resources = java.util.ResourceBundle.getBundle(RESOURCE_PATH); SimpleSocketServer(int port) { PORT = port; yield(); } public void run() { createSocket(); }// end run public static void createSocket() { ServerSocket serverSocket = null; Socket clientSocket = null; try { serverSocket = new ServerSocket(PORT); System.out.println(_resources.getString("SimpleSocketServer.Started")); System.out.println(_resources.getString("SimpleSocketServer.WaitingForClient")); clientSocket = serverSocket.accept(); //blocking call - waits for a connection System.out.println(_resources.getString("SimpleSocketServer.ClientConnecting") + ":" + clientSocket.getInetAddress().getHostAddress()); (new WorkerDB(clientSocket)).start();//spin off a new thread to handle this socket - this way new socket connections can be served immediately } catch (IOException e) { System.err.println("Error starting socket connection: "+e + "- port:" + PORT); } } public static int getPort() { return PORT; } }

 

This code together always provides this output: 

Server StartedListening for clients...

Error starting socket connection: java.net.BindException: Address already in use: JVM_Bind- port:4440 

 

Further debug has told me that the Server Started and Listening for clients.... lines are corresponding to the first port 4444.  I don't understand why the SimpleServer is thinking that the port is in use.  For the record, I have tried a lot of other ports not just 4440.   

 

Thanks again for all your help.

 

Developer
Posts: 67
Registered: ‎01-13-2009
My Device: Not Specified

Re: Opening Multiple Ports

Has anyone else been able to run this segment of code without the error I described?
Highlighted
BlackBerry Development Advisor
Posts: 15,700
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Opening Multiple Ports

[ Edited ]

Try running the following from a command line:

 

netstat -a -b

 

This will list all of the ports in use on your PC and show the executable file that has it open.

Message Edited by MSohm on 04-20-2009 04:33 PM
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker