Link to home
Start Free TrialLog in
Avatar of InteractiveMind
InteractiveMindFlag for United Kingdom of Great Britain and Northern Ireland

asked on

2 socket connections to the same host...

Hey guys.

I am trying to establish two Socket connections between to applications, over a network (well, I'm currently testing just on my local machine).

On the Server-application, I'm doing this:

Socket sock1;                 // Class instance
Socket sock2;                 //
...
ServerSocket srv = new ServerSocket( port );
sock1 = srv.accept();

try
{
    ServerSocket srv2 = new ServerSocket( port );
    srv2.setSoTimeout( 10 *1000 );
    sock2 = srv2.accept();
} catch ( InterruptedIOException ioe2 )
{
    error( "Timeout exception occured." );
} catch ( Exception e2 )
{
    e2.printStackTrace();
}


Then on the client-application:

Socket sock1;                // Class Instance
Socket sock2;                //
...
sock1 = new Socket( InetAddress.getByName( server ), port );
sock2 = new Socket( InetAddress.getByName( server ), port );


Obviously, it's all in try{}catch statements appropriately.. Now, when I run it, what appear to be happening, is that the first connection is made without problem, but then a problem occurs on the second connection attempt.. The server is throwing the following exception:

java.net.BindException: Address already in use: JVM_Bind
      at java.net.PlainSocketImpl.socketBind(Native Method)
      at java.net.PlainSocketImpl.bind(Unknown Source)
      at java.net.ServerSocket.bind(Unknown Source)
      at java.net.ServerSocket.<init>(Unknown Source)
      at java.net.ServerSocket.<init>(Unknown Source)
      at ConnHandler.run(ConnHandler.java:48)

Where ConnHandler.java is the Server class, and line 48 is this line:

    ServerSocket srv2 = new ServerSocket( port );

Why is it not making a second connection? What would you say is the best way to achieve this?

Regards;
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

do I need to do:

srv.close();

before creating the second ServerSocket? I haven't bothered trying it; because I'm guessing that it would close sock1, right?
Avatar of CodingExperts
CodingExperts

sock1 = new Socket( InetAddress.getByName( server ), port );
sock2 = new Socket( InetAddress.getByName( server ), port );

would mean you are connecting to the same port twice.

Are you changing the port while connecting

CE
> would mean you are connecting to the same port twice
Yeah; I need two connections between the two applications, both on the same port.

> Are you changing the port while connecting
Nope.
By the way: I need to create both ServerSockets on the Server Application; so I can't just create a server sock on the Server app, and another Server sock on the client app...
This is what you should do ...


ServerSocket srv = new ServerSocket( port1 );
ServerSocket srv = new ServerSocket( port 2);

sock1 = new Socket( InetAddress.getByName( server ), port1 );
sock2 = new Socket( InetAddress.getByName( server ), port 2);

-CE
Typo error ... it should be ..

ServerSocket srv1 = new ServerSocket( port1 );
ServerSocket srv2 = new ServerSocket( port 2);

-CE
But, I need both ports to be the same...
When a ServerSocket object is created, it attempts to bind to the port on the local host given by the port argument.

If another server socket is already listening to the port, then a java.net.BindException, a subclass of IOException, is thrown.

No more than one process or thread can listen to a particular port at a time. This includes non-Java processes or threads.

For example, if there's already an HTTP server running on port 80, you won't be able to bind to port 80.

-CE
But, from my first attempt, what it *should* do (at least, what I would expect it to do), is listen on the 'port', then block until it accepts a connection, with this command:

sock1 = srv.accept();

Then, that should stop it from listenning on that port... right? And then, I can create a new ServerSocket on that same port..?

Or, do I need to do something like this:

ServerSocket srv1 = new ServerSocket( port );
sock1 = srv1.accept();
srv1.close();

ServerSocket srv2 = new ServerSocket( port );
sock2 = srv2.accept();
srv2.close();

??
ASKER CERTIFIED SOLUTION
Avatar of guitaristx
guitaristx

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The above code makes certain that there is a minimum amount of time taken when the ServerSocket is not accepting connections.