Article View: borland.public.jbuilder.java.api
Article #943Re: an idea to consider
From: John McGrath TeamB
Date: Mon, 08 Jul 2002 23:48
Date: Mon, 08 Jul 2002 23:48
81 lines
4608 bytes
4608 bytes
On Mon, 08 Jul 2002 17:04:14 -0400, Alan Shiers wrote:
> The sample application I sent on the previous post was
> obviously a GUI based application. So I guess I'll try
> making it a non-GUI based application and try once again.
I don't think the GUI nature of it would make a difference. You did
have the console window where you started it active when you pressed
Ctrl-C, right?
> My development computer happens to be Windows98.
Well, that could be the problem. I have found so many oddities and
problems with Windows 98 that I avoid it like the plague.
> I'm thinking that the only way I'm going to pull this one
> off is to simply encorportate another threaded class as part
> of the server app. The class would contain a ServerSocket
> object listening only to the loopback address.
>
> Then, when I want to shut down the server app, all I got to
> do is log in as "root" and execute another java program named
> Terminator. Terminator will connect to the server app and
> issue the command to shutdown. Both programs will then shut
> down.
I have used that approach several times and it has worked well. I have
generally added other commands that the program responds to on the
socket -- status reports and the like. It turns out to be very
convenient.
I have typically used a simple NNTP-line protocol -- line oriented,
where each command line contains the command-name followed by optional
arguments and single line response with a numeric status code at the
beginning, followed by readable text. Then you can connect to it using
telnet, which makes debugging much nicer. You could even eliminate the
special "terminator" program, but it is nice to have.
And somewhere down the road, you can disable the code that checks
whether an incoming connection comes from the local host and sell
it as a "remote administration" feature. :o)
> String address = "127.0.0.1";
> socket = new ServerSocket(port, 10,
> InetAddress.getByName(address));
I do not think the above code will restrict connections to the local
host. On a multi-homed system, the third argument specifies which
address of the interface on which it will accepts connections. I do
not think it will prevent someone from connecting from a remote host.
What you need to do is to call getInetAddress() on the Socket returned
by connect() and check whether it is actually the local host. I have
used the following code:
InetAddress addr = socket.getInetAddress();
isLocalClient = addr.getHostAddress().equals( "127.0.0.1" )
|| addr.equals( InetAddress.getLocalHost() );
I compare with both "127.0.0.0" and getLocalHost() because I have seen
variations in what is returned by getInetAddress.getHostAddress().
Sometimes it reports the loopback address and sometimes it reports the
actual IP. I think this can be the result of an incorrect network
configuration. I have not tested this with a multi-homed system.
One additional note: If you decide to use a line-oriented protocol,
the line terminators becomes important, since clients and servers may
have different terminators. You need to explicitly specify terminators
when communicating on the socket. I have used a CR-LF pair for the
terminator, since that makes it compatible with telnet.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<MPG.1793eed4e5e42ee798b80b@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!supernews.com!iad-peer.news.verio.net!news.verio.net!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3D279D25.59AA52E@hfx.eastlink.ca> <3d27a0b2_2@dnews> <3D2861BB.63220DF5@hfx.eastlink.ca> <MPG.17923b35889aef9498b7f9@newsgroups.borland.com> <3D28A22A.89CB73DC@hfx.eastlink.ca> <MPG.1793793e26a87ab98b7fd@newsgroups.borland.com> <3D29FE4E.C3EEF5B5@hfx.eastlink.ca>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,4157c064c52214d4 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-07-08 16:51:27 PST From: John McGrath [TeamB] <ng-b@JPMcGrath.net> Newsgroups: borland.public.jbuilder.java.api Subject: Re: an idea to consider Date: Mon, 8 Jul 2002 19:48:45 -0400 Message-ID: <MPG.1793eed4e5e42ee798b80b@newsgroups.borland.com> References: <3D279D25.59AA52E@hfx.eastlink.ca> <3d27a0b2_2@dnews> <3D2861BB.63220DF5@hfx.eastlink.ca> <MPG.17923b35889aef9498b7f9@newsgroups.borland.com> <3D28A22A.89CB73DC@hfx.eastlink.ca> <MPG.1793793e26a87ab98b7fd@newsgroups.borland.com> <3D29FE4E.C3EEF5B5@hfx.eastlink.ca> X-Newsreader: MicroPlanet Gravity v2.60 NNTP-Posting-Host: 209.92.40.60 X-Trace: dnews 1026172124 209.92.40.60 (8 Jul 2002 16:48:44 -0700) Lines: 81 X-Authenticated-User: jpmcgrath Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!supernews.com!iad-peer.news.verio.net!news.verio.net!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews