Solved

How to force JVM Listen on a particular Network Interface card

Posted on 2004-10-27
418 Views
Last Modified: 2010-03-31
My PC has 2 network interface cards.
How to force JVM Listen on a particular Network Interface card that is make serversocket
0
Question by:alpjose
    13 Comments
     

    Author Comment

    by:alpjose
    sorry, the question is incomplete,

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

    regards,
    alpjose
    0
     
    LVL 11

    Expert Comment

    by:cjjclifford
    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
     

    Author Comment

    by:alpjose
    Though i configure ServerSocket to listen on a Address, it is not able to receive any data
    0
     
    LVL 11

    Expert Comment

    by:cjjclifford
    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
     

    Author Comment

    by:alpjose
    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
     
    LVL 11

    Expert Comment

    by:cjjclifford
    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
     

    Author Comment

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

    that is

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

    0
     

    Expert Comment

    by:nigelsheldon
    Can you ping your machines alternate ip address when both cards are connected?
    0
     

    Author Comment

    by:alpjose
    Yes i am able to ping
    0
     
    LVL 11

    Accepted Solution

    by:
    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
     
    LVL 11

    Expert Comment

    by:cjjclifford
    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
     

    Expert Comment

    by:nigelsheldon
    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
     
    LVL 11

    Expert Comment

    by:cjjclifford
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    910 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now