how to get all the system names connected to LAN using java? - java

I want to get all the systems connected in a LAN?
Can anyone suggest me?

The Package I used in Java to achieve this was called jcifs. Here is the link to the Library .
Please note, to identify a machine on a subnet you will need to ping all the avaliable Ip addresses on the subnet. From there you can do a reverse IP Address lookup to get the machines details.
From memory from the OSI the reason why you want to ping all the machines on the network is because ICMP is still the lowest layer on the OSI Model. Though you just cannot trust just using ICMP (ping) requests to all machines on the subnet. The reason for this is most Windows Machines to prevent fishing attacks will block that protocol. So instead, you need to take a two stage detection approach. First, broadcast over the SubNet Ip address with a ICMP ping request. From the ICMP ping request after waiting 1 second, proceed to perform a SMB connection to the non responding IP addresses. You will find that most people will have either a shared printer of Microsoft Network that will respond to a request under that port.
The alternative is to use a NetBios reverse name lookup, though it still involves spamming the IP address range of the subnet.
Hope this helps.

Why do you want them, and why must they be names? Not all systems have a name.
What kind of systems are you trying to enumerate?
If you're trying to locate nearby instances of your own application, use multicast UDP to create a discovery protocol.
If you're trying to locate some server which has its own protocol for that purpose, then use its one.

If I was going to try to implement this in Java, I'd pick some unused TCP/IP port number and then try to open a socket to each IP address in the LAN's IP address range(s). You expect all the connection attempts to fail, but they should fail in different ways depending on the whether a machine is using the IP address. If an IP address is in use, you should get a "connection refused". If it is not in use, you should get a "no route to host" or "no route to network". A "connect timed out" may indicate that the host is fire-walled, or that was recently alive but is not alive at the moment.
Another approach (which I don't know is you can do from Java) is to send ARP requests for each of the IP addresses in the network address range and see what turns up in your machine's ARP cache.
And of course, you can try sending ICMP Ping messages, but they may be firewalled.
Once you have a list of live IP addresses, use DNS reverse lookup to find the corresponding DNS names. But beware that not all IP addresses are bound to DNS names.
However this is all a bit tenuous given that a lot of machines and networks use firewalls of various kinds, and these are wont to drop network messages or send misleading responses.

Related

Different IP Addresses from the same machine

I have a client-server setup. Where client connects to the server and asks which program to execute next. Server after getting a request checks for the hostname of the other end of the socket using below chunk of code which returns "127.0.0.1"
socket.getInetAddress().getCanonicalHostName();
After getting the program name the client creates a process which also tries to connect to the server but this time the above call returns different address. "mypc.foo.com" masking the domain name here
This behavior is bugging me as I am unable to lookup the hashmap where I store all the process details grouped by the machine ip.
Is it a bug in java lookup implementation or am I missing something. Please suggest some other way to do this lookup
I beleive socket.getInetAddress() returns your own address so basically always 127.0.0.1. Try using socket.getRemoteSocketAddress() - to get the other party's ip address.
Using IP address to match distinct users is generally a bad idea though. First of all they can be in some network or behind firewall and you can get requests from multiple clients coming from the same IP address. Also you are not guaranteed that it is static or dynamic IP. And also if your application is running in some strange network setup with strange routing you might end up getting all requests from the router IP address.
The better design would be to share some token between the server and the client and use that for identification. Does it sound familiar? For example http sessions are done like that ;)

Static and Dynamic Ip address problems

Let's say we are hosting on a Personal Computer and Server Program is written in Java.
1.If we host server using static IP-address then does that means we can change machine and replace it with other ones, restart machine as many times we want and our Client will still be able to communicate with us after system is back?
2.if we host server using Dynamic IP-address then just by restarting once the machine we have to tell every single client out there that new IP-address is this one?
3.Will Dynamic IP-addresses change even if we don't restart the machine ?
Yes, a static ip address means that it is fixed. There is more to the whole system, of course, since there is resolution of an IP to a MAC address. Nonetheless, a static IP (assuming it is only on the network once) means that any machine with that IP will respond. So a client can always connect to the IP address.
Note: not a very friendly way to go.
Just because something has a "dynamic" IP address does not necessarily mean it changes every single time. Using DHCP it is possible to assign the same IP address to the same MAC address each time. It is a much better approach than hardcoding an IP address to a machine.
In addition, you really should not have your clients use an IP address to connect. They should look up the machine by a name in DNS. You can coordinate the DNS lookup to the DHCP, so machines do not need to do anything but resolve the hostname.
However, as your question stands, if a machine uses DHCP and does not receive the same IP address each time, and you have your clients connecting by IP address, then on each new assignment the clients would need the new IP address.
Whether a machine's IP address updates even when it does not restart is a policy that is controlled by the DHCP server. In general, there is a renewal time for an IP address. Without going into great deal, the client during the DHCP conversation may request the same IP address, but it is up to the server as to whether to hand out the same one or not. Conversely, most servers are configured to hand out the same IP address to a given MAC address as long as the DHCP cache is current. It is completely a policy decision.
At the end of the day, however, it seems like you are attempting to tie clients to a server's IP address, and this is not a good strategy. It is a better strategy to use DNS and have the clients resolve a hostname. If I am incorrectly inferring your intent, I apologize.
NOTE: I have used DHCP as the way to give out dynamic IP addresses. You might use some other strategy, but I think the concept is the same.
1: Yes, if the server has a static IP clients will always be able to count on the server being at that IP address.
2: Most likely, yes. In many networks the DHCP server will give the same IP back to a machine that has rebooted but you certainly should not count on that.
3: Yes - dynamically assigned IP addresses have a "TTL" - a time to live. This may be a long time (weeks or more) but they still could expire and change. Most often the machine will get the same IP back but, again, you should not count on that.

Setting the UDP datagram source on a multicast join

I have a windows XP machine having a single network adapter with 2 IP addresses (basically there are 2 subnets on the same physical network):
IP Address 1: 1.51.1.2 mask 255.255.0.0
IP address 2: 1.162.1.2 mask 255.255.255.0
Default Gateway: 1.51.1.1
My application which is written in java needs to communicate with other applications using IP multicast on say IP 224.0.5.1. However, this does not work as sound as I add the second IP address to the network adapter. I have done some digging with good old Wireshark and come to the conclusion that the IGMP join request is the problem. The source IP on the datagram is 1.51.1.2 in the case where it works and 1.162.1.2 when it does not. The gateway which is a router seem to ignore the join if the source is not on the same subnet as it itself is configured to be on. You could say that is somewhat fine (although the IGMP spec is a bit silent here), but I cannot see any way to actually influence the source IP of the datagram in any of the many socket APIs I have looked at so far. Everything seems to occur on the network interface with no regards to the actual subnet used.
Also strangely it seems that the source IP on the datagram is actually chosen to be the highest IP number which I guess is predictable but not exactly intuitive. So here are my questions:
Is there any way in Java or any of the native windows libraries to set the source IP on the UDP datagram of the IGMP request?
Is the fact that the gateway ignores the join because it does not recognise the source the correct behavior (I am going with de facto behavior as the spec is vague on this point)?
About a way to actually influence the source IP of the datagram:
SocketOptions.setOption(IP_MULTICAST_IF, InetAddress);
OR
MulticastSocket.setInterface(InetAddress)

How can I get all IP addresses of a subnet which run on a specific port

I want to find all systems that are on the same local network i.e. that have the same subnet mask.
I think I must work with InetAddress Class in Java but I don't know what should I do.
My algorithm is:
Find all available system in a local network
Check if they run on a my requested port
Keep in mind the IP of system which runs on this port
InetAddress localHost = Inet4Address.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
System.out.println(networkInterface);
networkInterface.getByInetAddress(localHost);
for (){
networkInterface.getByInetAddress(localHost)
}
I think that the networkInterface.getByInetAddress(localHost) can give me all available IP addresses in a subnet, but I don't how I can do this for all available systems, and what is the condition for the for clause.
Finding all of the possible IP addresses in an IP subnet is simple. Just write a loop that enumerates all possible byte[] representations in the subnet and constructs InetAddress instances for them.
That's the easy bit, the hard bit is solving these two problems:
how do you find all if the live IP addresses on a subnet, and
for a given IP address, how do you figure out if it is using a particular port.
The first problem can be solved in two ways:
You could use the InetAddress.isAlive() method to see if the host responds to a ping. The problem is that some hosts may be configured to not respond to pings, or the pings may be blocked by an internal firewall. This also doesn't work if the subnet is large. The problem is that you need to ping a large number of hosts, and that will take a long time and generate a lot of network traffic ... especially if there are multiple application instances doing this.
The smarter alternative is to examine your local machine's ARP cache, and extract all IP addresses that it knows about. The problem is that 1) your application may not have the access rights to access the ARP cache, 2) some IP addresses may not be in the cache, and 3) there is no way to access the ARP cache in pure Java.
The second problem depends on the nature of the service on the port:
If it is a TCP-based service, then you can attempt to connect to an IP/port using a plain socket. If the connection is accepted, then the port is in use by something. It may or may not be the service you expect, but in general the only way to tell that is to try to use the service.
If it is a UDP-based service, then there's now way in general to know if something is using the port.
I'm going to hope that you mean the following:
"I wish to discover all systems on the same local network as (the computer running my program)'s primary IP address which are listening to a certain TCP port."
With UDP, this is a slightly harder problem - you could look for bounces, but your packet may be eaten or lost (as could the reply).
Now, assuming TCP, you have three steps.
Get the IP address of the local interface you believe is the "primary" one.
Enumerate hosts on the same subnet as this address.
For each host, determine if the appropriate port is listening.
For step 1, use InetAddress.getLocalHost().
For step 2, get the subnet portion of the retrieved address, and then try every possible host within that subnet. Let's hope it's a Class C and there are only 254 hosts. There is no 100% sure way to get all the hosts without trying each one. A non-100%-certain (and more complex) way is to use ARP tables to get a router's view of the network, to send a broadcast ping to the local net, or to fall down to layer 2 and send a broadcast Ethernet packet.
For step 3, open a connection to each host on the appropriate port and then close it. If the connection times out or is denied, scratch the host. Otherwise, add it to the "listening" list. You probably want to try more than one connection at a time and set the timeout interval to something small, since most people won't be listening.
Step 4: congratulations! You've just made a very feature-poor version of nmap in Java.

Java Socket Programming

Java Socket Program did not work for WAN
I have written a TCP IP socket program which works fine in my LAN.
One of my friend is at bangalore He ran the server and I ran the
client with the host name of my friend's IP. In this case my
socket program did not work.
You said that your program is attempting to connect to host 192.168.1.107 port 46216.
192 prefix specifies it is a class C address and is private. Making your program connect to that will force it to remain on the local network searching for that node. You will need to find the IP address of your router (you can use http://whatismyip.org/ to find this out). Then go into your router settings and forward port 46216 to 192.168.1.107 (your node), or even better, your MAC address which is not subject to change (in case your router is running DHCP).
on a side note, it isn't good to hardcode IP addresses. Simply use a textfield to avoid having to redistribute the client when your IP is changed, as it is likely you have a dynamic IP from your ISP.
Your friend running the server is most likely behind either a firewall or NAT. Make sure you are using the external IP address and if necessary port forwarding the packets to the correct IP.
The IP address you gave seems to be a local address, rather than a public internet address. If you are looking for 192.x.x.x, you will not make it out to "the internet", but will be confined behind your router.
WhatIsMyIP is a good way of getting a public IP address, and THAT is the address you should use in your connection. Also, be sure to forward any ports that will be used by your program, because otherwise your router will likely filter the traffic and still create an issue.
You could use an implementation of STUNT or other NAT Traversal protocol.
The ip of computer on thih u deployed your server program is not in your reach.
192.x.x.x ip means (class C) it is for local subnet.
You need to have change your ip address of your net-adapter so that your router could route it through internet.

Categories