LJ Archive

Listing 1

// Joe Novosel
// StreamListener
// This is a class that is launched as a thread to
// listen to the server for
// Incoming data
// This is roughly taken from a similar program
// in 'Java in a Nutshell'
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class CBListener extends Thread{
  DataInputStream dis;
  TextArea output;
  int channel;
  int command;
  byte[] buffer = new byte[120];
  String line;

public CBListener(DataInputStream dis, TextArea output){
    this.dis = dis;
    this.output = output;
    this.start();  //launch the thread
  }

public void run(){
    String handle;
    String message;
    try{
      for(;;){
   //let's do this forever
        (or at least until we're killed)
   channel =dis.readInt();
       //read a channel command=dis.readInt();
       //read a command (we won't use it here)
         for(int i=0;i<120;i++)
       //read the handle and message byte array
         buffer[i]=dis.readByte();
    System.out.print("(");
    handle =new String(buffer,0,0,20);
       //print the handle
    message = new String(buffer,0,20,100);
    output.appendText("("+handle);
       //print it to the screen
    output.appendText(") "+message);
    output.appendText("\n");
      }
    }catch (IOException e)output.setText(e.toString());
    finally output.setText("Connection closed by server.");
  }
}
LJ Archive