Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a Linux VPS with 2 virtual network interfaces. Each one has it own IP :

                  +          +
                  |          |
               venet0:0   venet0:1
          78.***.***.52   78.***.***.66

My aim is to make HTTP Requests, with both IPs, dynamically. For example, I would like to be able to read "" with IP1 and "" with IP2, in the same instance of program (java).

Is it possible ?



share|improve this question
up vote 0 down vote accepted

This is actually a Java question and more appropiate for

However, it's worth noting that in general, this technique is common across all programming languages that don't abstract away the socket interface too much from you.

The default way to do this is by specifying the interface or IP within the socket.

You need to pass an InetAddress to Socket.bind(). You can get an Eumeration of IP addresses from a specific interface with the following code:

  String iface = "venet0:0";
  NetworkInterface nif = NetworkInterface.getByName(iface);
  Enumeration nifAddresses = nif.getInetAddresses();

  while (nifAddresses.hasMoreElements()) {
    InetAddress addr = (InetAddress) nifAddresses.nextElement();

You then need to create your socket with one of these InetAddresses. If you have an InetAddress object selected called addr then the following will work

  Socket soc = new;
  soc.connect(new InetSocketAddress(address, port));

This technique works for most programming languages on Linux. You're using bind(2) with a specific IP to tell the system what IP address and interface you'll be binding to.

share|improve this answer
Hi, Thanks for this great anwswer. I apologize to post it here, but my main question was to know if is possible. Java was my second question :). Anyway, I don't understand the difference of addr and address. soc.bind(addr); soc.connect(new InetSocketAddress(address, port)); I think 'addr is 78.***.***.52 for exemple, one of the ip of my VPS. But 'address', I don't understand. Thanks, Bill0ute – bill0ute Dec 24 '09 at 2:00
It works ! Thanks a lot :) Bill0ute – bill0ute Dec 26 '09 at 22:03

Actually, that's pretty simple.

If you know your IP addresses, for exemple IP1 and IP2, you just have to creat a socket and bind it to the ip you want to use. For exemple, if IP1 = :

Socket  s = new Socket ();
s.bind    (new InetSocketAddress ("", 0)); // The second attribut is the Port. If it's value is 0, Java will automatically choose a free port.
s.connect (new InetSocketAddress ("myServerIp", 80));

Then, you send your HTTP request. I do it like that :

PrintWriter     writer = new PrintWriter    (s.getOutputStream ());
BufferedReader  reader = new BufferedReader (new InputStreamReader (s.getInputStream ()));

// Its an exemple of a GET request
writer.print ("GET HTTP/1.0\r\n\r\n"); 
writer.flush ();

StringBuffer  sbResp = new StringBuffer ();
String        szLine = new String       ();

// I get the response here
while ((szLine = reader.readLine ()) != null)
    sb.append (szLine);

s     .close ();
reader.close ();
writer.close ();

If you don't know you IP addresses, just read Phil answer. He explains everything about it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.