/** * ForumComm.java 1.00 1.15.97 Michael Shoffner * * Copyright (c) 1997 Prominence Dot Com, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * for non-commercial purposes and without fee is hereby granted * provided that this copyright notice appears in all copies. * * http://prominence.com/ shoffner@prominence.com */ import java.net.*; import java.util.*; import java.io.*; import ForumLauncher; public class ForumComm { // port for server to listen for Forum clients static final int FORUM_PORT = 5000; // possible client requests static final int LOAD_ALL_THREADS = 1; static final int LOAD_THREAD_ARTICLES = 2; static final int POST_ARTICLE = 3; ForumLauncher gp; public ForumComm (ForumLauncher gparent) { gp = gparent; } Hashtable loadAllThreads() { Hashtable a = new Hashtable(); String thread = ""; try { URL serverURL = gp.getCodeBase(); Socket server = new Socket (serverURL.getHost(), FORUM_PORT); InputStream in = new BufferedInputStream (server.getInputStream()); OutputStream out = new BufferedOutputStream (server.getOutputStream()); DataInputStream dIn = new DataInputStream (in); DataOutputStream dOut = new DataOutputStream (out); dOut.writeInt (LOAD_ALL_THREADS); dOut.flush(); thread = dIn.readUTF(); while (!thread.equals ("")) { a.put (thread, new Vector()); thread = dIn.readUTF(); } } catch (IOException ex) { System.out.println ("Error reading threads from server."); } return a; } Vector loadThreadArticles (String t) { Vector ta = new Vector (); String art = ""; try { URL serverURL = gp.getCodeBase(); Socket server = new Socket (serverURL.getHost(), FORUM_PORT); InputStream in = new BufferedInputStream (server.getInputStream()); OutputStream out = new BufferedOutputStream (server.getOutputStream()); DataInputStream dIn = new DataInputStream (in); DataOutputStream dOut = new DataOutputStream (out); dOut.writeInt (LOAD_THREAD_ARTICLES); dOut.writeUTF (t); dOut.flush(); art = dIn.readUTF(); while (!art.equals ("")) { ta.addElement (art); art = dIn.readUTF(); } } catch (IOException ex) { System.out.println ("Error reading articles for thread '" + t + "' from server."); } return ta; } boolean postArticle (String art, String t) { try { URL serverURL = gp.getCodeBase(); Socket server = new Socket (serverURL.getHost(), FORUM_PORT); InputStream in = new BufferedInputStream (server.getInputStream()); OutputStream out = new BufferedOutputStream (server.getOutputStream()); DataInputStream dIn = new DataInputStream (in); DataOutputStream dOut = new DataOutputStream (out); dOut.writeInt (POST_ARTICLE); dOut.writeUTF (t); dOut.writeUTF (art); dOut.flush(); return true; } catch (IOException ex) { System.out.println ("Error posting article in thread '" + t + "' to server."); return false; } } }