LJ Archive

Listing 2

// CB Radio Client
// 1996, Joe Novosel
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class CBclient extends Frame {
  int command ; //cb_on
  int channel=19;
  Socket s;

String hostName = new String();
  String message;
  byte[] buffer = new byte[120];
  DataOutputStream dos;
  DataInputStream dis;
  TextField channelfield;
  TextField inputfield;
  TextArea outputarea;
  CBListener listener;
  final int CB_ON=1;
  final int CB_OFF=2;
  final int SET_CHAN=3;
  final int WHO_CHAN=4;
  final int WHO_ALL=5;
  final int SVR_STATS=6;
  final int SEND_MESSAGE=7;

public static void main(String args[]){
  if ((args.length <2)||(args.length >2)){
    System.out.println("Usage: client HANDLE HOSTNAME");
    System.exit(0);
  }
  //Here we create a new instance of the client object.
    CBclient n = new CBclient(args[0],args[1]);
}

Client code begins here
     CBclient(String handle, String h){
      super(handle);
    //print the handle name in the screen header
  try{
   hostName =h ;

    for(int i =0;i<120;i++) //zero out byte array
      buffer[i]=(byte)0;
    // Change 3507 to whatever port you wish,
    // but be sure that the server
    // is listening to that port.
    Socket s = new Socket(hostName,3507);
    // create the socket
    dis = new DataInputStream(s.getInputStream());
    dos = new DataOutputStream (new
BufferedOutputStream(s.getOutputStream()));

    //Menu stuff here
    MenuBar mb = new MenuBar();
    // create the menu bar itself
    Menu mc = new Menu("Connect");
    //create a menu item "connect"
    mc.add(new MenuItem("Quit"));
    //add an item "quit" to the menu "connect"
    mc.add(new MenuItem("Who"));
    //add an item "who" to the menu "connect"
    mb.add(mc);
    //add the menu "connect" to the menu bar
    setMenuBar(mb);
    //Set up the window
    channelfield = new TextField();
    // create a TextField for channel input
    inputfield = new TextField();
    // create a TextField for user input
    outputarea=new TextArea();
    //create a TextArea for output
    outputarea.setEditable(false);
    //We don't want the user to edit output
    this.setLayout(new BorderLayout());
    //We want to use a border layout
    this.add("North", inputfield);
    // put the inputfield at the top
    this.add("Center",outputarea);
    // put the output in the center
    this.add("South",channelfield);
   // put the channel field at the bottom

Launch a CBListener thread to listen for data from server
    listener = new CBListener (dis,outputarea);

//Size and show the window
    this.resize(400,200);
    show();

//Get a packet ready to send
//We are telling the server who we are
//and that we wish to connect
//Put the handle into the byte array
//to send to server
    handle.getBytes(0,handle.length(),buffer,0);
    dos.writeInt(19);> //channel
    dos.writeInt(1);  //cb_on
    for(int i =0;i<120;i++)
   //write the byte buffer with handle and message
      dos.writeByte(buffer[i]);
    dos.flush();
   //flush the write buffer
  }catch( IOException e) {
  }
  }

// Here's were we go when the user
// interacts with the program
  public boolean action(Event e,Object what){
    try{
//If the user types something in the
//inputfield (message field)
//Get it and send to the server
      if(e.target==inputfield) {
   message = new String((String)e.arg);
   message.getBytes(0,message.length(),buffer,20);
   dos.writeInt(channel);
  //request a channel change
   dos.writeInt(SEND_MESSAGE);
  //send message
   for(int i=0;i<120;i++)
     dos.writeByte(buffer[i]);
   dos.flush();
   for (int i=19;i<120;i++)
     buffer[i]=0;
   inputfield.setText("");
}
// If the user enters something in the channelfield
// request a channel change to the server
      if(e.target==channelfield){
   dos.writeInt(Integer.parseInt(new String((String)e.arg)));
   channel = Integer.parseInt(new String((String)e.arg));
   dos.writeInt(3);
  //set channel
   for(int i=0;i<120;i++)
     dos.writeByte(buffer[i]);
   dos.flush();
   inputfield.setText("");
      }
// If the user selects an item from the menu
// Process that item
   if(e.target instanceof MenuItem){
   String label = new String((String)e.arg);
   // The user wants to quit, send a CB_OFF
        // command to the server
   // and shut things down here
   if(label.equals("Quit")){
     dos.writeInt(channel);
     dos.writeInt(CB_OFF); //cb off
       for(int i=0;i<120;i++)
         dos.writeByte(buffer[i]);
   listener.stop();
        //Stop the listener thread
     dos.flush();
        //Flush the buffer to make sure
        //server has everything
          System.exit(0);  //Bye Bye now!

}
   // Process the who command
   // This will return a list (from the server)
        // of all clients on our channel
   if(label.equals("Who")){
   dos.writeInt(channel);
   dos.writeInt(WHO_CHAN);
        //who_chann  who's on this channel
   for(int i=0;i<120;i++)
     dos.writeByte(buffer[i]);
   dos.flush();}
}
      return true;}
      // return that we've handled the event
      catch(IOException ve) {
   System.out.println("error io exception");}

      return true;
    }

}
LJ Archive