home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / ListenThread.java < prev    next >
Text File  |  1998-05-08  |  3KB  |  89 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.apps.chess.server;
  21.  
  22. import java.net.*;
  23. import java.io.*;
  24. import java.util.Enumeration;
  25. import borland.samples.apps.chess.client.ServerMessage;
  26.  
  27. public class ListenThread extends Thread
  28. {
  29.   SendThread t;
  30.   DataInputStream is;
  31.   ServerMessage smsg;
  32.   Socket s;
  33.  
  34.   ListenThread(Socket s ,SendThread t) {
  35.     this.t = t;
  36.     this.s = s;
  37.   }
  38.  
  39.   public void run() {
  40.     try{
  41.       is = new DataInputStream(new BufferedInputStream(s.getInputStream()));
  42.       InputStreamReader isr  = new InputStreamReader(is);
  43.       BufferedReader br = new BufferedReader(isr);
  44.       String inputLine = new String();
  45.       int port;
  46.       String msgid = new String();
  47.       String message = new String();
  48.       int offset;
  49.       //int blank = 32;
  50.       System.out.println("ListenThread - we are listening to " + String.valueOf(t.portnum));
  51.       while ((inputLine = br.readLine()) != null && !inputLine.startsWith("Bye ")) {
  52.         offset = inputLine.indexOf(' ');
  53.            //System.out.println("LT-"  + String.valueOf(t.portnum) + " Received:" + inputLine);
  54.         if (offset  > 0) {
  55.           msgid = inputLine.substring(0,offset);
  56.           //System.out.println("LT smsg   msgid:" + msgid);
  57.           message = inputLine.substring(offset+1); //don't store the space
  58.           smsg = new ServerMessage(t.portnum,msgid,message);
  59.           System.out.println("ListenThread smsg " + String.valueOf(t.portnum)  +" msgid:" +
  60.                              smsg.msgid + "msg:" + smsg.msg);
  61.           t.addMsg(smsg);
  62.           //synchronized (t) {
  63.           //  t.msgque.addElement(smsg);
  64.           //  t.notify();
  65.           //}
  66.         }
  67.         else
  68.           System.out.println("ListenThread read weird line"  );
  69.       }
  70.       is.close();
  71.     }
  72.     catch (IOException e) {
  73.       System.out.println("ListenThread " + t.portnum + e  );
  74.       //e.printStackTrace();
  75.     }
  76.     catch (Exception e) {
  77.       System.out.println("ListenThread " + t.portnum + e  );
  78.       e.printStackTrace();
  79.     }
  80.     System.out.println("ListenThread " + t.portnum + "going byebye"  );
  81.     smsg = new ServerMessage(t.portnum,"dead","");
  82.     t.addMsg(smsg);
  83.     //synchronized (t) {
  84.     //  t.msgque.addElement(smsg);
  85.     //  t.notify();
  86.     //}
  87.   }
  88. }
  89.