0

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

                      vps
                    (venet0)
                  +          +
                  |          |
               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 "http://stackoverflow.com" with IP1 and "http://serverfault.com" with IP2, in the same instance of program (java).

Is it possible ?

Thanks,

Bill0ute

bill0ute
  • 3
  • 1

2 Answers2

0

This is actually a Java question and more appropiate for stackoverflow.com.

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();
    System.out.println(addr.getHostAddress());
  }

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 java.net.Socket();
  soc.bind(addr);
  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.

Philip Reynolds
  • 9,751
  • 1
  • 32
  • 33
  • 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 02:00
0

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 = 56.89.122.5 :

Socket  s = new Socket ();
s.bind    (new InetSocketAddress ("56.89.122.5", 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://myUrl.com 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.

bill0ute
  • 3
  • 1