?
Solved

How to force JVM Listen on a particular Network Interface card

Posted on 2004-10-27
13
Medium Priority
?
494 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
Comment
Question by:alpjose
  • 6
  • 5
  • 2
13 Comments
 

Author Comment

by:alpjose
ID: 12420833
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
ID: 12421015
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
ID: 12421211
Though i configure ServerSocket to listen on a Address, it is not able to receive any data
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 11

Expert Comment

by:cjjclifford
ID: 12421294
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
ID: 12421347
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
ID: 12421525
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
ID: 12430304
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
ID: 12431297
Can you ping your machines alternate ip address when both cards are connected?
0
 

Author Comment

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

Accepted Solution

by:
cjjclifford earned 400 total points
ID: 12431572
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
ID: 12431585
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
ID: 12432053
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
ID: 12432783
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

607 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