|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Product Reviews News Today, Gone Tomorrow
News Today, Gone Tomorrow
By: Alan Williamson
Jan. 1, 1998 12:00 AM
From the very first steps this column took, a journey of discovery was promised which, hopefully, has not disappointed. Using Visual Cafe as our development platform, we have ventured into areas of Java that others have feared to tread. From building complete applets with database connectivity to developing classes for sending and receiving e-mail, this column has so far tackled a wide variety of different problems. On the face of it, many of the topics appear difficult or far too complicated to have in an ordinary applet, application or servlet. But I think you'll agree that once the initial wall has been scaled, the rest of the problem becomes somewhat academic. My previous two columns looked at interfacing to two of the more popular TCP services; POP and SMTP. This month, we will round off the TCP services with an implementation for a generic class to interface to the Usenet newsgroup network.
Usenet Connecting to a Usenet server is a simple matter of opening up a socket connection to the server which usually sits and listens for client connections on port 119. The procedure for performing this can be seen in the complete listing of the class. Like the POP and SMTP servers, communication with the Usenet server is through command strings passed as carriage-returned lines. The available command set to communicate with the server is surprisingly small, with only a handful of commands required to get the most out of a server. But before we go into the commands, let's take a quick look at how articles are arranged on the server.
Articles
Associated with each article is a header. This header, shown in Listing 1, describes the article, which includes the e-mail of the poster, the subject of the article, the newsgroup the article has been posted in and the date it was posted (to name some of the more useful fields).
Retrieving the article from the server is a simple matter of issuing the commands
HEAD
Listing 2 shows the method, getArticle(...) which downloads the article from the server. First, it issues the HEAD command which asks the server to download the header of the given article. As each line is received, it is parsed so we may extract the e-mail address of the poster, the date it was posted and the subject of the article. If for some reason, the article is no longer available, then the server will return an error message line which contains a status code at the start of the line. If this code is either 423 or 430, we can assume the article is no longer available. If this is the case, the body is not downloaded and the method returns a null.
The body operates on somewhat the same principle, copying the lines of the article into a Vector for later use. Note that in this version we are actively filtering out all binary attachments as this can make the article body very large indeed. You may have noticed that the method returns a class of type cArticle. This is a simple wrapper class that represents a single Usenet article, with the body held in a Vector class.
Posting Articles
Listing 3 shows the implementation of the postArticle(...) method. This method takes in the newsgroup the article is to be posted to, the e-mail of the poster, the subject and finally the actual body of the article formatted in a Vector, where each element is a single line.
The most important part of posting to the server is correctly formatting the header. If any of the syntax is wrong, then the article will be rejected. One of the features of the Usenet system is the ability to cross-post. This is where the same article is posted to multiple newsgroups at the same time. This is particularly handy as it saves on both bandwidth and time. To do this, simply list the newsgroups as a comma separated list. The server will then do the rest without any further intervention from the client.
Article Listings
Fortunately, this is a fairly easy operation to perform. The server can be asked to return all the message ids for a particular newsgroup, since a certain date. For example, to request all the messages since the 9th of December 1997, midnight, for the newsgroup comp.lang.java.programmer, the following command would be issued
NEWNEWS comp.lang.java.programmer 971209 000000
The server will then return a list of message ids, each in their own line, with the list terminated using a period (.). This message id can then be used in the method described earlier to retrieve that article. Listing 4 demonstrates the communication with the server to determine the list of message ids.
The method returns a Vector of message ids back to the caller, or null if an error occurred. Again, as with the posting of articles, the formatting of the date is most important; otherwise, the server will recheck the command.
Summary
Communication with the more popular TCP servers is performed using simple ASCII commands, which allow for very straightforward clients to be implemented. This is one of the real powers the Internet has to offer.
In the next column we will go further down the road of Java enlightenment and tackle another 'looks-far-too-complicated' topic area. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||