Home arrow static arrow Java Programming [Archive] - Connecting To Newsgroups Server
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Connecting To Newsgroups Server
This topic has 10 replies on 1 page.

Posts:55
Registered: 10/19/98
Connecting To Newsgroups Server  
Aug 9, 2004 12:12 PM



 
As part of learning about Sockets and connecting, I'm trying to write an application that connects to a newsgroup server and downloads the list of newsgroups. Here's the part of the code that does the connecting and downloading.

System.out.println("Processing " + hosts.elementAt(i).toString() + " ("+(i+1)+"/"hosts.size()")");
java.net.Socket sock = new java.net.Socket(hosts.elementAt(i).toString(),119);
if (sock.isConnected()) {
System.out.println("Connect OK.");
}
else {
System.out.println("Connect Failed");
}
dis = new java.io.DataInputStream(sock.getInputStream());
String server = dis.readLine();
System.out.println(server);
dos = new java.io.DataOutputStream(sock.getOutputStream());
String send = "LIST\n\n";
dos.writeBytes(send);
dos.flush();
System.out.println("Waiting For Response");
String in = dis.readLine(); //Deprecated
System.out.println(in);
in = dis.readLine();
while (!in.equals(".")) {
//Format -- name [space] lastmsgid [space] firstmsgid [space] postingallowedyn
logger.debug(in);
//System.out.println(in);
ng group = new ng(in, hosts.elementAt(i).toString());
group.check();
in = dis.readLine();
ngs.addElement(group);

My question is that on some servers, when I connect using this, the application appears to hang when reading the response to the LIST command. I know it can't be to do with the program, because the same program will happily connect to another newsgroup server. Annoyingly, using Windows' own Telnet application, I can do the LIST command with no problems. Does anyone know why this might be the case? Could it have something to do with the fact that I'm using the DataInputStream's readLine() method, which is deprecated (if so, can someone properly advise me which is the correct form for this.)

Many thanks for any help.

Johnny
 

Posts:18,384
Registered: 21.03.00
Re: Connecting To Newsgroups Server  
Aug 9, 2004 12:16 PM (reply 1 of 10)



 
I know it
can't be to do with the program, because the same
program will happily connect to another newsgroup
server. Annoyingly, using Windows' own Telnet
application, I can do the LIST command with no
problems.

That all indicates that the error is within your program. Have you tried to run it in a debugger?

/Kaj
 

Posts:55
Registered: 10/19/98
Re: Connecting To Newsgroups Server  
Aug 9, 2004 12:18 PM (reply 2 of 10)



 
Yes, I stepped through the entire application, and it stayed on the readLine() bit and stayed like that for ages. I even left it while I had a bath! :-)
 

Posts:18,384
Registered: 21.03.00
Re: Connecting To Newsgroups Server  
Aug 9, 2004 12:23 PM (reply 3 of 10)



 
Hi,

First of all replace the line:
 dis = new java.io.DataInputStream(sock.getInputStream());


With a BufferedReader, see the documentaion for DataInputStream. That might be your problem, but it might also be that the server doesn't send a new line character, which readLine requires.

/Kaj
 

Posts:55
Registered: 10/19/98
Re: Connecting To Newsgroups Server  
Aug 9, 2004 12:24 PM (reply 4 of 10)



 
I'm doing that right now... :-P

I'll let you know how it goes.
 

Posts:5,904
Registered: 04/03/99
Re: Connecting To Newsgroups Server  
Aug 9, 2004 12:37 PM (reply 5 of 10)



 
I even left it while I had a bath!

I think we have a natural programmer here...

Dave. Who is about to have a bath while deleting his spam backlog...
 

Posts:55
Registered: 10/19/98
Re: Connecting To Newsgroups Server  
Aug 9, 2004 12:39 PM (reply 6 of 10)



 
I think we have a natural programmer here...

Dave. Who is about to have a bath while deleting his
spam backlog...

Errr... I don't have a spam backlog -- my mailserver on my PC does that for me as the e-mails come in, but I am going to have a bath now, though. The traits of a "Natural Programmer", right? :-D
 

Posts:5,904
Registered: 04/03/99
Re: Connecting To Newsgroups Server  
Aug 9, 2004 1:10 PM (reply 7 of 10)



 
I'm using Spamcop to filter my incoming mail. Most spam gets filtered out before I have to look at it, and is filed into a Held Mail folder.

This is now filling at a rate of 9,000 spam messages per day. That takes a substantial amount of time to delete using most IMAP clients. So I whipped up a little script (which follows) to delete them. I run it from a webserver in a data centre to save the hassle of having the PC in the bedroom running while I sleep (and hence so that my girlfriend doesn't throttle me).
// Get a Properties objectSystem.out.println("Get the properties");Properties props = System.getProperties(); // Get a Session objectSystem.out.println("Get the session (debug on)");		Session session = Session.getInstance(props, null);session.setDebug(false);		 // Get a Store objectSystem.out.println("Get the (imap) store object");		Store store = session.getStore("imap"); // ConnectSystem.out.println("Connect to the IMAP store");store.connect("mail.spamcop.net", "myusername@example.com", "mypassword"); // List namespaceSystem.out.println("Reference the folder");Folder rf = store.getFolder("INBOX.Held Mail");		System.out.println("Open the folder");rf.open(Folder.READ_WRITE);		 // Retrieve all the (lightweight) messages in the folderSystem.out.println("Get the message info");		Message[] msgs = rf.getMessages();		 System.out.println("You have " + rf.getMessageCount() + " held messages"); System.out.println("Setting deleted flag on ALL messages");rf.setFlags(1,rf.getMessageCount(),new Flags(Flags.Flag.DELETED),true);		System.out.println("Close the folder, expungeing the deleted messages");rf.close(true);		// Close the store.System.out.println("Close the store");store.close();		System.out.println("Check your mail !");

Most of that spam's caused by my wildcard email address, which accepts any old junk. I'm going to lock that down to the ones I actually use, but first I have to work out what those are - hence this rather klunky stopgap.

Anyway, the point of this digression (other than to moan about spam) was to briefly demonstrate the JavaMail API, which might be of interest to you, since it would be entirely possible to use an existing JavaMail compatible NNTP client, or (more interesting I suspect) adapt your NNTP so that it can be used from the JavaMail API.

That and to extol the virtues of clean living.

Dave
 

Posts:55
Registered: 10/19/98
Re: Connecting To Newsgroups Server  
Aug 9, 2004 1:18 PM (reply 8 of 10)



 
Haven't got a girlfriend yet -- think it's too much commitment for a programmer, IMHO. :-D
 

Posts:5,904
Registered: 04/03/99
Re: Connecting To Newsgroups Server  
Aug 9, 2004 1:22 PM (reply 9 of 10)



 
Then why are you taking baths ? ;-)

D.
 

Posts:55
Registered: 10/19/98
Re: Connecting To Newsgroups Server  
Aug 9, 2004 1:23 PM (reply 10 of 10)



 
Err. Because I'm hot and sweaty? Not very attractive to girls, right?
 
This topic has 10 replies on 1 page.