Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

IP addresses stored in Java?

GeeManGeeMan Member Posts: 12
Hi there,

I am writing a distributed application and was just wondering if anyone knows the data type Java recognises IP addresses as.

For example, does java recognise the ip address 192.168.0.1 as a string, long, float etc. or does the URL class define a special data type for IP addresses. If so - how is it declared?

I am wanting to store Ip addresses in some sort of list or array from different clients and then be able to use stored ip addresses at a later point from within the program.

Always appreciated

Comments

  • : Hi there,
    :
    : I am writing a distributed application and was just wondering if anyone knows the data type Java recognises IP addresses as.
    :
    : For example, does java recognise the ip address 192.168.0.1 as a string, long, float etc. or does the URL class define a special data type for IP addresses. If so - how is it declared?
    :
    : I am wanting to store Ip addresses in some sort of list or array from different clients and then be able to use stored ip addresses at a later point from within the program.
    :
    : Always appreciated
    :

    There is the InetAddress class in the java.net package. However, I don't think this would be much useful, since you can only get the addresses of a known host-name or the local host ... I can not see a way to create an InetAddress object from a known IP-address.

    I think you should use an own class to encapsulate the IP-addresses.

    tron.
  • GeeManGeeMan Member Posts: 12
    : : Hi there,
    : :
    : : I am writing a distributed application and was just wondering if anyone knows the data type Java recognises IP addresses as.
    : :
    : : For example, does java recognise the ip address 192.168.0.1 as a string, long, float etc. or does the URL class define a special data type for IP addresses. If so - how is it declared?
    : :
    : : I am wanting to store Ip addresses in some sort of list or array from different clients and then be able to use stored ip addresses at a later point from within the program.
    : :
    : : Always appreciated
    : :
    :
    : There is the InetAddress class in the java.net package. However, I don't think this would be much useful, since you can only get the addresses of a known host-name or the local host ... I can not see a way to create an InetAddress object from a known IP-address.
    :
    : I think you should use an own class to encapsulate the IP-addresses.
    :
    : tron.
    :
    I am using InetAddress class in my client to get the clients IP addrss - this sends the IP address back to the server no probs - but how to store it within the server so it can be accessed as an ipaddress for the server to use at a later point in the program is the problem.

    I could parse it as a 32 bit float I suppose but then it is no longer an ip address - maybe I can somehow then convert it back to an ip address when it is needed again in the program?

    Any ideas anybody?

    ThnX G
  • : : : Hi there,
    : : :
    : : : I am writing a distributed application and was just wondering if anyone knows the data type Java recognises IP addresses as.
    : : :
    : : : For example, does java recognise the ip address 192.168.0.1 as a string, long, float etc. or does the URL class define a special data type for IP addresses. If so - how is it declared?
    : : :
    : : : I am wanting to store Ip addresses in some sort of list or array from different clients and then be able to use stored ip addresses at a later point from within the program.
    : : :
    : : : Always appreciated
    : : :
    : :
    : : There is the InetAddress class in the java.net package. However, I don't think this would be much useful, since you can only get the addresses of a known host-name or the local host ... I can not see a way to create an InetAddress object from a known IP-address.
    : :
    : : I think you should use an own class to encapsulate the IP-addresses.
    : :
    : : tron.
    : :
    : I am using InetAddress class in my client to get the clients IP addrss - this sends the IP address back to the server no probs - but how to store it within the server so it can be accessed as an ipaddress for the server to use at a later point in the program is the problem.
    :
    : I could parse it as a 32 bit float I suppose but then it is no longer an ip address - maybe I can somehow then convert it back to an ip address when it is needed again in the program?
    :
    : Any ideas anybody?
    :
    : ThnX G
    :

    Is the storage the problem? I didn't got your question that way.

    You can use a java.util.Vector to store the InetAddress objects. See this example:

    [code]
    import java.util.Vector;

    Vector clientIPs = new Vector();

    void addIPAddress(InetAdress ina) {
    clientIPs.addElement(ina);
    }

    InetAddress getIPAddressAt(int i) {
    return (InetAddress) clientIPs.elementAt(i);
    }

    void removeIPAddressAt(int i) {
    clientIPs.removeElementAt(i);
    }

    void removeIPAddress(InetAddress ina) {
    clientIPs.removeElement(ina);
    }
    [/code]

    tron.
  • : : : Hi there,
    : : :
    : : : I am writing a distributed application and was just wondering if anyone knows the data type Java recognises IP addresses as.
    : : :
    : : : For example, does java recognise the ip address 192.168.0.1 as a string, long, float etc. or does the URL class define a special data type for IP addresses. If so - how is it declared?
    : : :
    : : : I am wanting to store Ip addresses in some sort of list or array from different clients and then be able to use stored ip addresses at a later point from within the program.
    : : :
    : : : Always appreciated
    : : :
    : :
    : : There is the InetAddress class in the java.net package. However, I don't think this would be much useful, since you can only get the addresses of a known host-name or the local host ... I can not see a way to create an InetAddress object from a known IP-address.
    : :
    : : I think you should use an own class to encapsulate the IP-addresses.
    : :
    : : tron.
    : :
    : I am using InetAddress class in my client to get the clients IP addrss - this sends the IP address back to the server no probs - but how to store it within the server so it can be accessed as an ipaddress for the server to use at a later point in the program is the problem.
    :
    : I could parse it as a 32 bit float I suppose but then it is no longer an ip address - maybe I can somehow then convert it back to an ip address when it is needed again in the program?
    :
    : Any ideas anybody?
    :
    : ThnX G
    :

    You're writing that the client sends the ip-address to the server. Umm, well ...?

    If you are using the ServerSocket class you get a Socket object for every client that connects to your server (accept method on ServerSocket). Instead of storing the ip-addresses of the connected clients you should rather store these Socket objects - you can get the ip-address later at any time by calling the getInetAddress() method on such a Socket object.
Sign In or Register to comment.