import java.io.*; import java.net.*; import java.util.*; class NewsGroup implements Comparable { String name; int first; int last; public NewsGroup (String n, int f, int l) { name = n; first = f; last = l; } public String toString() { return name + ": " + first + "-" + last; } public int compareTo(Object other) { return name.compareTo(((NewsGroup)other).name); } } public class NNTPClient { public static void main(String[] args) { String host = "news.oswego.edu"; int nntpServicePortNumber = 119; Socket nntpSocket = null; PrintWriter out = null; BufferedReader in = null; try { nntpSocket = new Socket(host, nntpServicePortNumber); out = new PrintWriter(nntpSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( nntpSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host " + host); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection."); System.exit(1); } try { String response200 = in.readLine(); if (!response200.startsWith("200")) { System.err.println("Protocol Error"); System.exit(1); } out.println("LIST"); String response215 = in.readLine(); if (!response215.startsWith("215")) { System.err.println("Protocol Error"); System.exit(1); } for (;;) { String line = in.readLine(); if (line.startsWith(".")) break; StringTokenizer toks = new StringTokenizer(line); String name = toks.nextToken(); int first = 0; String firstAsString = toks.nextToken(); try { first = Integer.parseInt(firstAsString); } catch (NumberFormatException ex) { System.err.println("Bad number format? " + firstAsString); System.exit(1); } int last = 0; String lastAsString = toks.nextToken(); try { last = Integer.parseInt(lastAsString); } catch (NumberFormatException ex) { System.err.println("Bad number format? " + lastAsString); System.exit(1); } NewsGroup n = new NewsGroup(name, first, last); System.out.println(n); } out.close(); in.close(); nntpSocket.close(); } catch (IOException ex) { System.err.println("IO failure."); ex.printStackTrace(); } } }