aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: About server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: About server" Watch "NX: About server" New topic
Author

NX: About server

Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi, everyone,
How can a program get the name or ip of the computer on which the program itself is running? What should I do when I showdown the server?
Recards
Davidd
Bharat Ruparel
Ranch Hand

Joined: Jul 30, 2003
Posts: 493
Hello Davidd,
I am not sure if you know that the computer hosting both the client and the server is universally identified (at least in the windows world) as "localhost". The ip address assigned to the localhost name is always 127.0.0.1.
Is that what you are after?
Regards.
Bharat


SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi,
Bharat,
Thank you for your answer.
I would like to know the ip or name of the computer on the net when it act as a server so that the server code can bund itself and a client can find it. Again, what should I do when I showdown the server?
Recards
Davidd
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Hi Davidd & Bharat,
the server is universally identified (at least in the windows world) as "localhost"

I think this is an IP standard, not specific to Microsoft. It is certainly valid on all platforms I have worked on.
The ip address assigned to the localhost name is always 127.0.0.1

This is defined by RFC 1020, (and, I believe, endorsed by Internet Assigned Numbers Authority (IANA)) so it is an internet standard. Technically any 127.x.x.x address should loopback to the localhost.
I would like to know the ip or name of the computer on the net when it act as a server so that the server code can bund itself and a client can find it.

All of the above is a long way of agreeing with Bharat that you can use the 127.0.0.1 address (or "localhost") as the address that your server will use - the Java RMI Registry will accept all incoming calls, not just those on the 127.0.0.1 address. (Interestingly, I cannot see any way of forcing the RMI Registry to only listen on a specific address. If this ever became an issue I guess I would have to block access to the registry at another level - possibly the firewall ).
If you really wanted to, I guess you could use InetAddress.getLocalHost(), but I don't see any need to do this - just use the standard localhost address.
I would like to know the ip or name of the computer on the net when it act as a server so that the server code can bund itself and a client can find it.

The only way (that I will recommend) for the client to find the server is by the user entering the name or IP address of the server. When you are running your application you should know what your server's name/address is. When the examiner runs the client application, the examiner should know what the server's name/address is.
What should I do when I showdown the server?

Personally I think you should stop new clients from acquiring locks, wait for existing locks to be released (possibly with a timeout), then close the file (if you have it open) and exit.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi, Andrew,
I use a ConnectionFactory bound to rmiregistry. Should I unbind it before exit? There are many Connections with their instance of Data class as a member, how could I deal with all of them? Could I ignore them and let the finalize() method of the Data class work?
I lock and unlock the records on the server in the booking() method of ConnectionImpl class. In my instructions, there is no request about database lock. I am afraid it will cause the code more complex and increase the possibility of losing score if I add the database lock, because I found there are few person get high score in lock/unlock.
Could you give me any suggestion?
Regards
Davidd
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Originally posted by Davidd Smith:
I use a ConnectionFactory bound to rmiregistry. Should I unbind it before exit?

That would be a very good idea.
Originally posted by Davidd Smith:
There are many Connections with their instance of Data class as a member, how could I deal with all of them? Could I ignore them and let the finalize() method of the Data class work?

Are you starting the registry from within your application? If so, have you thought about how you are going to exit from your application since you wont be able to shut it down cleanly? And if you force an exit, will your finalize methods run?
Originally posted by Davidd Smith:
I lock and unlock the records on the server in the booking() method of ConnectionImpl class. In my instructions, there is no request about database lock. I am afraid it will cause the code more complex and increase the possibility of losing score if I add the database lock, because I found there are few person get high score in lock/unlock.
Could you give me any suggestion?

OK, I agree that locking the database may add unwanted complexity. But you probably still want to stop clients from modifying the database while you are shutting it down. Perhaps a flag somewhere that the booking() method can check to see if it is allowed to proceed with an existing booking?
Regards, Andrew
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi, Andrew,
Thank you.
I am not sure when client mode (no command line arguments) starts and user type in "localhost" while selecting host, should the stand alone mode run or loop back net client run?
Regards
Davidd
Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435
Hi Andrew,
Why do you need to unbind before you close down the registry with exit ?
Won't it be unbound on exit ?
Or are registry values persistent across invocations of the JVM ?
I agree it's probably good practice, but is it necessary.
BTW exit seems to be the only way of terminating the registry.

Tony
Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435
Originally posted by Davidd Smith:
Hi, everyone,
What should I do when I shutdown the server?
Recards
Davidd

I thought maybe you could unbind the factory, pause(or wait for all locks to become free) then exit.
This would disallow any further connections, wait for current remote invocations to end and then the exit would shut down the registry.
I tested unbind this morning, just because the factory is unbound doesn't mean you can't access remote objects you hold the reference to.
It just means no new connections canbe made.
Though for this design to work a new connection to the server would be required every time a read/update is required.
Tony
[ September 05, 2003: Message edited by: Tony Collins ]
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi, Andrew,
I start the registry from within my application, but I did not think clearly about how to exit. I think when net is down, the Connections and their member object Data will be garbage collected automatically. I do not know whether it is right and what should I do.
Would you like to give me some suggestion?
Regards
Davidd
Davidd Smith
Ranch Hand

Joined: Jul 28, 2003
Posts: 62
Hi,
Tony,
Thank you for your answer.
I have also tested unbind, it works just as you said.
Regards
Davidd
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Hi Davidd & Tony,
[Davidd] I am not sure when client mode (no command line arguments) starts and user type in "localhost" while selecting host, should the stand alone mode run or loop back net client run?

'localhost' is an alias for loop back to local host.
So if the user enters 'localhost' then you should use networking. You should not care that it just happens to be loop back networking.
[Tony] Why do you need to unbind before you close down the registry with exit ?
Won't it be unbound on exit ?

You are correct: after exit it will no longer be bound (not quite the same as saying it will be unbound but the result is the same).
But if you explicitly unbind, then if you later start using a registry that is not started internally, then you already have the code in place to do the unbind on shutdown. You do not have to worry about whether the programmer making that change will notice it or not.
Extreme programming would tell us that we don't need to do this, but I think it is such a simple little thing and is more technically correct.
[Tony] BTW exit seems to be the only way of terminating the registry.

Correct. I have commented on that in this thread.
[Davidd]What should I do when I shutdown the server?
[Tony]I thought maybe you could unbind the factory, pause(or wait for all locks to become free) then exit.

I also set a flag so that no new locks could be granted.
My implementation waited until all existing locks were released, or 5 minutes had elapsed, whichever came first. Then I exited.
[Davidd] I think when net is down, the Connections and their member object Data will be garbage collected automatically.

Correct. Some time after the java.rmi.dgc.leaseValue expires (default is supposed to be 10 minutes) the dgc (distributed garbage collector) will run, and will remove all connections that have not sent a dirty() call in the lease timeframe. If you have implemented Unreferenced then this is the time that your unreferenced() method will be called. Or if you have used WeakReferences (for example in a WeakHashMap) then this is the time that these weak references will be removed. And so on.
Regards, Andrew
Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435
Cheers Andy,
5 minutes seems a long time to wait before shuting down ?
I was thiking more of 5 seconds.
I also begining to think just exiting is OK, just makes things simpler.
It would be very hard to leave the Server in a dodgy state.
How did you do with your Server design ?
Tony
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Hi Tony,
Well I think you saw J.S.'s comment where he got 100% for server design just doing a Control-C. Sounds like the way to go.
I lost 4 marks for my server but I don't know what part of it (of course).
Regards, Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: About server