PDA

View Full Version : all you mamma jamma cumputa programmas



dave_p
08-04-2002, 07:15 PM
i need to increment ip addresses for a little proof of concept thing i am doing. im using java for this cuz it makes socket programming easy. the constructors for sockets all accept strings for ip addresses so here is what i came up with. there are other ways im sure. i know of one converting the ip as a polynomial of 256 and so forth but anyway, here is what i tossed together. if anyone has a more elegant solution, please chime in.

public String incrementIp(String s){

int[] octet = new int[4];
int i=0;
StringTokenizer st=new StringTokenizer(s,".");

while(st.hasMoreTokens()&&i<4){
octet[i]=Integer.parseInt(st.nextToken());
i++;
}
octet[3]+=1;
if(octet[3]>254){
octet[3]=1;
octet[2]+=1;
}
if(octet[2]>254){
octet[2]=1;
octet[1]+=1;
}
s=new String(octet[0] + "." + octet[1] + "." + octet[2]
+ "." + octet[3]);
return s;
}

Hexis
08-05-2002, 11:35 AM
Can't you simply deal with the IP in int format?

intip = (octet[3]+(256*octet[2])+(256*256*octet[1])+(256*256*256*octet[0]);
intip++;

No reason to do math on the dotted decimal format.

dave_p
08-05-2002, 08:50 PM
i had considered it. i still have to convert back to strings because im getting the ip range as text strings from textboxes in a gui. the socket constructors take the ips as strings or inetAddress objects. its cleaner mathematically your way but an additional step will be required to factor it back out into octets to make the strings. im gonna try it out anyway.
thanks.

Hexis
08-05-2002, 09:07 PM
There is prob a DD to int IP conversion function somewhere, if not write a lil one. It makes a lot more sense to worko n IPs in int format than DD.

Miscue
08-05-2002, 09:23 PM
Looks like you're tossing 0 and 255... everything looks good to me, that's how I would write it.

Hexis, makes no sense to turn the IP into an integer because socket calls expect IPs in dotted quad strings.

dave_p
08-05-2002, 09:36 PM
yeah, i agree. i need a good book on socket programming. like you say, if i cant find a conversion function(method in java) ill just make one. then ill extend the Socket object to deal with an integer in the constructor.
thanks

edited after reading miscues post:
ok maybe ill leave it!
thanks both of you

Miscue
08-05-2002, 10:07 PM
Originally posted by dave_p
its cleaner mathematically your way but an additional step will be required to factor it back out into octets to make the strings.

Hexis's formula works... but what are you going to do with that number afterwards?

For instance: 1.1.1.1 = 16843009 = 01010101h
Increment: 1.1.1.2 = 16843010 = 01010102h

Now... 16843010 doesn't look like 1.1.1.2 at all.

Ok... now you got to reverse the formula used to make it into a useable IP. PITA! And, what about tossing your 0 and 255s? Still gotta convert it back to a dotted quad string too!

The way you picked out is a good solution... stick with that.

Hexis
08-05-2002, 11:56 PM
octet[0]=(IntIp/(256*256*256))&256;
octet[1]=(IntIp/(256*256))&256;
octet[2]=(IntIp/256)&256;
octet[3]=IntIP&256;

Depending on the network you may want to not exclude the 255 addresses. If you want to get nice and fancy test according to network with a subnet mask and exclude the network and broadcast addresses accordingly (which can be found with simple binary operations).

For example: excluding a *.0 and *.255 address would be bad if testing 10.10.10.10/23, in that case, 10.10.10.255 and 10.10.11.0 are both valid host IP addresses.

Hexis
08-06-2002, 11:35 AM
dave_p refresh my memory, it's been a while since I have done any sockets programming. You can use dotted decimal or FQDN for the hostname when opening a socket, right? If so then I'm pretty sure you can feed it an IP in int format and it will be just fine. Internally it should resolve whatever you give it into an unsigned int anyways, since that's the format in the header.

dave_p
08-06-2002, 05:24 PM
here are the constructors in JAVA for creating a socket:

public Socket(String host, int port)
throws UnknownHostException, IOException
public Socket(InetAddress address, int port) throws IOException
public Socket(String host, int port, InetAddress localAddress,
int localPort) throws IOException
public Socket(InetAddress address, int port, InetAddress localAddress,
int localPort) throws IOException

InetAddress is an object in java:
from a tutorial:

The InetAddress class is a little unusual in that it doesn't have any public constructors. Instead you pass the host name or string format of the dotted quad address to the static InetAddress.getByName() method like this:

try {
InetAddress utopia = InetAddress.getByName("utopia.poly.edu");
InetAddress duke = InetAddress.getByName("128.238.2.92");
}
catch (UnknownHostException e) {
System.err.println(e);
}

now this is just java. in C the address part of the Socket structure is an unsigned long as far as i know.in C++ im not sure, i have to look. im fairly new at sockets programming. i chose java to start out because it makes socket programming fairly simple to do. 2 pages of reading and some info from a RFC and i had a working SMTP client up in no time.
eventually im going to translate what i learn over to C and C++. i havent used java in a long time so i figured this sockets stuff was a good way to reaquaint myself and learn something to boot.

Miscue
08-07-2002, 08:38 PM
Socket programming in C/C++ is similar... same principles. I have a multi-threaded http client prog laying around somewhere... if you want to take a look at C socket programming, and multi-threading while you're at it. I'd have to dig around for it though.

dave_p
08-08-2002, 06:26 PM
if you stumble across it , itd be appreciated for sure, but dont go out of your way. im going to code my current project so each operation on each ip address runs in its own thread. java makes that pretty simple too. im a certified C programmer, but java is starting to appeal to me more and more.

Miscue
08-08-2002, 06:29 PM
Same here. Java is uber-good. Doing my internet programming with it for current project I'm working on. Goal: To never have to work again. :)