questions regarding Java network programming

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2791 Views - Last Post: 26 August 2010 - 06:21 PM Rate Topic: -----

#1 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 154
  • Joined: 15-December 08

questions regarding Java network programming

Posted 08 August 2010 - 09:34 PM

I've been writing some network applications in Java as a way to pass the time, and there are two types of applications that I have in mind that I am wondering if it is possible to write in Java.

My first application is an IP address generator, but so far I can only use random to generate numbers from the range of 1 to 255 in single digits, not in an IP address form (ie: 0.0.0.0) as I wanted to.

My second application is supposed to be an application that finds and lists all the computers available on a network, and then sends them back to the server where the program is launched from. My friend said that developing that kind of a program is impossible, but I think it is possible, and with multithreading included in the code, it'll speed up the program a bit in the quest to find the computers in the network, and somehow I kept on thinking what I'm doing is more like a worm.

So, I was wondering, is it possible to write both these applications in Java? I have searched the internet for people who might have done it before me, but so far the results are scant, to say the least.

Is This A Good Question/Topic? 0
  • +

Replies To: questions regarding Java network programming

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: questions regarding Java network programming

Posted 08 August 2010 - 10:00 PM

What do you want to write ? A DHCP server ? THat is easy in Java and will take less than 100 lines of code.

Finding a list of the host already in the network: Java is NOT the tool for that
Was This Post Helpful? 0
  • +
  • -

#3 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 154
  • Joined: 15-December 08

Re: questions regarding Java network programming

Posted 08 August 2010 - 10:29 PM

Well, what I have in mind isn't actually a DHCP server, it's more like a stand alone program where I can generate random IP addresses without having to connect to a computer, but I might look into a DHCP server as well.

And why is Java not the best tool for finding a list of hosts on a network?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: questions regarding Java network programming

Posted 09 August 2010 - 04:50 PM

View Posthdr, on 08 August 2010 - 11:29 PM, said:

And why is Java not the best tool for finding a list of hosts on a network?

Java is a high level language as hardware independant as possible
It is hard to write an application that will ask the ethernet port to transfer to the application all the frames passing by. It is possible but you will break Java portability and you will have to hardcode some device names somewhere and your program won't work on other platforms.
C++ and even better plain C is better to achieve what you want to do
Was This Post Helpful? 1
  • +
  • -

#5 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 154
  • Joined: 15-December 08

Re: questions regarding Java network programming

Posted 10 August 2010 - 03:13 AM

oh, I see. thanks for the information, didn't know that it'll require me to hardcode the devices and platforms into the code for me to be able to achieve it.

It's because I thought the program had to do was to connect to a computer, check if it is connected, and then report back to me. Didn't realize it was much work under Java.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: questions regarding Java network programming

Posted 10 August 2010 - 03:01 PM

View Posthdr, on 10 August 2010 - 04:13 AM, said:

It's because I thought the program had to do was to connect to a computer, check if it is connected, and then report back to me. Didn't realize it was much work under Java.

This is actually easier to do in Java than in C/C++ about half the lines of code.
I though you were talking about a sniffer like program.

If you already know the address of the hosts you want to check Java would be perfect for the job no hardcoding (beside your hosts address) required
Was This Post Helpful? 0
  • +
  • -

#7 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 154
  • Joined: 15-December 08

Re: questions regarding Java network programming

Posted 10 August 2010 - 10:26 PM

but what if the application I want to write is looking for unknown hosts in the network? would hardcoding for devices still be required? I was thinking of maybe implementing something like where I can find the list of hosts in a network, and then add them into a text file, and maybe it will check it again if I run the same application again in the future?
Was This Post Helpful? 0
  • +
  • -

#8 RascalRobot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 03-August 10

Re: questions regarding Java network programming

Posted 11 August 2010 - 05:49 AM

It sounds like your trying to ping all addresses on a local network.
Usually a local network is at address 192.168.x.x

So, you could setup a nested loop to try all values from 1 to 255 for
the last 2 octets ...

import java.net.InetAddress;

...

    InetAddress address = InetAddress.getByName(String.format("192.168.%d.%d", oct1, oct2));
    boolean reachable = address.isReachable(1000);



Was This Post Helpful? 0
  • +
  • -

#9 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 154
  • Joined: 15-December 08

Re: questions regarding Java network programming

Posted 11 August 2010 - 05:46 PM

that's one way of doing it, but what if I want to use it on a larger network, not just a LAN?

and I thought the addresses even in a LAN would be randomly generated instead of it being in a 192.168.x.x block?

but I like the idea, though. seems ideal for what I'm doing.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: questions regarding Java network programming

Posted 11 August 2010 - 08:29 PM

View PostRascalRobot, on 11 August 2010 - 06:49 AM, said:

It sounds like your trying to ping all addresses on a local network.
Usually a local network is at address 192.168.x.x

So, you could setup a nested loop to try all values from 1 to 255 for
the last 2 octets ...

import java.net.InetAddress;

...

    InetAddress address = InetAddress.getByName(String.format("192.168.%d.%d", oct1, oct2));
    boolean reachable = address.isReachable(1000);



Horribly inneficient but it will work. No hardcoding required but C is still the best tool for that. You can grab all the frames on the wire and decide what you want to do with it
Was This Post Helpful? 0
  • +
  • -

#11 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 154
  • Joined: 15-December 08

Re: questions regarding Java network programming

Posted 21 August 2010 - 11:25 AM

well, after a sometime procasinating, I finally got around to writing the ping application that pings the ip's in a 192.168.x.x block.

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

public class pingBlock
{
	public static void main(String args[])
	{
		int oct1, oct2;
		
		for(oct1=1; oct1 <=255; oct1++)
		{
			for(oct2=1; oct2 >= oct1; oct2++)
			try
		{
			InetAddress address = InetAddress.getByName(String.format("192.168.%d.%d", oct1, oct2));
			boolean reachable = address.isReachable(1000);

			System.out.println("Addr: " + address.getHostAddress());
       		System.out.println("Reach: " + address.isReachable(3000));
    	}
    		catch (UnknownHostException e)
    		{
    			System.out.println("unable to look up IP Address block");
    		}
			catch (IOException e)
			{
				System.out.println("unable to look up IP Address block");
			}
		}
	
	}
}



the issue right now is that when I run it, the result is this:

--------------------Configuration: <Default>--------------------
Addr: 192.168.1.1
Reach: false
Addr: 192.168.1.2
Reach: false
Addr: 192.168.1.3
Reach: true
Addr: 192.168.1.4
Reach: false
Addr: 192.168.1.5
Reach: false
Addr: 192.168.1.6
Reach: false
Addr: 192.168.1.7
Reach: false
Addr: 192.168.1.8
Reach: false
Addr: 192.168.1.9
Reach: false
Addr: 192.168.1.10
Reach: false
.....
Addr: 192.168.1.252
Reach: false
Addr: 192.168.1.253
Reach: false
Addr: 192.168.1.254
Reach: false
Addr: 192.168.1.255
Reach: false
unable to look up IP Address block
unable to look up IP Address block
unable to look up IP Address block
unable to look up IP Address block
unable to look up IP Address block

Process interrupted by user.

So shouldn't the application continue at 192.168.2.1 after it has finished pinging 192.168.1.255?

I guess there must have been something wrong I did in the loop, or is it due to my connection?
Was This Post Helpful? 0
  • +
  • -

#12 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: questions regarding Java network programming

Posted 21 August 2010 - 12:14 PM


for(oct2=1; oct2 >= oct1; oct2++)



when oct is equal to two, that loop wont run.
Was This Post Helpful? 0
  • +
  • -

#13 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 154
  • Joined: 15-December 08

Re: questions regarding Java network programming

Posted 21 August 2010 - 12:22 PM

so should the loop be

for(oct2 > 1; oct2 >= oct1; oct2++)



because I got this error:

C:\Programming\java\pingBlock.java:13: not a statement
for(oct2 > 1; oct2 >= oct1; oct2++)
^
1 error

or should it be

for(oct2 >= 1; oct2 >= oct1; oct2++)



because I also got the same error for that one as well.
Was This Post Helpful? 0
  • +
  • -

#14 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: questions regarding Java network programming

Posted 21 August 2010 - 12:27 PM

View Posthdr, on 21 August 2010 - 11:22 AM, said:

so should the loop be

for(oct2 > 1; oct2 >= oct1; oct2++)



because I got this error:

C:\Programming\java\pingBlock.java:13: not a statement
for(oct2 > 1; oct2 >= oct1; oct2++)
^
1 error

or should it be

for(oct2 >= 1; oct2 >= oct1; oct2++)



because I also got the same error for that one as well.

it should be
for(oct2=1; oct2 <=255; oct2++)


you get those errors, because you are trying to be a comparing operator when you need an assignment operator.

This post has been edited by ianian112: 21 August 2010 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#15 hdr  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 154
  • Joined: 15-December 08

Re: questions regarding Java network programming

Posted 21 August 2010 - 12:39 PM

ok thanks. took me sometime to figure it out, thanks again for helping.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2