FileDocCategorySizeDatePackage
ListenThread.javaAPI DocExample3191Sat Sep 12 03:01:00 BST 1998borland.samples.apps.chess.server

ListenThread

public class ListenThread extends Thread

Fields Summary
SendThread
t
DataInputStream
is
borland.samples.apps.chess.client.ServerMessage
smsg
Socket
s
Constructors Summary
ListenThread(Socket s, SendThread t)

    this.t = t;
    this.s = s;
  
Methods Summary
public voidrun()

    try{
      is = new DataInputStream(new BufferedInputStream(s.getInputStream()));
      InputStreamReader isr  = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);
      String inputLine = new String();
      int port;
      String msgid = new String();
      String message = new String();
      int offset;
      //int blank = 32;
      System.out.println("ListenThread - we are listening to " + String.valueOf(t.portnum));
      while ((inputLine = br.readLine()) != null && !inputLine.startsWith("Bye ")) {
        offset = inputLine.indexOf(' ");
       	//System.out.println("LT-"  + String.valueOf(t.portnum) + " Received:" + inputLine);
        if (offset  > 0) {
          msgid = inputLine.substring(0,offset);
          //System.out.println("LT smsg   msgid:" + msgid);
          message = inputLine.substring(offset+1); //don't store the space
          smsg = new ServerMessage(t.portnum,msgid,message);
          System.out.println("ListenThread smsg " + String.valueOf(t.portnum)  +" msgid:" +
                             smsg.msgid + "msg:" + smsg.msg);
          t.addMsg(smsg);
          //synchronized (t) {
          //  t.msgque.addElement(smsg);
          //  t.notify();
          //}
        }
        else
          System.out.println("ListenThread read weird line"  );
      }
      is.close();
    }
    catch (IOException e) {
      System.out.println("ListenThread " + t.portnum + e  );
      //e.printStackTrace();
    }
    catch (Exception e) {
      System.out.println("ListenThread " + t.portnum + e  );
      e.printStackTrace();
    }
    System.out.println("ListenThread " + t.portnum + "going byebye"  );
    smsg = new ServerMessage(t.portnum,"dead","");
    t.addMsg(smsg);
    //synchronized (t) {
    //  t.msgque.addElement(smsg);
    //  t.notify();
    //}