How to force JVM Listen on a particular Network Interface card

My PC has 2 network interface cards.
How to force JVM Listen on a particular Network Interface card that is make serversocket
alpjoseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alpjoseAuthor Commented:
sorry, the question is incomplete,

That is how to make ServerSocket listen on all the Network Interface cards,

regards,
alpjose
0
cjjcliffordCommented:
Configure each of the interface cards with IP Addresses from different Sub-networks, then have the ServerSocket objects listen on the different IP addresses...
0
alpjoseAuthor Commented:
Though i configure ServerSocket to listen on a Address, it is not able to receive any data
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

cjjcliffordCommented:
what do you mean, not able to receive any data? Is the ServerSocket listening? try "telnet ipaddress portnum" where ipaddress is the address the ServerSocket is listening, and portnum is the port number in question....
0
alpjoseAuthor Commented:
I have 2 network cards,

161.x.x.x and 192.x.x.x

When both are enabled, 192.x.x.x is the default.
Though there are clients responding whose ip falls in the range (161.x.x.x), my Application is not able to receive .
but when i disable 192.x.x.x, then i am able to receive the data from clients whose ip falls in the range (161.x.x.x),.
ServerSocket is listening at 161.x.x.x ipaddress

hope this is clear.

0
cjjcliffordCommented:
ok, I think I'm with you now...
How are you creating your ServerSocket object? If you are not using the ServerSocket(int port,  int backlog,  InetAddress bindAddr) constructor the JVM will automatically listen on the "default" address (which in your case is the 192.... address.

Try:

// This assumes you have an entry in your hosts file for "secondary" being the secondary NIC's address... otherwise use getByAddress()...
ServerSocket ssock = new ServerSocket( portNumber, 100, InetAddress.getByName( "secondary" ) );

Cheers,
C.
0
alpjoseAuthor Commented:
I have tried the same thing, but still doesn't work.

that is

ServerSocket ssock = new ServerSocket( portNumber, 100, InetAddress.getByName( "secondary" ) );

0
nigelsheldonCommented:
Can you ping your machines alternate ip address when both cards are connected?
0
alpjoseAuthor Commented:
Yes i am able to ping
0
cjjcliffordCommented:
Humm, something is strange alright, I tried the same thing here on a box with 2 NICs (test code below). What do you see when you "ping -s secondary" on the commandline?

test code:

import java.net.*;
public class TestSocket {
    public static void main( String [] args ) throws Exception {
        byte[] primary = {}; // truncated
        byte[] secondary = {}; // truncated
        System.out.println( "Attempting primary" );
        ServerSocket psock = new ServerSocket( 6785, 100, InetAddress.getByAddress( primary ) );
        psock.accept();
        System.out.println( "Connection made" );

        System.out.println( "Attempting secondary" );
        ServerSocket ssock = new ServerSocket( 6785, 100, InetAddress.getByAddress( secondary ) );
        ssock.accept();
        System.out.println( "Connection made" );
    }
}

This works fine for me - I did "telnet primary 6785", "Connection made" was output, then telnet secondary 6785", "Connection Made output again - works correctly here, are you sure your NICs are correctly configured, and both up?

What OS have you on the Box? If Linux, run "/sbin/ifconfig", if MS, try "ipconfig" on commandline...

Cheers,
C.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cjjcliffordCommented:
I also tried the test code with the names in the /etc/hosts file for the primary and secondary, and used InetAddress.getByName(), and it still worked as expected...
0
nigelsheldonCommented:
I know that you are being shoved around here. But try this and see if you get the addresses that you expect.

Check out java.net.NetworkInterface. It was added in 1.4 and will allow you to get an Enumeration of all network cards and an Enumeration of all InetAddresses for each interface
0
cjjcliffordCommented:
Big aside, Sun do it again, they add a new class and use Classes that they themselves recommend against using in new implementations (i.e. the Enumeration!) Quoted from the JavaDoc for Enumeration: "New implementations should consider using  Iterator in preference to Enumeration"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.