[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

2 socket connections to the same host...

Posted on 2005-04-08
12
Medium Priority
?
352 Views
Last Modified: 2008-02-01
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;
0
Comment
Question by:InteractiveMind
  • 5
  • 5
  • 2
12 Comments
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13734916
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?
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 13734956
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
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13735125
> 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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Author Comment

by:InteractiveMind
ID: 13735135
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...
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 13735196
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
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 13735201
Typo error ... it should be ..

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

-CE
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13735257
But, I need both ports to be the same...
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 13735312
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
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13735359
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();

??
0
 
LVL 6

Accepted Solution

by:
guitaristx earned 2000 total points
ID: 13737596
If you're only wanting the server to accept connections through one port, you only need one ServerSocket object.  However, you need to spawn a new thread each time the ServerSocket's accept() method returns.

<pseudocode alert!>

ServerSocket svr = new ServerSocket( port );
Socket s = srv1.accept();
while( some_condition )
{
     MySocketThread t = new MySocketThread(s);
     t.start();
     s = srv1.accept();
}

<and elsewhere in the server>
public class MySocketThread extends Thread
{
     private Socket s;

     public MySocketThread(Socket sock)
     {
          s = sock;
     }

     public void run()
     {
          //do your server-side stuff here
     }
}
0
 
LVL 6

Expert Comment

by:guitaristx
ID: 13737623
The above code makes certain that there is a minimum amount of time taken when the ServerSocket is not accepting connections.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question