I am building a Flex application which communicates with a Java server using BlazeDS.
What would be the best way to go about making the server aware of the client's hostname?
Thanks.
You can't determine the client's hostname from within Flex and it's not possible with JavaScript either. However, you can try to lookup the hostname on the server side using a reverse DNS lookup.
First, you'll need to determine the client's IP address. Take a look at "Can I get the client's IP address in an LCDS service call?" for an example on how to do it in BlazeDS.
In order to get the hostname you need to execute a reverse DNS lookup for the IP address. Please keep in mind that not every IP address has a hostname that can be determined throug reverse DNS lookups.
Related
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 ;)
My question feels kind of basic, and yet it has made me curious for a while:
Does using the name of a server instead of its IP address work when using a Java Socket?
For example, if I am the manager of a certain server with the address "bogusserver.com" and use this address instead of the actual IP of the server when opening the Socket with a 'new Socket("bogusserver.com", 8080);' will it actually open the socket normally?
If it does, how exactly does the Socket class solve the address? Does it use the DNS server registered at the computer running the code?
I know that using the "localhost" address the Socket will open normally, but then, everything works with localhost :P
You just need to read the javadoc. A Socket is constructed using an InetAddress.
The InetAddress javadoc says:
Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned.
Does using the name of a server instead of its IP address work when using a Java Socket?
DNS maybe used by the OS to translate the hostname into an IP address. It doesn't matter if you use TCP, UDP or ICMP.
If it does, how exactly does the Socket class solve the address?
It passes it to the OS to do the lookup.
Does it use the DNS server registered at the computer running the code?
The OS has registered DNS servers and they may be contacted if there isn't a hosts entry for that hostname.
I have a simple security routine that uses an MD5 hash of a concatenation of an IP address and a shared secret from client to server. (The client provides this hash to the server, and the server makes its own hash based on the client's IP and checks for equality.)
The client end may have multiple network interfaces; in development for example we have a public IP address, localhost, and a VPN network address, not to mention IPv6 addresses.
When making requests from the client to the host the client needs to concatenate the IP address of the requesting interface to the shared secret. I'm hung up on the requesting interface part. Other than making this a configuration item is there anyway to determine through the JDK which network interface will be used for resolving and transmitting to a particular host? (I already have the host name as a configuration item so that's not an issue.)
This is fundamentally a network question, not a Java question. And there is no direct way to know your own public IP address even if you only have one interface, let alone several. You could scrape your address off a site like whatismyip.com, but there is no guarantee that the route chosen by your OS to reach that site will always or ever go through the same interface as the route to the server that needs the hashed address.
i want to get server actual name and ip address in jsp . when i do
request.getServerName()
it returns me localhost. But i dont want name as localhost but actual name of machine (like in my case it is IBV-0076). Is there a way i can get Ip address also of server which is processing the request?
When i do request.getLocalAddr() i get address as 0.0.0.0 but my server ip is 10.253.18.76
I would use NetworkInterfaces to obtain information about servers configured interfaces. See Oracle What Is a NetworkInterface?
This also depends on how you have accessed the server.
Please use the acutal server IP address, which is used for accessing the webpage and then check the results.
This is because if you use the loopback IP of the server, the interface will be bound to "localhost" host name.
This usually happens when you are testing the webapp on the same machine as the server.
For the IP-Address, you can find Methods like getLocalAdrr() for the Request-Object.
Also, you can check the URL for the Servername with getRequestURL().
If you always get 0.0.0.0, please check the Network-Configuration of your Server.
here is an sample with Servername and IP. It is the IP the server see on its endpoint (maybe different because of NAT) and the serverName the client used and is accessible from the client.
${pageContext.request.localAddr}
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.