[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to force JVM Listen on a particular Network Interface card

Posted on 2004-10-27
13
Medium Priority
?
482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

650 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