title: Send Multiple choice questionaire ( with possible answers) in Java/android
body: <p>Hi everyone I've searched a lot but couldn't find any answer that satisfies what I want to do for my project. Maybe I didn't know what to search for but he it is:
  project: constant in-class feedback</p>
  
  <p>I'm trying to write a program that enables a lecturer to send(from java app) a multiple choice questionnaire(a question with 4 possible answers) to students' mobile phones(android app) so they can touch an answer click "submit" then it gets sent back to the teacher.</p>
  
  <p>I just don't know how to do this. I did radio buttons but I got stuck even more because I have no idea how to send them(if it was possible, that is).
  Another thing.. what do you advise me to use for protocol that would best fit what I'm trying to do? I've used WI-Fi but I got this problem that every time I have to put the port to be used here and there...</p>
  
  <p>I'm ready to make any changes you suggest but please try to find a solution for me :)</p>
  
  <p>This is my code so far: (sorry if i messed up presenting this question this is the first time I do this)</p>
  
  <p>the Android package</p>
  
  <p>Welcome.java:</p>
  
  <pre><code>package mobileapp.com;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Handler;
  
  public class Welcome extends Activity {
  private Handler handler = new Handler();
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.welcome);
      handler.postDelayed(loadNextWindow, 3000);  
  }
  
  private Runnable loadNextWindow = new Runnable(){
      public void run(){
          Intent intent = new Intent(Welcome.this,Answer.class);
          startActivity(intent);
      }
  
  };
  
  }
  </code></pre>
  
  <p>Answer.java:</p>
  
  <pre><code>package mobileapp.com;
  
  import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.InetAddress;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.RadioButton;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class Answer extends Activity {
  private TextView qtext;
  private RadioButton rB1, rB2, rB3, rB4;
  private Button sendButton, connectButton;
  private Socket socket;
  private String serverIpAddress = "";
  private String answer;
  // AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO
     // PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S
     // PORT 6000
  private static final int REDIRECTED_SERVERPORT = 7076;
  
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle icicle) {
  
  
      answer = null;
      super.onCreate(icicle);
      setContentView(R.layout.answer);
  
      qtext = (TextView) findViewById(R.id.textQuestion1);
      rB1 = (RadioButton) findViewById(R.id.rButton1);
      rB2 = (RadioButton) findViewById(R.id.rButton2);
      rB3 = (RadioButton) findViewById(R.id.rButton3);
      rB4 = (RadioButton) findViewById(R.id.rButton4);
      sendButton = (Button) findViewById(R.id.button1);
      connectButton = (Button) findViewById(R.id.button2);
  
  
      connectButton.setOnClickListener(new OnClickListener(){
  
          public void onClick(View v2) {
              // TODO Auto-generated method stub
  
          try {
              BufferedReader  br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
              qtext.setText(br.toString());
  
          }
       catch (IOException e2) {
          // TODO Auto-generated catch block
          e2.printStackTrace();
       }
  
           }
  
      });
  
  
  
  
      sendButton.setOnClickListener(new OnClickListener(){
          public void onClick(View v) {
              // TODO Auto-generated method stub
  
              if (rB1.isChecked()){
                  Toast t= Toast.makeText(getApplicationContext(), rB1.getText().toString()+ " sent!", Toast.LENGTH_LONG);
                  t.show();
                  answer = "1st Choice";
  
              }
              if (rB2.isChecked()){
                  Toast t= Toast.makeText(getApplicationContext(), rB2.getText().toString()+ " sent!", Toast.LENGTH_LONG);
                  t.show();
                  answer = "2nd  Choice";
  
              }
              if (rB3.isChecked()){
                  Toast t= Toast.makeText(getApplicationContext(), rB3.getText().toString()+ " sent!", Toast.LENGTH_LONG);
                  t.show();
                  answer = "3rd  Choice";
  
              }
              if (rB4.isChecked()){
                  Toast t= Toast.makeText(getApplicationContext(), rB4.getText().toString()+ " sent!", Toast.LENGTH_LONG);
                  t.show();
                  answer = "4th  Choice";
  
              }
              try {
                  InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                  socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
              } catch (UnknownHostException e1) {
                  e1.printStackTrace();
              } catch (IOException e1) {
                  e1.printStackTrace();
              }
  
              try {
              DataOutputStream oos = new DataOutputStream(socket.getOutputStream());
              oos.writeBytes(answer);
              oos.close();
                  //Toast toast3 = Toast.makeText(getBaseContext(), "Message was sent successfully!", Toast.LENGTH_LONG);
                  //toast3.show();
              } catch (UnknownHostException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (Exception e) {
                  e.printStackTrace();
              }
              try {
  
                  socket.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      });
  
  }
  }
  </code></pre>
  
  <p>welcome.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout android:id="@+id/LinearLayout01"
  android:layout_width="fill_parent" android:layout_height="fill_parent" 
  xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
  android:background="@drawable/evening" 
  &gt;
  
  
  &lt;TextView android:text="@string/welcomeMessage" android:layout_width="fill_parent" android:layout_height="fill_parent" 
  android:textStyle="bold" android:textSize="25sp" android:textColor="#FFFFFF"
  android:gravity="center"
  &gt;&lt;/TextView&gt;
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Answer.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout android:id="@+id/LinearLayout01"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  
  &gt;
  &lt;ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="true" 
  android:scrollbarStyle="outsideOverlay"&gt;
  &lt;LinearLayout android:id="@+id/LinearLayout02"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:scrollbars="vertical" android:scrollbarAlwaysDrawVerticalTrack="true"
  &gt;
      &lt;TextView android:id="@+id/textQuestion1" android:layout_width="fill_parent" android:text="@string/textQuestion" android:layout_height="wrap_content"&gt;&lt;/TextView&gt;
  &lt;RadioGroup android:id="@+id/radioGroup01" 
  android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="fill_parent"  &gt;
  &lt;RadioButton android:id="@+id/rButton1" android:text="@string/c1Label" android:layout_height="wrap_content" android:layout_width="fill_parent"&gt; &lt;/RadioButton&gt;
  &lt;RadioButton android:id="@+id/rButton2" android:text="@string/c2Label" android:layout_height="wrap_content" android:layout_width="fill_parent"&gt; &lt;/RadioButton&gt;
  &lt;RadioButton android:id="@+id/rButton3" android:text="@string/c3Label" android:layout_height="wrap_content" android:layout_width="fill_parent"&gt; &lt;/RadioButton&gt;
  &lt;RadioButton android:id="@+id/rButton4" android:text="@string/c4Label" android:layout_height="wrap_content" android:layout_width="fill_parent"&gt; &lt;/RadioButton&gt;
  &lt;/RadioGroup&gt;
  &lt;Button android:text="Submit" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt;
  &lt;Button android:text="Connect" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt;
  &lt;/LinearLayout&gt;
  
  &lt;/ScrollView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>label.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  &lt;style name="CodeFont"&gt;
      &lt;item name="android:layout_width"&gt;fill_parent&lt;/item&gt;
      &lt;item name="android:layout_height"&gt;fill_parent&lt;/item&gt;
      &lt;item name="android:textColor"&gt;#00FF00&lt;/item&gt;
      &lt;item name="android:singleLine"&gt;false&lt;/item&gt;
       &lt;item name="android:lines"&gt;2&lt;/item&gt;
  &lt;/style&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>strings.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
  &lt;string name="app_name"&gt;Please answer quickly&lt;/string&gt;
  &lt;string name="c1Label"&gt;First choice&lt;/string&gt;
  &lt;string name="c2Label"&gt;Second choice&lt;/string&gt;
  &lt;string name="c3Label"&gt;Third choice&lt;/string&gt;
  &lt;string name="c4Label"&gt;Fourth choice&lt;/string&gt;
  &lt;string name="welcomeMessage"&gt;Welcome!&lt;/string&gt;
  
  &lt;string name="textQuestion"&gt;No question yet...&lt;/string&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>This is the java class that is supposed to send the question to the android app</p>
  
  <p>serverapp.java:</p>
  
  <pre><code>import java.io.*;
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import javax.swing.border.EmptyBorder;
  
  import java.net.*;
  
  public class ServerApp implements Runnable {
  // Connect status constants
  public final static int NULL = 0;
  public final static int DISCONNECTED = 1;
  public final static int DISCONNECTING = 2;
  public final static int BEGIN_CONNECT = 3;
  public final static int CONNECTED = 4;
  
  // Other constants
  public final static String statusMessages[] = {
    " Error! Could not connect!", " Disconnected",
    " Disconnecting...", " Connecting...", " Connected"
  };
  public final static ServerApp tcpObj = new ServerApp();
  public final static String END_SESSION =
    new Character((char)0).toString(); // Indicates the end of a session
  
  // Connection atate info
  public static String hostIP = "localhost";
  public static int port = 7076;
  public static int connectionStatus = DISCONNECTED;
  public static boolean isAnswerMode = true;
  public static String statusString = statusMessages[connectionStatus];
  public static StringBuffer toAppend = new StringBuffer("");
  public static StringBuffer toSend = new StringBuffer("");
  
  // Various GUI components and info
  public static JFrame mainFrame = null;
  public static JTextArea getText = null;
  public static JTextField outText = null;
  public static JCheckBox cbAnswer1 = null;
  public static JCheckBox cbAnswer2 = null;
  public static JCheckBox cbAnswer3 = null;
  public static JCheckBox cbAnswer4 = null;
  public static JPanel statusBar = null;
  public static JLabel statusField = null;
  public static JTextField statusColor = null;
  public static JTextField ipField = null;
  public static JTextField portField = null;
  public static JRadioButton answerMode = null;
  public static JRadioButton questionMode = null;
  public static JButton connectButton = null;
  public static JButton disconnectButton = null;
  
  // TCP Components
  public static ServerSocket hostServer = null;
  public static Socket socket = null;
  public static BufferedReader in = null;
  public static PrintWriter out = null;
  
  //members
  public static String questionText = null;
  
  /////////////////////////////////////////////////////////////////
  
  private static JPanel initOptionsPane() {
    JPanel pane = null;
    ActionAdapter buttonListener = null;
  
    // Create an options pane
    JPanel optionsPane = new JPanel(new GridLayout(6, 1));
  
    //Window label
    pane = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pane.add(new JLabel("SERVER APP"));
    //pane.add(ipField);
    optionsPane.add(pane);
  
    // IP address input
    pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    pane.add(new JLabel("Host IP:"));
    ipField = new JTextField(10); 
    ipField.setText(hostIP);
    ipField.setEnabled(false);
    ipField.addFocusListener(new FocusAdapter() {
          public void focusLost(FocusEvent e) {
             ipField.selectAll();
             // Should be editable only when disconnected
             if (connectionStatus != DISCONNECTED) {
                changeStatusNTS(NULL, true);
             }
             else {
                hostIP = ipField.getText();
             }
          }
       });
    pane.add(ipField);
    optionsPane.add(pane);
  
    // Port input
    pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    pane.add(new JLabel("Port:"));
    portField = new JTextField(10); portField.setEditable(true);
    portField.setText((new Integer(port)).toString());
    portField.addFocusListener(new FocusAdapter() {
          public void focusLost(FocusEvent e) {
             // should be editable only when disconnected
             if (connectionStatus != DISCONNECTED) {
                changeStatusNTS(NULL, true);
             }
             else {
                int temp;
                try {
                   temp = Integer.parseInt(portField.getText());
                   port = temp;
                }
                catch (NumberFormatException nfe) {
                   portField.setText((new Integer(port)).toString());
                   mainFrame.repaint();
                }
             }
          }
       });
    pane.add(portField);
    optionsPane.add(pane);
  
    pane = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pane.add(new JLabel());
    optionsPane.add(pane);
  
    pane = new JPanel(new FlowLayout(FlowLayout.LEFT));
    pane.add(new JLabel());
    optionsPane.add(pane);
  
    // Host/guest option
    buttonListener = new ActionAdapter() {
          public void actionPerformed(ActionEvent e) {
  
             if (connectionStatus != DISCONNECTED) {
                changeStatusNTS(NULL, true);
             }
             else {
                isAnswerMode = e.getActionCommand().equals("answers");
  
                // Cannot supply host IP if host option is chosen
                if (isAnswerMode) {
                   ipField.setEnabled(false);
                   ipField.setText("localhost");
                   hostIP = "localhost";
                }
                else {
                   ipField.setEnabled(true);
                }
             }
          }
       };
  
  
    // Connect/disconnect buttons
    JPanel buttonPane = new JPanel(new GridLayout(1, 2));
    buttonListener = new ActionAdapter() {
          public void actionPerformed(ActionEvent e) {
             // Request a connection initiation
             if (e.getActionCommand().equals("connect")) {
                changeStatusNTS(BEGIN_CONNECT, true);
             }
             // Disconnect
             else {
                changeStatusNTS(DISCONNECTING, true);
             }
          }
       };
    connectButton = new JButton("Connect");
    connectButton.setMnemonic(KeyEvent.VK_C);
    connectButton.setActionCommand("connect");
    connectButton.addActionListener(buttonListener);
    connectButton.setEnabled(true);
    disconnectButton = new JButton("Disconnect");
    disconnectButton.setMnemonic(KeyEvent.VK_D);
    disconnectButton.setActionCommand("disconnect");
    disconnectButton.addActionListener(buttonListener);
    disconnectButton.setEnabled(false);
    buttonPane.add(connectButton);
    buttonPane.add(disconnectButton);
    optionsPane.add(buttonPane);
  
    return optionsPane;
  }
  
  /////////////////////////////////////////////////////////////////
  
  // Initialize all the GUI components and display the frame
  private static void initGUI() {
    // Set up the status bar
    statusField = new JLabel();
    statusField.setText(statusMessages[DISCONNECTED]);
    statusColor = new JTextField(1);
    statusColor.setBackground(Color.red);
    statusColor.setEditable(false);
    statusBar = new JPanel(new BorderLayout());
    statusBar.add(statusColor, BorderLayout.WEST);
    statusBar.add(statusField, BorderLayout.CENTER);
  
    // Set up the options pane
    JPanel optionsPane = initOptionsPane();
  
    // Set up the questions and answers pane
    JPanel questionsAnswersPane = new JPanel(new GridLayout(6,1) );
    questionsAnswersPane.setBorder(new EmptyBorder(4, 4, 4, 4) );
  
    getText = new JTextArea(10, 20);
    getText.setLineWrap(true);
    getText.setEditable(false);
    getText.setForeground(Color.blue);
    getText.setBackground(Color.LIGHT_GRAY);
    JScrollPane chatTextPane = new JScrollPane(getText,
       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
       JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  
    //Question text     
    //JPanel pane = new JPanel(new FlowLayout(FlowLayout.LEFT));
    outText = new JTextField(20);
  
    //getLine
    //pane.add(getLine);
  
    JPanel pane2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    cbAnswer1 = new JCheckBox("Answer one ........... ");
    pane2.add(cbAnswer1);
    JPanel pane3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    cbAnswer2 = new JCheckBox("Answer two ........... ");
    pane3.add(cbAnswer2);
  
    JPanel pane4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    cbAnswer3 = new JCheckBox("Answer three ........... ");
    pane4.add(cbAnswer3);
  
    JPanel pane5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    cbAnswer4 = new JCheckBox("Answer four ........... ");
    pane5.add(cbAnswer4);
  
  
    questionsAnswersPane.add(outText, BorderLayout.NORTH);
    questionsAnswersPane.add(pane2);
    questionsAnswersPane.add(pane3);
    questionsAnswersPane.add(pane4);
    questionsAnswersPane.add(pane5);
    questionsAnswersPane.add(chatTextPane, BorderLayout.SOUTH);
    questionsAnswersPane.setPreferredSize(new Dimension(200, 200));
  
    // Set up the main pane
    JPanel mainPane = new JPanel(new BorderLayout());
    mainPane.add(statusBar, BorderLayout.SOUTH);
    mainPane.add(optionsPane, BorderLayout.WEST);
    mainPane.add(questionsAnswersPane, BorderLayout.CENTER);
  
    // Set up the main frame
    mainFrame = new JFrame("Server");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setContentPane(mainPane);
    mainFrame.setSize(mainFrame.getPreferredSize());
    mainFrame.setLocation(200, 200);
    mainFrame.pack();
    mainFrame.setVisible(true);
  }
  
  /////////////////////////////////////////////////////////////////
  
  // The thread-safe way to change the GUI components while
  // changing state
  private static void changeStatusTS(int newConnectStatus, boolean noError) {
    // Change state if valid state
    if (newConnectStatus != NULL) {
       connectionStatus = newConnectStatus;
    }
  
    // If there is no error, display the appropriate status message
    if (noError) {
       statusString = statusMessages[connectionStatus];
    }
    // Otherwise, display error message
    else {
       statusString = statusMessages[NULL];
    }
  
    // Call the run() routine (Runnable interface) on the
    // error-handling and GUI-update thread
    SwingUtilities.invokeLater(tcpObj);
  }
  
  /////////////////////////////////////////////////////////////////
  
  // The non-thread-safe way to change the GUI components while
  // changing state
  private static void changeStatusNTS(int newConnectStatus, boolean noError) {
    // Change state if valid state
    if (newConnectStatus != NULL) {
       connectionStatus = newConnectStatus;
    }
  
    // If there is no error, display the appropriate status message
    if (noError) {
       statusString = statusMessages[connectionStatus];
    }
    // Otherwise, display error message
    else {
       statusString = statusMessages[NULL];
    }
  
    // Call the run() routine (Runnable interface) on the
    // current thread
    tcpObj.run();
  }
  
  /////////////////////////////////////////////////////////////////
  
  // Thread-safe way to append to the chat box
  private static void appendToChatBox(String s) {
    synchronized (toAppend) {
       toAppend.append(s);
    }
  }
  
  /////////////////////////////////////////////////////////////////
  
  // private static void sendString(String s) {
  //    synchronized (toSend) {
  //      toSend.append(s + "\n");
  //  }
  //}
  
  /////////////////////////////////////////////////////////////////
  
  // Cleanup for disconnect
  private static void cleanUp() {
    try {
       if (hostServer != null) {
          hostServer.close();
          hostServer = null;
       }
    }
    catch (IOException e) { hostServer = null; }
  
    try {
       if (socket != null) {
          socket.close();
          socket = null;
       }
    }
    catch (IOException e) { socket = null; }
  
    try {
       if (in != null) {
          in.close();
          in = null;
       }
    }
    catch (IOException e) { in = null; }
  
    if (out != null) {
       out.close();
       out = null;
    }
  }
  
  /////////////////////////////////////////////////////////////////
  
  // Checks the current state and sets the enables/disables
  // accordingly
  public void run() {
    switch (connectionStatus) {
    case DISCONNECTED:
       // this line is here for testing purposes
       questionText =  null;
       connectButton.setEnabled(true);
       disconnectButton.setEnabled(false);
       ipField.setEnabled(true);
       portField.setEnabled(true);
       //answerMode.setEnabled(true);
       //questionMode.setEnabled(true);
       //getLine.setText(""); 
       //getLine.setEnabled(false);
       statusColor.setBackground(Color.red);
       break;
  
    case DISCONNECTING:
       connectButton.setEnabled(false);
       disconnectButton.setEnabled(false);
       ipField.setEnabled(false);
       portField.setEnabled(false);
       //answerMode.setEnabled(false);
       //questionMode.setEnabled(false);
       //getLine1.setEnabled(false);
       statusColor.setBackground(Color.orange);
       break;
  
    case CONNECTED:
       // this line is here for testing purposes
       questionText = outText.getText() ;
       connectButton.setEnabled(false);
       disconnectButton.setEnabled(true);
       ipField.setEnabled(false);
       portField.setEnabled(false);
       //answerMode.setEnabled(false);
       //questionMode.setEnabled(false);
       //getLine.setEnabled(true);
       statusColor.setBackground(Color.green);
       break;
  
    case BEGIN_CONNECT:
       connectButton.setEnabled(false);
       disconnectButton.setEnabled(false);
       ipField.setEnabled(false);
       portField.setEnabled(false);
       //answerMode.setEnabled(false);
       //questionMode.setEnabled(false);
       //getLine.setEnabled(false);
       //getLine.grabFocus();
       statusColor.setBackground(Color.orange);
       break;
    }
  
    // Make sure that the button/text field states are consistent
    // with the internal states
    ipField.setText(hostIP);
    portField.setText((new Integer(port)).toString());
    //answerMode.setSelected(isAnswerMode);
    //questionMode.setSelected(!isAnswerMode);
    statusField.setText(statusString);
    getText.append(toAppend.toString());
    toAppend.setLength(0);
  
    mainFrame.repaint();
  }
  
  /////////////////////////////////////////////////////////////////
  
  // The main procedure
  public static void main(String args[]) {
    String s;
  
    initGUI();
  
    while (true) {
       switch (connectionStatus) {
       case BEGIN_CONNECT:
          try {
             // Try to set up a server if host
             //if (isAnswerMode) {
                hostServer = new ServerSocket(port);
                socket = hostServer.accept();
             //}
             in = new BufferedReader(new 
                InputStreamReader(socket.getInputStream()));
             out = new PrintWriter(socket.getOutputStream(), true);
             changeStatusTS(CONNECTED, true);
          }
          // If error, clean up and output an error message
          catch (IOException e) {
             cleanUp();
             changeStatusTS(DISCONNECTED, false);
          }
          break;
  
       case CONNECTED:
          try {
             // Receive data
             if (in.ready()) {
                s = in.readLine();
                if ((s != null) &amp;&amp;  (s.length() != 0)) {                    
                    if (s.equalsIgnoreCase("q"))
                    {
  
                           out.print(questionText); 
                           out.flush();
                           toSend.setLength(0);
                           changeStatusTS(NULL, true);
                    }else
                    {
                           appendToChatBox("Answer: " + s + "\n");
                           changeStatusTS(NULL, true);
                    }                     
                }
             }
          }
          catch (IOException e) {
             cleanUp();
             changeStatusTS(DISCONNECTED, false);
          }
          break;
  
       case DISCONNECTING:
          // Tell other chatter to disconnect as well
          out.print(END_SESSION); 
          out.flush();
          // Clean up (close all streams/sockets)
          cleanUp();
          changeStatusTS(DISCONNECTED, true);
          break;
  
       default: 
          break; // do nothing
       }
    }
  }
  }
  
  ////////////////////////////////////////////////////////////////////
  
  // Action adapter for easy event-listener coding
  class ActionAdapter implements ActionListener {
  public void actionPerformed(ActionEvent e) {}
  }
  
  ////////////////////////////////////////////////////////////////////
  </code></pre>
  
  <p>thank you very much in advance for your time and help.</p>