Link to home
Start Free TrialLog in
Avatar of alpjose
alpjose

asked on

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
Avatar of alpjose
alpjose

ASKER

sorry, the question is incomplete,

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

regards,
alpjose
Configure each of the interface cards with IP Addresses from different Sub-networks, then have the ServerSocket objects listen on the different IP addresses...
Avatar of alpjose

ASKER

Though i configure ServerSocket to listen on a Address, it is not able to receive any data
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....
Avatar of alpjose

ASKER

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.

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.
Avatar of alpjose

ASKER

I have tried the same thing, but still doesn't work.

that is

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

Can you ping your machines alternate ip address when both cards are connected?
Avatar of alpjose

ASKER

Yes i am able to ping
ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

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
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...
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
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"