Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4107059

ftp protocol fails when the client machine has multiple IP adresses

XMLWordPrintable

    • beta
    • generic, x86
    • generic, windows_98



      Name: dgC58589 Date: 01/27/98


      [the text from the user is reformatted and more explanations
      are added after exchanging e-mail (###@###.###

      The problem appears when a machine that has more than one
      IP addresses tries to use a ftp URL to get something. The two
      IP addresses are not routable one to another - this situation
      is normal for a machine connected to an internal LAN with some
      unroutable addresses (example 10.1.1.1) and with a dial up
      connection.

      When the ftp protocol binds a server socket for receiving data
      from the server it send the socj`ket IP and port to the server
      so that the server can connect to it. The address sent to the server
      is the one returned by InetAddress.getLocalHost().getAddress().
      When the host has multiple addresses there is no way to reliably
      tell which one should be returned - the internal one will not
      wrok when connecting to an external host and the external one
      will not work when connecting to an internal host.

      A possible suggested fix is to use the local address from the
      client socket already connected to the server - if the server can
      talk to it and the server socket is bound with inAddrAny then
      this should work. The question is wether the getLocalAddress()
      [acctually getsockname() in the C library] will return the good
      address for a connected socket in the case of multiple IP addresses.

      Here is the test case sent by the user:

      import java.io.*;
      import java.net.*;

      /**
       *
       * Program to snarf files from a random website.
       *
       */
      public class Snarf {
        public static void main(String[] args) {
          InputStream inStream = null;
          OutputStream outStream = null;

          try {
            // Check args.
            if (args.length < 2) {
      throw new IllegalArgumentException("Wrong number of arguments.");
            } else {
      String urlRoot = args[0];
      for (int i = 1; i < args.length; i++) {
      URL turl = new URL(urlRoot + args[i]);
      System.out.println("Snarfing " + turl + " via " +
      turl.getProtocol() + " on port " +
      turl.getPort() + ".");
      String protocol = turl.getProtocol();
      int port = turl.getPort();
      String host = turl.getHost();
      String file = turl.getFile();
      turl = null;
      URL url = new URL(protocol, host, port, file);
      URLConnection urlConnection = url.openConnection();
      inStream = urlConnection.getInputStream();
      outStream = new FileOutputStream(args[i]);
      byte[] buffer = new byte[1096];
      int bytes_read = 0;
      while((bytes_read = inStream.read(buffer)) != -1) {
      outStream.write(buffer, 0, bytes_read);
      System.out.print(".");
      }
      System.out.println("\nDone with " + url);
      inStream = null; outStream = null; buffer = null; url = null;
      }
            } // Args were OK.
          } catch(Exception e) {
            // Bail informatively.
            System.err.println(e);
            System.err.println("Usage: java Snarf URL files ...");
          } finally { // Always close streams!
            try {
      inStream.close();
      outStream.close();
            } catch(Exception e) {
      /* NOTHING */
            }
          }
        }
      }
      (Review ID: 22303)
      ======================================================================

            jccollet Jean-Christophe Collet (Inactive)
            dgrahamcsunw David Graham-cumming (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: