20-CS-694 Advanced Programming Techniques Spring 2012
Networking

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous     Next     RVClient.java     RVServer.java     Serializable class      All lectures

Teleportation

The client side of the teleportation pair. See notes on the previous slide
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

class Application extends PipedWriter implements Runnable, Serializable {
   PipedReader pipe;
   Thread runner;
   
   public void start () {
      runner = new Thread(this);
      runner.start();
   }
   
   public void connect (PipedReader pipe) throws java.io.IOException {
      super.connect(pipe);
   }
   
   public void run () {
      try {
         for (int i=0 ; i < 100 ; i++) {
            write(String.valueOf(i)+"\n");
         }
      }
      catch (IOException e) {
         System.out.println("Cannot open piped connection to agent");
      }
   }
}

class RVClients implements Runnable {
   Thread runner = null;
   Socket socket = null;
   BufferedReader in = null;
   ObjectOutputStream out = null;
   RVClient rv;
   String name;
   
   public RVClients(RVClient ca, String nm) { rv = ca; name = nm; }
   
   public void disconnect () {
      try {
         socket.close();
      }
      catch (Exception e) { rv.status.setText(String.valueOf(e));  }
      rv.status.setText("Connection Closed");
      rv.client = null;
   }
   
   public void connectit () {
      try {
         socket = new Socket("localhost", 8789);
         InputStream ins = socket.getInputStream();
         in  = new BufferedReader(new InputStreamReader(ins));
         out = new ObjectOutputStream(socket.getOutputStream());
         rv.status.setText("Ready to send");
         runner = new Thread(this);
         runner.start();
      }
      catch (Exception e) {
         rv.status.setText("Connection Refused - Server Running?");
         rv.client = null;
      }
   }
    
   public void transmitit() {
      try {
         out.writeObject(new Application());
         rv.status.setText("Sending to server");
      }
      catch (Exception e) {
         rv.status.setText("Cannot Send to Server - Server Down?");
         rv.client = null;
      }
   }
    
   public void run () {
      try {
         while (true) {
            String str = in.readLine();
            rv.takein.append(str+"\n");
            if (str.equals("") || str == null) break;
         }
      }
      catch (Exception e) { }
      disconnect();
   }
}

public class RVClient extends Applet implements ActionListener {
   JButton sendit, leave, openit;
   JTextField status;
   JTextArea takein;
   RVClients client = null;
   
   public void init () {
      setLayout(new BorderLayout());
      Panel p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new JLabel("Status"));
      p.add("Center", status = new JTextField("No connection yet"));
      add("North", p);
      
      p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new JLabel("Received From Server"));
      p.add("Center", new JScrollPane(takein = new JTextArea(10,30)));
      add("Center", p);

      p = new Panel();
      p.setLayout(new GridLayout(1,3,10,10));
      p.add(sendit = new JButton("Send It"));
      p.add(openit = new JButton("Connect"));
      p.add(leave = new JButton("Close"));
      add("South", p);
   }

   public void start () {
      status.setEditable(false);
      status.setBackground(Color.white);
      takein.setEditable(false);
      takein.setBackground(Color.white);
      sendit.addActionListener(this);
      openit.addActionListener(this);
      leave.addActionListener(this);
   }
   
   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == sendit && client != null)
         client.transmitit();
      else
      if (evt.getSource() == openit && client == null) {
         client = new RVClients(this, "object_client");
         client.connectit();
      }
      else
      if (evt.getSource() == leave && client != null) {
         client.disconnect();
         client = null;
      }
   }
}