aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Connecting to server using its host name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Connecting to server using its host name" Watch "Connecting to server using its host name" New topic
Author

Connecting to server using its host name

abdy hussein
Greenhorn

Joined: Feb 16, 2012
Posts: 10
Hi all,

I have a created a TCPClient class to handle the socket programming on the client side. The server is a device that I intend to send messages to execute various commands. Under this class I have constructed a client socket with the server's host name and port no. Running the program throws UnknownHostException. I have verified I am using the correct device name. i knew this after I checked on my router's configuration page where a list of connected devices were displayed.

But when I use an InetAddress object containing the server's IP address, I get no errors and the connection is successful. This however brings a huge limitation in that, I have to enter the IP address every time since the device's (server) DHCP is enabled. What could be the problem?

Here is the code.



Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sounds like there's nothing present to resolve that name, which is not a Java issue. The fact that the name shows up in your router's config table doesn't necessarily mean anything. That may just be the name that that host is reporting to the router, but that doesn't mean the router will act as a resolver for that name.

From a command line if you do ping hostname or telnet hostname, what result to do you get? If you get something like "unknown host", then it's definitely not a Java issue. If this is just a host on your LAN and its IP address won't change, you can add an entry for it in your hosts file. On Linux it's /etc/hosts and on Windows it's something like C:/Windows/System32/drivers/etc/hosts.
abdy hussein
Greenhorn

Joined: Feb 16, 2012
Posts: 10
I think its definitely not a java issue becoz I am getting this erro; "Ping request could not find host Wifly GSX. Please check the name and try again."

Thanks.
abdy hussein
Greenhorn

Joined: Feb 16, 2012
Posts: 10
Since I know the host Ip address is there a way I can use it to get its hostname?. I know am going out of topic but I would appreciate any help on this.

Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

abdy hussein wrote:Since I know the host Ip address is there a way I can use it to get its hostname?.


That doesn't really make any sense.

If you know the IP address, you can just use that to connect.

If you want to use the host name instead of the IP (a better approach, when feasible), then either have your network admins put an entry in whatever naming service you're using, or else add an entry to your /etc/hosts or C:/Windows/System32/drivers/etc/hosts. (If you do the latter, then you'll of course have to update it each time the IP address changes.)

Note also that the hostname that maps to an IP address doesn't have to be the same as what the computer thinks its own name is (i.e., what you get as a result of the hostname command on that computer).


Ian James
Greenhorn

Joined: Oct 25, 2012
Posts: 2
Hi,

I was wondering whether you have managed to find a solution as I have exactly the same problem.

I am writing an Android app to link to a WiFly module.

The problem is that the ip address assigned to the WiFly by my router can change thus making it impossible to to know where to connect to at the Android end.

Thanks

Ian James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting to server using its host name