#/* author: Stan McCaslin (mccaslin@scis.acast.nova.edu) */ /* date: 16-Jul-1996 */ /* A simple java chat server. Modeled after the echo server in Core Java by Cornell and Horstmann Sunsoft Press, 1996 p 561 */ import java.io.*; import java.net.*; class Chat { public static void main(String[] args) { int i = 1; int portval; LinkedList list = new LinkedList(); try { if (args.length == 0) portval = 6001; else { Integer port = new Integer(args[0]); portval = port.intValue(); } ServerSocket s = new ServerSocket(portval); for (;;) { Socket incoming = s.accept(); System.out.println("Spawning " + i); new ThreadedChatHandler(incoming, i, list).start(); i++; } } catch (Exception e) { System.out.println(e); } } } class ThreadedChatHandler extends Thread { Socket incoming; int counter; LinkedList list; ThreadedChatHandler(Socket i, int c, LinkedList a) {incoming = i; counter=c; list = a;} public void run() { try {DataInputStream in = new DataInputStream(incoming.getInputStream()); PrintStream out = new PrintStream(incoming.getOutputStream()); list.append(out); out.println("Welcome! Enter BYE to exit.\r"); out.println("Please enter a short id for this session.\r"); String id = in.readLine(); printall("Just signed on\r", id, out); boolean done = false; while (!done) { String str = in.readLine(); if (str == null) done = true; else { printall(str + "\r", id, out); if (str.trim().equals("BYE")) done = true; } } list.reset(); while (list.hasMoreElements()) { if (out == (PrintStream)list.currentElement()) { list.remove(); break; } } in.close(); out.close(); incoming.close(); } catch(Exception e) {System.out.println(e); } } public synchronized void printall(String str, String id, PrintStream noecho) { PrintStream o; list.reset(); while (list.hasMoreElements()) { o = (PrintStream)list.currentElement(); list.nextElement(); /* if (o != noecho) */ o.println(id + ": " + str); } } } /* * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) * Published By SunSoft Press/Prentice-Hall * Copyright (C) 1996 Sun Microsystems Inc. * All Rights Reserved. ISBN 0-13-565755-5 * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ /** * A simple implementation of a linked list * @version 1.01 15 Feb 1996 * @author Cay Horstmann */ /* public class LinkedList */ class LinkedList { /** * resets the cursor */ public void reset() { pre = null; } /** * @return true iff the cursor is not at the end of the * list */ public boolean hasMoreElements() { return cursor() != null; } /** * move the cursor to the next position * @return the current element (before advancing the * position) * @exception java.util.NoSuchElementException if already at the * end of the list */ public Object nextElement() { if (pre == null) pre = head; else pre = pre.next; if (pre == null) throw new java.util.NoSuchElementException(); return pre.data; } /** * @return the current element under the cursor * @exception java.util.NoSuchElementException if already at the * end of the list */ public Object currentElement() { Link cur = cursor(); if (cur == null) throw new java.util.NoSuchElementException(); return cur.data; } /** * insert after the tail of the list * @param n - the value to insert */ public void append(Object n) { Link p = new Link(n, null); if (head == null) head = tail = p; else { tail.next = p; tail = p; } len++; } /** * remove the element under the cursor * @return the removed element * @exception java.util.NoSuchElementException if already at the * end of the list */ public Object remove() { Link cur = cursor(); if (cur == null) throw new java.util.NoSuchElementException(); if (tail == cur) tail = pre; if (pre != null) pre.next = cur.next; else head = cur.next; len--; return cur.data; } private Link cursor() { if (pre == null) return head; else return pre.next; } private Link head; private Link tail; private Link pre; // predecessor of cursor private int len; } class Link { Object data; Link next; Link(Object d, Link n) { data = d; next = n; } }