obtaining local IP address

obtaining local IP address

Post by clewel » Fri, 09 Jul 1999 04:00:00



I'm working on a GUI FTP client application in Java. In order to
implement the PORT command I need the IP address of the client machine
i.e. an IP address that the FTP host can use to connect to a port on the
client machine. The Java method "getLocal Host()" just returns
127.0.0.1".  I'm looking for some way to get this, hopefully a UNIX
command that would return the IP number.

Thanks Clu

  budh.vcf
< 1K Download
 
 
 

obtaining local IP address

Post by Glen Wile » Fri, 09 Jul 1999 04:00:00


: This is a multi-part message in MIME format.
: --------------3A9D1D027FECBF4F7DAB0838
: Content-Type: text/plain; charset=us-ascii
: Content-Transfer-Encoding: 7bit

: I'm working on a GUI FTP client application in Java. In order to
: implement the PORT command I need the IP address of the client machine
: i.e. an IP address that the FTP host can use to connect to a port on the
: client machine. The Java method "getLocal Host()" just returns
: 127.0.0.1".  I'm looking for some way to get this, hopefully a UNIX
: command that would return the IP number.

: Thanks Clu

in C the following program will obtain all of the IP addresses
available for the machine:

/* on Solaris build using: cc -o getip getip.c -lxnet */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>

int
main(int argc, char *argv[])
{
        struct hostent *he;
        char   hn[256];
        int    i = 0;
        struct in_addr *addr;

        /* get our hostname */

        if(gethostname(hn, 256) != 0)
        {
                perror("gethostname");
                return 1;
        }

        printf("host: %s\n", hn);

        /* enumerate addresses available on this host */
        he = gethostbyname(hn);
        if(he)
        {
                while(he->h_addr_list[i])
                {
                        addr = (struct in_addr *) he->h_addr_list[i];
                        printf("  %s\n", inet_ntoa(*addr));
                        i++;
                }
        }
        else
                perror("gethostbyname");

        return 0;

Quote:}

--

Senior Software Engineer       http://www.wwa.com/~gwiley/glen
3Com - Carrier Systems R&D

"UNIX _IS_ user friendly, its just picky about who its friends are."

 
 
 

obtaining local IP address

Post by Bjorn Rees » Fri, 09 Jul 1999 04:00:00



> I'm working on a GUI FTP client application in Java. In order to
> implement the PORT command I need the IP address of the client machine
> i.e. an IP address that the FTP host can use to connect to a port on the
> client machine. The Java method "getLocal Host()" just returns
> 127.0.0.1".  I'm looking for some way to get this, hopefully a UNIX
> command that would return the IP number.

The only _correct_ way to get this address is to use the getsockname()
system call on the opened connection.

It might be possible to obtain this through JNI (although it is a bit
tricky.)

The procedure would be something like this: pass the socket descriptor
through JNI, use getsockname(), extract the address from 'struct sockaddr
*' (by casting it to 'struct sockaddr_in *' first), and return this
address in some format you find appropriate.

 
 
 

obtaining local IP address

Post by jcpm.. » Tue, 20 Jul 1999 04:00:00




Quote:> I'm working on a GUI FTP client application in Java. In order to
> implement the PORT command I need the IP address of the client machine
> i.e. an IP address that the FTP host can use to connect to a port on
the
> client machine. The Java method "getLocal Host()" just returns
> 127.0.0.1".  I'm looking for some way to get this, hopefully a UNIX
> command that would return the IP number.

If I understand well:
1. The client opens a socket to the server.
2. The server needs to open a socket on the client on a different port.

Why can't you use the original socket on the server, the one the
client opened, and use getInetAddress() to extract the IP address from
that.

JC

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

 
 
 

obtaining local IP address

Post by Pete Mullin » Tue, 20 Jul 1999 04:00:00


Maybe use the gethostbyaddr() on the client side of the connected (control)
socket. This way you can be sure to get the IP address of the correct
interface.

Regards,

Pete M.




> > I'm working on a GUI FTP client application in Java. In order to
> > implement the PORT command I need the IP address of the client machine
> > i.e. an IP address that the FTP host can use to connect to a port on
> the
> > client machine. The Java method "getLocal Host()" just returns
> > 127.0.0.1".  I'm looking for some way to get this, hopefully a UNIX
> > command that would return the IP number.

> If I understand well:
> 1. The client opens a socket to the server.
> 2. The server needs to open a socket on the client on a different port.

> Why can't you use the original socket on the server, the one the
> client opened, and use getInetAddress() to extract the IP address from
> that.

> JC

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

 
 
 

1. Need help obtaining dynamic local IP Address using PPP

Hi

I need help. Just installed linux 1.2.13 from slackware distribution disks. I'm
using DIP to dial my service provider who provides a PPP connection and a
dynamic local IP address.

I've configured it with 0.0.0.0 and when it makes the connection I want to
the real local IP address and set it. My problem is that my ISP doesn't display
the address after logging in.

I am able to use Netscape, ircII, etc configured this way but at times
transmissions stall and I'm assuming this to be the problem.

I've also tried PPP with the same results.

Anyone out there have any suggestions?

John Yungk

2. Problem with CGI on Apache1.2b8 on FreeBSD2.1.7

3. Obtaining IP Address from Hardware Address

4. ip_masq_quake old in newer kernels??

5. obtain IP address from a mac address

6. Mandrake 7.0 problem

7. obtaining local address

8. What's wrong with SLS?

9. How to get local IP address, router address, etc when using DHCP

10. Bell-Atlantic tollfree dialups, no local ip obtained.

11. DIP: How to obtain local IP-adress

12. Server obtaining client's IP Address. How?

13. Obtain IP address of client's machine in called Perl script