1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.nntp;
17
18 import java.io.IOException;
19 import org.apache.commons.net.nntp.NNTPClient;
20 import org.apache.commons.net.nntp.NewsgroupInfo;
21
22 /***
23 * This is a trivial example using the NNTP package to approximate the
24 * Unix newsgroups command. It merely connects to the specified news
25 * server and issues fetches the list of newsgroups stored by the server.
26 * On servers that store a lot of newsgroups, this command can take a very
27 * long time (listing upwards of 30,000 groups).
28 * <p>
29 ***/
30
31 public final class newsgroups
32 {
33
34 public final static void main(String[] args)
35 {
36 NNTPClient client;
37 NewsgroupInfo[] list;
38
39 if (args.length < 1)
40 {
41 System.err.println("Usage: newsgroups newsserver");
42 System.exit(1);
43 }
44
45 client = new NNTPClient();
46
47 try
48 {
49 client.connect(args[0]);
50
51 list = client.listNewsgroups();
52
53 if (list != null)
54 {
55 for (int i = 0; i < list.length; i++)
56 System.out.println(list[i].getNewsgroup());
57 }
58 else
59 {
60 System.err.println("LIST command failed.");
61 System.err.println("Server reply: " + client.getReplyString());
62 }
63 }
64 catch (IOException e)
65 {
66 e.printStackTrace();
67 }
68 finally
69 {
70 try
71 {
72 if (client.isConnected())
73 client.disconnect();
74 }
75 catch (IOException e)
76 {
77 System.err.println("Error disconnecting from server.");
78 e.printStackTrace();
79 System.exit(1);
80 }
81 }
82
83 }
84
85 }
86
87