Exercises the various protocols available in the Commons Net Package : Net « Apache Common « Java






Exercises the various protocols available in the Commons Net Package

/*
 * ProtocolRunner.java
 *
 * Created on 22 December 2003, 12:57
 */
// Import Java Packages
import java.awt.Point;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Component;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JDialog;
import javax.swing.UIManager;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.net.SocketException;

import java.io.File;
import java.util.Date;
import java.util.Calendar;
import javax.swing.JDialog;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.GregorianCalendar;

// Import NET Packages
import org.apache.commons.net.*;
import org.apache.commons.net.io.*;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.nntp.*;
import org.apache.commons.net.pop3.*;
import org.apache.commons.net.smtp.*;
import org.apache.commons.net.tftp.*;

/**
 * ProtocolRunner class exercises the various protocols available in the
 * Commons Net Package
 * @author  Vikram Goyal
 */
public class ProtocolRunner extends javax.swing.JFrame {
    
    /** Creates new form ProtocolRunner */
    public ProtocolRunner() { 
        // set the native look and feel
        try{
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e) {}
        
        initComponents();
        // size it so that it appears full screen
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screen.width, screen.height);
        this.pack();
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        ftpPanel = new javax.swing.JPanel();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton8 = new javax.swing.JRadioButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jRadioButton7 = new javax.swing.JRadioButton();
        nntpPanel = new javax.swing.JPanel();
        jRadioButton10 = new javax.swing.JRadioButton();
        jRadioButton15 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton11 = new javax.swing.JRadioButton();
        jRadioButton12 = new javax.swing.JRadioButton();
        jRadioButton13 = new javax.swing.JRadioButton();
        jRadioButton14 = new javax.swing.JRadioButton();
        smtpPanel = new javax.swing.JPanel();
        jRadioButton17 = new javax.swing.JRadioButton();
        jRadioButton18 = new javax.swing.JRadioButton();
        jRadioButton16 = new javax.swing.JRadioButton();
        pop3Panel = new javax.swing.JPanel();
        jRadioButton19 = new javax.swing.JRadioButton();
        jRadioButton20 = new javax.swing.JRadioButton();
        jRadioButton21 = new javax.swing.JRadioButton();
        jRadioButton22 = new javax.swing.JRadioButton();
        jRadioButton23 = new javax.swing.JRadioButton();
        jRadioButton24 = new javax.swing.JRadioButton();
        jRadioButton9 = new javax.swing.JRadioButton();
        ftpButtonGroup = new javax.swing.ButtonGroup();
        nntpButtonGroup = new javax.swing.ButtonGroup();
        smtpButtonGroup = new javax.swing.ButtonGroup();
        pop3ButtonGroup = new javax.swing.ButtonGroup();
        tftpPanel = new javax.swing.JPanel();
        jRadioButton25 = new javax.swing.JRadioButton();
        jRadioButton26 = new javax.swing.JRadioButton();
        tftpPanelButtonGroup = new javax.swing.ButtonGroup();
        loginDialog = new javax.swing.JDialog();
        jLabel9 = new javax.swing.JLabel();
        jPanel13 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        loginUsername = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        loginPassword = new javax.swing.JPasswordField();
        jPanel14 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        singleValueDialog = new javax.swing.JDialog();
        jLabel12 = new javax.swing.JLabel();
        jPanel15 = new javax.swing.JPanel();
        singleValueLabel = new javax.swing.JLabel();
        singleValueField = new javax.swing.JTextField();
        jPanel16 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        multipleValueDialog = new javax.swing.JDialog();
        jLabel13 = new javax.swing.JLabel();
        jPanel17 = new javax.swing.JPanel();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jPanel18 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        messageArea = new javax.swing.JTextArea();
        jPanel19 = new javax.swing.JPanel();
        jLabel14 = new javax.swing.JLabel();
        fromField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        subjectField = new javax.swing.JTextField();
        variableLabel = new javax.swing.JLabel();
        variableField = new javax.swing.JTextField();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tcpServerResponse = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        presetPanel = new javax.swing.JPanel();
        defaultPanel = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        commandBox = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        tcpSelectionBox = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        serverNameField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        portNumberField = new javax.swing.JTextField();
        tcpConnectButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        udpSelectionBox = new javax.swing.JComboBox();
        jPanel12 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        udpRemoteServerField = new javax.swing.JTextField();
        jPanel11 = new javax.swing.JPanel();
        udpCommandPanel = new javax.swing.JPanel();
        udpStartButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        udpServerResponse = new javax.swing.JTextArea();

        ftpPanel.setLayout(new java.awt.GridLayout(3, 3));

        jRadioButton4.setText("Login");
        ftpButtonGroup.add(jRadioButton4);
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        ftpPanel.add(jRadioButton4);

        jRadioButton8.setText("Logout");
        ftpButtonGroup.add(jRadioButton8);
        jRadioButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton8ActionPerformed(evt);
            }
        });

        ftpPanel.add(jRadioButton8);

        jRadioButton1.setText("Change Working Directory");
        ftpButtonGroup.add(jRadioButton1);
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        ftpPanel.add(jRadioButton1);

        jRadioButton3.setText("List Files");
        ftpButtonGroup.add(jRadioButton3);
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        ftpPanel.add(jRadioButton3);

        jRadioButton5.setText("Make Directory");
        ftpButtonGroup.add(jRadioButton5);
        jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton5ActionPerformed(evt);
            }
        });

        ftpPanel.add(jRadioButton5);

        jRadioButton6.setText("Upload File");
        ftpButtonGroup.add(jRadioButton6);
        jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton6ActionPerformed(evt);
            }
        });

        ftpPanel.add(jRadioButton6);

        jRadioButton7.setText("Download File");
        ftpButtonGroup.add(jRadioButton7);
        jRadioButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton7ActionPerformed(evt);
            }
        });

        ftpPanel.add(jRadioButton7);

        nntpPanel.setLayout(new java.awt.GridLayout(3, 3));

        jRadioButton10.setText("Login");
        nntpButtonGroup.add(jRadioButton10);
        jRadioButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton10ActionPerformed(evt);
            }
        });

        nntpPanel.add(jRadioButton10);

        jRadioButton15.setText("Logout");
        nntpButtonGroup.add(jRadioButton15);
        jRadioButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton15ActionPerformed(evt);
            }
        });

        nntpPanel.add(jRadioButton15);

        jRadioButton2.setText("List All NewsGroups");
        nntpButtonGroup.add(jRadioButton2);
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        nntpPanel.add(jRadioButton2);

        jRadioButton11.setText("List New NewsGroups");
        nntpButtonGroup.add(jRadioButton11);
        jRadioButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton11ActionPerformed(evt);
            }
        });

        nntpPanel.add(jRadioButton11);

        jRadioButton12.setText("List New News");
        nntpButtonGroup.add(jRadioButton12);
        jRadioButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton12ActionPerformed(evt);
            }
        });

        nntpPanel.add(jRadioButton12);

        jRadioButton13.setText("Post Article");
        nntpButtonGroup.add(jRadioButton13);
        jRadioButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton13ActionPerformed(evt);
            }
        });

        nntpPanel.add(jRadioButton13);

        jRadioButton14.setText("Get Article");
        nntpButtonGroup.add(jRadioButton14);
        jRadioButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton14ActionPerformed(evt);
            }
        });

        nntpPanel.add(jRadioButton14);

        smtpPanel.setLayout(new java.awt.GridLayout(1, 1));

        jRadioButton17.setText("Login");
        smtpButtonGroup.add(jRadioButton17);
        jRadioButton17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton17ActionPerformed(evt);
            }
        });

        smtpPanel.add(jRadioButton17);

        jRadioButton18.setText("Logout");
        smtpButtonGroup.add(jRadioButton18);
        jRadioButton18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton18ActionPerformed(evt);
            }
        });

        smtpPanel.add(jRadioButton18);

        jRadioButton16.setText("Send Simple Message");
        smtpButtonGroup.add(jRadioButton16);
        jRadioButton16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton16ActionPerformed(evt);
            }
        });

        smtpPanel.add(jRadioButton16);

        pop3Panel.setLayout(new java.awt.GridLayout(2, 0));

        jRadioButton19.setText("Login");
        pop3ButtonGroup.add(jRadioButton19);
        jRadioButton19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton19ActionPerformed(evt);
            }
        });

        pop3Panel.add(jRadioButton19);

        jRadioButton20.setText("Logout");
        pop3ButtonGroup.add(jRadioButton20);
        jRadioButton20.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton20ActionPerformed(evt);
            }
        });

        pop3Panel.add(jRadioButton20);

        jRadioButton21.setText("List All Messages");
        pop3ButtonGroup.add(jRadioButton21);
        jRadioButton21.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton21ActionPerformed(evt);
            }
        });

        pop3Panel.add(jRadioButton21);

        jRadioButton22.setText("Status");
        pop3ButtonGroup.add(jRadioButton22);
        jRadioButton22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton22ActionPerformed(evt);
            }
        });

        pop3Panel.add(jRadioButton22);

        jRadioButton23.setText("Retrieve Message");
        pop3ButtonGroup.add(jRadioButton23);
        jRadioButton23.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton23ActionPerformed(evt);
            }
        });

        pop3Panel.add(jRadioButton23);

        jRadioButton24.setText("Delete Message");
        pop3ButtonGroup.add(jRadioButton24);
        jRadioButton24.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton24ActionPerformed(evt);
            }
        });

        pop3Panel.add(jRadioButton24);

        jRadioButton9.setText("Reset");
        pop3ButtonGroup.add(jRadioButton9);
        jRadioButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton9ActionPerformed(evt);
            }
        });

        pop3Panel.add(jRadioButton9);

        tftpPanel.setLayout(new java.awt.GridLayout(1, 0));

        jRadioButton25.setText("Send A File");
        tftpPanelButtonGroup.add(jRadioButton25);
        jRadioButton25.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton25ActionPerformed(evt);
            }
        });

        tftpPanel.add(jRadioButton25);

        jRadioButton26.setText("Receive A File");
        tftpPanelButtonGroup.add(jRadioButton26);
        jRadioButton26.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton26ActionPerformed(evt);
            }
        });

        tftpPanel.add(jRadioButton26);

        loginDialog.getContentPane().setLayout(new java.awt.BorderLayout(0, 5));

        loginDialog.setTitle("Input Required");
        loginDialog.setModal(true);
        loginDialog.setResizable(false);
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Please Enter Login Details");
        loginDialog.getContentPane().add(jLabel9, java.awt.BorderLayout.NORTH);

        jPanel13.setLayout(new java.awt.GridLayout(2, 2, 5, 3));

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Username:");
        jPanel13.add(jLabel10);

        loginUsername.setColumns(15);
        jPanel13.add(loginUsername);

        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("Password:");
        jPanel13.add(jLabel11);

        loginPassword.setColumns(15);
        jPanel13.add(loginPassword);

        loginDialog.getContentPane().add(jPanel13, java.awt.BorderLayout.CENTER);

        jPanel14.setLayout(new java.awt.GridLayout(1, 0));

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel14.add(jButton1);

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel14.add(jButton2);

        loginDialog.getContentPane().add(jPanel14, java.awt.BorderLayout.SOUTH);

        singleValueDialog.setTitle("Input Required");
        singleValueDialog.setModal(true);
        singleValueDialog.setResizable(false);
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel12.setText("Please enter a value");
        singleValueDialog.getContentPane().add(jLabel12, java.awt.BorderLayout.NORTH);

        jPanel15.setLayout(new java.awt.GridLayout(1, 0, 3, 0));

        singleValueLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jPanel15.add(singleValueLabel);

        singleValueField.setColumns(15);
        jPanel15.add(singleValueField);

        singleValueDialog.getContentPane().add(jPanel15, java.awt.BorderLayout.CENTER);

        jPanel16.setLayout(new java.awt.GridLayout(1, 0, 5, 0));

        jButton3.setText("OK");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jPanel16.add(jButton3);

        jButton4.setText("Cancel");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jPanel16.add(jButton4);

        singleValueDialog.getContentPane().add(jPanel16, java.awt.BorderLayout.SOUTH);

        multipleValueDialog.setTitle("Input Required");
        multipleValueDialog.setModal(true);
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel13.setText("Please enter the following values");
        multipleValueDialog.getContentPane().add(jLabel13, java.awt.BorderLayout.NORTH);

        jPanel17.setLayout(new java.awt.GridLayout(1, 0, 5, 0));

        jButton5.setText("OK");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jPanel17.add(jButton5);

        jButton6.setText("Cancel");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jPanel17.add(jButton6);

        multipleValueDialog.getContentPane().add(jPanel17, java.awt.BorderLayout.SOUTH);

        jPanel18.setLayout(new java.awt.BorderLayout());

        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel16.setText("Enter Message:");
        jPanel18.add(jLabel16, java.awt.BorderLayout.WEST);

        messageArea.setColumns(20);
        messageArea.setRows(15);
        jScrollPane3.setViewportView(messageArea);

        jPanel18.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jPanel19.setLayout(new java.awt.GridLayout(3, 2, 3, 3));

        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel14.setText("From:");
        jPanel19.add(jLabel14);

        fromField.setColumns(15);
        jPanel19.add(fromField);

        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel15.setText("Subject:");
        jPanel19.add(jLabel15);

        subjectField.setColumns(15);
        jPanel19.add(subjectField);

        variableLabel.setText("Newsgroup:");
        jPanel19.add(variableLabel);

        variableField.setColumns(15);
        jPanel19.add(variableField);

        jPanel18.add(jPanel19, java.awt.BorderLayout.NORTH);

        multipleValueDialog.getContentPane().add(jPanel18, java.awt.BorderLayout.CENTER);

        setTitle("Protocol Runner");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel4.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setBorder(new javax.swing.border.TitledBorder("Server Output"));
        tcpServerResponse.setEditable(false);
        tcpServerResponse.setFont(new java.awt.Font("Monospaced", 0, 12));
        jScrollPane1.setViewportView(tcpServerResponse);

        jPanel4.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel7.setLayout(new java.awt.GridLayout(1, 2, 5, 0));

        presetPanel.setLayout(new java.awt.BorderLayout());

        presetPanel.setBorder(new javax.swing.border.TitledBorder("Preset Commands"));
        jLabel5.setFont(new java.awt.Font("Dialog", 3, 12));
        jLabel5.setText("No Preset Commands. Simply Connect");
        defaultPanel.add(jLabel5);

        presetPanel.add(defaultPanel, java.awt.BorderLayout.CENTER);

        jPanel7.add(presetPanel);

        jPanel9.setBorder(new javax.swing.border.TitledBorder("Freeflow Commands"));
        jLabel4.setText("Type Command Here:");
        jPanel9.add(jLabel4);

        commandBox.setColumns(15);
        commandBox.setEnabled(false);
        commandBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                commandBoxActionPerformed(evt);
            }
        });

        jPanel9.add(commandBox);

        jPanel7.add(jPanel9);

        jPanel4.add(jPanel7, java.awt.BorderLayout.NORTH);

        jPanel1.add(jPanel4, java.awt.BorderLayout.CENTER);

        jPanel5.setLayout(new java.awt.GridLayout(2, 1, 0, 5));

        jPanel6.setBorder(new javax.swing.border.TitledBorder("Protocol"));
        jLabel3.setText("Select Protocol:");
        jPanel6.add(jLabel3);

        tcpSelectionBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CharGen", "Daytime", "Echo", "Finger", "FTP", "NNTP", "POP3", "SMTP", "Time", "Whois" }));
        tcpSelectionBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tcpSelectionBoxActionPerformed(evt);
            }
        });

        jPanel6.add(tcpSelectionBox);

        jPanel5.add(jPanel6);

        jPanel3.setBorder(new javax.swing.border.TitledBorder("Server Connection Details"));
        jLabel1.setText("Server:");
        jPanel3.add(jLabel1);

        serverNameField.setColumns(25);
        jPanel3.add(serverNameField);

        jLabel2.setText("Port:");
        jPanel3.add(jLabel2);

        portNumberField.setColumns(4);
        portNumberField.setText("19");
        jPanel3.add(portNumberField);

        tcpConnectButton.setText("CONNECT");
        tcpConnectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tcpConnectButtonActionPerformed(evt);
            }
        });

        jPanel3.add(tcpConnectButton);

        jPanel5.add(jPanel3);

        jPanel1.add(jPanel5, java.awt.BorderLayout.NORTH);

        jTabbedPane1.addTab("TCP", jPanel1);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel10.setLayout(new java.awt.GridLayout(2, 0));

        jPanel8.setBorder(new javax.swing.border.TitledBorder("Protocol"));
        jLabel6.setText("Select Protocol:");
        jPanel8.add(jLabel6);

        udpSelectionBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CharGen", "Daytime", "Echo", "TFTP", "Time" }));
        udpSelectionBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                udpSelectionBoxActionPerformed(evt);
            }
        });

        jPanel8.add(udpSelectionBox);

        jPanel10.add(jPanel8);

        jPanel12.setBorder(new javax.swing.border.TitledBorder("Remote Server Details"));
        jLabel7.setText("Remote Server:");
        jPanel12.add(jLabel7);

        udpRemoteServerField.setColumns(25);
        jPanel12.add(udpRemoteServerField);

        jPanel10.add(jPanel12);

        jPanel2.add(jPanel10, java.awt.BorderLayout.NORTH);

        jPanel11.setLayout(new java.awt.BorderLayout());

        udpCommandPanel.setLayout(new java.awt.GridLayout(1, 0));

        udpCommandPanel.setBorder(new javax.swing.border.TitledBorder("Command Panel"));
        udpStartButton.setText("START");
        udpStartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                udpStartButtonActionPerformed(evt);
            }
        });

        udpCommandPanel.add(udpStartButton);

        jPanel11.add(udpCommandPanel, java.awt.BorderLayout.NORTH);

        udpServerResponse.setEditable(false);
        udpServerResponse.setFont(new java.awt.Font("Monospaced", 0, 12));
        jScrollPane2.setViewportView(udpServerResponse);

        jPanel11.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel11, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("UDP", jPanel2);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void jRadioButton26ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton26ActionPerformed
        // tftp receive a file:
        udpRemoteServer = udpRemoteServerField.getText();
        if(udpRemoteServer == null || udpRemoteServer.length() == 0) {
            showError("Please enter a remote server");
            return;
        }
        
        // get the name of the file to receive
        udpStartButton.doClick();
        
        singleValueLabel.setText("Enter file path (TFTP-Receive):");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton26ActionPerformed

    private void jRadioButton25ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton25ActionPerformed
        // tftp send a file:
        udpRemoteServer = udpRemoteServerField.getText();
        if(udpRemoteServer == null || udpRemoteServer.length() == 0) {
            showError("Please enter a remote server");
            return;
        }
        
        // get the name of the file to send
        udpStartButton.doClick();
        
        singleValueLabel.setText("Enter file path (TFTP-Send):");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton25ActionPerformed

    private void udpStartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_udpStartButtonActionPerformed
        // handler for Start button for UDP:
        if(started) { // means a session has already started
            try{
                handleUDPStop();
            }catch(IOException ex) {
                showError(ex.getMessage());
            }
            return;
        }
        
        udpRemoteServer = udpRemoteServerField.getText();
        
        if(udpRemoteServer == null || udpRemoteServer.length() == 0) {
            showError("Please enter a remote server");
            return;
        }

        try{
            udpClient = 
              ClientFactory.getUDPClientInstance(
                  udpSelectionBox.getSelectedIndex());
            udpClient.open();
        }catch(SocketException se){
            showError(se.getMessage());
            return;
        }catch(IOException ioex){
            showError(ioex.getMessage());
            return;
        }
        
        // if we come here means we have opened a port
        started = true;
        udpStartButton.setText("Stop");
        udpSelectionBox.setEnabled(false);
        jTabbedPane1.setEnabledAt(0, false);
        
        try {
            ProtocolHandler.handleUDPProtocol(
                udpSelectionBox.getSelectedIndex(), 
                udpClient, 
                this);
        }catch (IOException io) {
            showError(io.getMessage());
            io.printStackTrace();
        }        
        
        
    }//GEN-LAST:event_udpStartButtonActionPerformed

    private void jRadioButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton9ActionPerformed
        // reset for pop3:
        if(!checkValidConnection()) return;
        try {
            ((POP3Client)tcpClient).reset();
            tcpServerResponse.append(
                ((POP3Client)tcpClient).getReplyString() + 
                "\r\n");
        }catch(IOException ioex) { showError(ioex.getMessage()); return; }        
    }//GEN-LAST:event_jRadioButton9ActionPerformed

    private void jRadioButton24ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton24ActionPerformed
        // delete message for pop3:
        if(!checkValidConnection()) return;
        
        singleValueLabel.setText("Enter message number to delete:");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton24ActionPerformed

    private void jRadioButton23ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton23ActionPerformed
        // Retrieve message for pop3:
        if(!checkValidConnection()) return;
        
        singleValueLabel.setText("Enter message number:");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton23ActionPerformed

    private void jRadioButton21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton21ActionPerformed
        // list all messages for pop3:
        if(!checkValidConnection()) return;
        try {
            ((POP3Client)tcpClient).listMessages();
            tcpServerResponse.append(
                ((POP3Client)tcpClient).getReplyString() + 
                "\r\n");
        }catch(IOException ioex) { showError(ioex.getMessage()); return; }        
    }//GEN-LAST:event_jRadioButton21ActionPerformed

    private void jRadioButton22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton22ActionPerformed
        // status for pop3:
        if(!checkValidConnection()) return;
        try {
            ((POP3Client)tcpClient).status();
            tcpServerResponse.append(
                ((POP3Client)tcpClient).getReplyString() +
                "\r\n");
        }catch(IOException ioex) { showError(ioex.getMessage()); return; }        
    }//GEN-LAST:event_jRadioButton22ActionPerformed

    private void jRadioButton20ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton20ActionPerformed
        // logout for pop3:
        if(!checkValidConnection()) return;
        try {
            ((POP3Client)tcpClient).logout();
            tcpServerResponse.append(
                ((POP3Client)tcpClient).getReplyString() +
                "\r\n");
            handleDisconnect();
        }catch(IOException ioex) { showError(ioex.getMessage()); return; }
    }//GEN-LAST:event_jRadioButton20ActionPerformed

    private void jRadioButton19ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton19ActionPerformed
        // login for pop3:
        if(!checkValidConnection()) return; 
        showDialog(loginDialog);
    }//GEN-LAST:event_jRadioButton19ActionPerformed

    private void jRadioButton16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton16ActionPerformed
        // Send simple message for SMTP:
        if(!checkValidConnection()) return;
        
        variableLabel.setText("To:");
        showDialog(multipleValueDialog);
    }//GEN-LAST:event_jRadioButton16ActionPerformed

    private void jRadioButton18ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton18ActionPerformed
        // logout for SMTP:
        if(!checkValidConnection()) return;
        
        try {
            ((SMTPClient)tcpClient).logout();
            tcpServerResponse.append(
                ((SMTPClient)tcpClient).getReplyString() +
                "\r\n");
        }catch(IOException ioex) { showError(ioex.getMessage()); return; }         
    }//GEN-LAST:event_jRadioButton18ActionPerformed

    private void jRadioButton17ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton17ActionPerformed
        // login for SMTP:
        if(!checkValidConnection()) return;
        
        // for SMTP login is simply sending HELO
        try {
            ((SMTPClient)tcpClient).login();
            tcpServerResponse.append(
                ((SMTPClient)tcpClient).getReplyString() +
                "\r\n");
        }catch(IOException ioex) { showError(ioex.getMessage()); return; } 
        
    }//GEN-LAST:event_jRadioButton17ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // ok for multiple value dialog:
        String fromText     = fromField.getText();
        String subjectText  = subjectField.getText();
        String variableText = variableField.getText();
        String messageText  = messageArea.getText();
        
        DotTerminatedMessageWriter writer;
              
        int selection = tcpSelectionBox.getSelectedIndex();
        try{
            if(selection == 5) { // means handling NNTP posting of message
                writer = (DotTerminatedMessageWriter)
                    ((NNTPClient)tcpClient).postArticle();             
                
                if(writer == null) { 
                    tcpServerResponse.append(
                        ((NNTPClient)tcpClient).getReplyString() + "\r\n");
                    return;
                }
                
                // create simple NNTP headers
                SimpleNNTPHeader headers = 
                    new SimpleNNTPHeader(fromText, subjectText);
                headers.addNewsgroup(variableText);
                
                // and write the message on
                writer.write(headers.toString());
                writer.write(messageText);
                writer.close();
                ((NNTPClient)tcpClient).completePendingCommand();                
                tcpServerResponse.append(
                    ((NNTPClient)tcpClient).getReplyString() + "\r\n");                
            } else if(selection == 7) { // means handling SMTP posting of message
                
                ((SMTPClient)tcpClient).setSender(fromText);
                ((SMTPClient)tcpClient).addRecipient(variableText);

                writer = (DotTerminatedMessageWriter) 
                    ((SMTPClient)tcpClient).sendMessageData();                
                
                if(writer == null) {
                    tcpServerResponse.append(
                        ((SMTPClient)tcpClient).getReplyString() + "\r\n");
                    return;
                }
                
                // create Simple SMTP header
                SimpleSMTPHeader headers = 
                    new SimpleSMTPHeader(fromText, variableText, subjectText);
                
                writer.write(headers.toString());
                writer.write(messageText);
                writer.close();
                ((SMTPClient)tcpClient).completePendingCommand();
                tcpServerResponse.append(
                    ((SMTPClient)tcpClient).getReplyString() + "\r\n");                
            }
        }catch(IOException ioex) { showError(ioex.getMessage()); }
        finally{ 
            fromField.setText(""); 
            subjectField.setText(""); 
            variableField.setText(""); 
            messageArea.setText("");
            multipleValueDialog.setVisible(false);
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // cancel for multiple value dialog:
        multipleValueDialog.setVisible(false);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jRadioButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton13ActionPerformed
        // post article for nntp:
        if(!checkValidConnection()) return;
        
        variableLabel.setText("Newsgroup:");
        showDialog(multipleValueDialog);
    }//GEN-LAST:event_jRadioButton13ActionPerformed

    private void jRadioButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton14ActionPerformed
        // get article for nntp:
        if(!checkValidConnection()) return;
        
        singleValueLabel.setText("Enter message id:");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton14ActionPerformed

    private void jRadioButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton12ActionPerformed
        // list new news messages for nntp:
        if(!checkValidConnection()) return;
        
        singleValueLabel.setText("Full or part name of group:");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton12ActionPerformed

    private void jRadioButton15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton15ActionPerformed
        // logout for nntp:
        if(!checkValidConnection()) return;
        
        try {
            ((NNTPClient)tcpClient).logout();
            tcpServerResponse.append(
                ((NNTPClient)tcpClient).getReplyString() + "\r\n");
            handleDisconnect();            
        }catch(IOException ioex) { showError(ioex.getMessage()); }
    }//GEN-LAST:event_jRadioButton15ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // list all news groups for nntp:
        if(!checkValidConnection()) return;
        
        try{
            NewsgroupInfo[] newsgroups = 
                ((NNTPClient)tcpClient).listNewsgroups();
            tcpServerResponse.append(
                ((NNTPClient)tcpClient).getReplyString() + "\r\n");
            for(int i=0; i<newsgroups.length; i++) {
                tcpServerResponse.append(newsgroups[i].getNewsgroup() + "\r\n");
            }
        }catch(IOException ioex) { showError(ioex.getMessage()); }        
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton11ActionPerformed
        // list new news group for nntp:
        if(!checkValidConnection()) return;
        
        // create a query to find new news groups since yesterday
        GregorianCalendar today = new GregorianCalendar();    
        today.roll(Calendar.DAY_OF_YEAR, false);
        NewGroupsOrNewsQuery query = 
          new NewGroupsOrNewsQuery(today, false);
        
        try{
            NewsgroupInfo[] newsgroups = 
                ((NNTPClient)tcpClient).listNewNewsgroups(query);
            tcpServerResponse.append(
                ((NNTPClient)tcpClient).getReplyString() + "\r\n");
            
            if(newsgroups == null) return;
            for(int i=0; i<newsgroups.length; i++) {
                tcpServerResponse.append(newsgroups[i].getNewsgroup() + "\r\n");                
            }
            if(newsgroups.length == 0)
                tcpServerResponse.append("No new NewsGroups\r\n");            
        }catch(IOException ioex) { showError(ioex.getMessage()); }      
        
    }//GEN-LAST:event_jRadioButton11ActionPerformed

    private void jRadioButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton10ActionPerformed
        // login for nntp:
        if(!checkValidConnection()) return; 
        showDialog(loginDialog);
    }//GEN-LAST:event_jRadioButton10ActionPerformed

    private void jRadioButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton7ActionPerformed
        // Download file for FTP:
        if(!checkValidConnection()) return;
        
        singleValueLabel.setText("Remote file:");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton7ActionPerformed

    private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton6ActionPerformed
        // upload file for FTP:
        if(!checkValidConnection()) return;
        
        singleValueLabel.setText("Full path to local file:");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton6ActionPerformed

    private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed
        // make directory for FTP:
        if(!checkValidConnection()) return;
        
        singleValueLabel.setText("New Directory Name:");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton5ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        // list files for FTP:
        if(!checkValidConnection()) return;
        
        try {
            FTPFile[] fileList = ((FTPClient)tcpClient).listFiles();
            tcpServerResponse.append(
                ((FTPClient)tcpClient).getReplyString() + "\r\n");
            if(fileList == null) return;
            for(int i=0; i<fileList.length; i++) {
              tcpServerResponse.append(fileList[i].toString() + "\r\n");
            }
        }catch(IOException ioex) { showError(ioex.getMessage()); return; }
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Single Value Dialog OK Button
        
        // this method only handles tcp protocols, for udp protocols, another
        // method is used
        if(jTabbedPane1.getSelectedIndex() == 1) { // means we are in udp tab
            udpSingleValOKButton(); 
            return;
        }
        
        String valueText = singleValueField.getText();
        String valueLabel = singleValueLabel.getText();
        int selection = tcpSelectionBox.getSelectedIndex();
        try{
            if(selection == 4) { // means handling FTP
                if("New Directory:".equals(valueLabel)) { 
                    // means CWD
                    ((FTPClient)tcpClient).changeWorkingDirectory(valueText);
                } else if(
                    "New Directory Name:".equals(valueLabel)) {
                        // means make new directory
                        ((FTPClient)tcpClient).makeDirectory(valueText);
                } else if(
                    "Full path to local file:".equals(
                        valueLabel)) { 
                            // means uploading file
                            FileInputStream input = 
                                new FileInputStream(valueText);
                            ((FTPClient)tcpClient).storeFile(
                                new File(valueText).getName(), input);
                            input.close();
                } else if("Remote file:".equals(valueLabel)) { 
                    // means downloading a file
                    FileOutputStream output = 
                        new FileOutputStream(
                            System.getProperty("user.dir") + 
                            System.getProperty("file.separator") + 
                            valueText);
                    ((FTPClient)tcpClient).retrieveFile(valueText, output);
                    output.close();
                }
                tcpServerResponse.append(
                    ((FTPClient)tcpClient).getReplyString() + "\r\n");
            } else if(selection == 5) { // means handling NNTP
                if("Full or part name of group:".equals(valueLabel)) {
                    // means getting new news of a group
                    
                    // create a query to get all new messages in this group
                    // since yesterday
                    GregorianCalendar today = new GregorianCalendar();    
                    today.roll(Calendar.DAY_OF_YEAR, false);
                    NewGroupsOrNewsQuery query = 
                      new NewGroupsOrNewsQuery(today, false);
                    query.addDistribution(valueText);
                    
                    String[] result = 
                        ((NNTPClient)tcpClient).listNewNews(query);
                    
                    tcpServerResponse.append(
                        ((NNTPClient)tcpClient).getReplyString() + "\r\n");
                    
                    // in case the NEWNEWS cmmand is disabled
                    if(result == null)  return;

                    for(int i=0; i<result.length; i++) {
                        tcpServerResponse.append(
                            result[i] + "\r\n");
                    }
                } else if("Enter message id:".equals(valueLabel)) {
                    // means getting a message based on id
                    DotTerminatedMessageReader reader = 
                        (DotTerminatedMessageReader)
                        ((NNTPClient)tcpClient).retrieveArticle(
                        "<" + valueText + ">");
                    tcpServerResponse.append(
                        ((NNTPClient)tcpClient).getReplyString() + "\r\n");                    
                    
                    if(reader == null) return;
                    
                    BufferedReader buffReader = new BufferedReader(reader);
                    String lineRead;
                    while((lineRead = buffReader.readLine()) != null) {
                        tcpServerResponse.append(lineRead + "\r\n");
                    }
                }
            } else if(selection == 6) { // means handling pop3
                if("Enter message number:".equals(valueLabel)) { 
                    // means getting a message based on number
                    DotTerminatedMessageReader reader = 
                        (DotTerminatedMessageReader)                    
                        ((POP3Client)tcpClient).retrieveMessage(
                            Integer.parseInt(valueText));
                    
                    tcpServerResponse.append(
                        ((POP3Client)tcpClient).getReplyString() + 
                        "\r\n");
                    
                    if(reader == null) return;
                    
                    BufferedReader buffReader = new BufferedReader(reader);
                    String lineRead;
                    while((lineRead = buffReader.readLine()) != null) {
                        tcpServerResponse.append(lineRead + "\r\n");
                    }                    
                } else if("Enter message number to delete:".equals(valueLabel)){
                    // means deleting a message based on id
                    ((POP3Client)tcpClient).deleteMessage(
                        Integer.parseInt(valueText));
                    tcpServerResponse.append(
                        ((POP3Client)tcpClient).getReplyString() + 
                        "\r\n");                    
                }
            } else if(selection == 2) { // is echo
                BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                        ((EchoTCPClient)tcpClient).getInputStream()));
                
                PrintWriter writer =
                    new PrintWriter(
                        new OutputStreamWriter(
                            ((EchoTCPClient)tcpClient).getOutputStream()), true);
                
                // write to the server
                writer.println(valueText);
                
                // read from the server
                tcpServerResponse.append(reader.readLine());
                
                // streams are not closed here as there is no provision for the
                // user to send another stream of data. Thus the user has to
                // close the connection, which cleans up open streams.
            } else if(selection == 3) { // is finger
                tcpServerResponse.setText(
                    ((FingerClient)tcpClient).query(false, valueText));
            } else if(selection == 9) { // is whois
                tcpServerResponse.setText(
                    ((WhoisClient)tcpClient).query(valueText));
            }
        }catch(IOException ioex) { showError(ioex.getMessage()); return; }
        finally {
            singleValueField.setText("");
            singleValueDialog.setVisible(false);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void udpSingleValOKButton() {
        // handles udp single value ok button clicks
        String valueText = singleValueField.getText();
        String valueLabel = singleValueLabel.getText();
        
        int selection = udpSelectionBox.getSelectedIndex();
        try{
            if(selection == 2) { // means handling echo
                if("Enter echo data (UDP):".equals(valueLabel)) { 
                    ((EchoUDPClient)udpClient).send(
                        valueText.getBytes(), 
                        InetAddress.getByName(udpRemoteServer));
                    byte[] data = new byte[valueText.length()];
                    
                    ((EchoUDPClient)udpClient).receive(data);
                    udpServerResponse.append(new String(data) + "\r\n");
                }
            } else if(selection == 3) { // handling tftp
                File localFile = new File(valueText);
                if("Enter file path (TFTP-Receive):".equals(valueLabel)) {
                    // means receiving a file via tftp
                    FileOutputStream output = new FileOutputStream(localFile);
                    ((TFTPClient)udpClient).receiveFile(
                        localFile.getName(),
                        TFTPClient.ASCII_MODE,
                        output,
                        InetAddress.getByName(udpRemoteServer));
                    output.close();
                } else if("Enter file path (TFTP-Send):".equals(valueLabel)) {
                    // means sending a file via tftp
                    FileInputStream input = new FileInputStream(localFile);
                    ((TFTPClient)udpClient).sendFile(
                        localFile.getName(),
                        TFTPClient.ASCII_MODE,
                        input,
                        InetAddress.getByName(udpRemoteServer));
                    input.close();
                }
                udpClient.close();
            }
        }catch(IOException ioex) { showError(ioex.getMessage()); return; }
        finally {
            singleValueField.setText("");
            singleValueDialog.setVisible(false);
        }
    }
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        singleValueDialog.setVisible(false);
        
        // if this is any of the following protocols disconnect as well
        int currentSel = tcpSelectionBox.getSelectedIndex();
        int currentUDPSel = udpSelectionBox.getSelectedIndex();
        int currentSelTab = jTabbedPane1.getSelectedIndex();
        try{
            if((currentSel == 2) && (currentSelTab == 0)) { 
                // is Echo for TCP
                handleDisconnect();
            } else if((currentUDPSel == 3) && (currentSelTab == 1)) { 
                // is TFTP for UDP
                handleUDPStop();
            }
        }catch(IOException ioex) {
            System.err.println(ioex.getMessage());
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jRadioButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton8ActionPerformed
        // logout for ftp:
        if(!checkValidConnection()) return;
        try{
            ((FTPClient)tcpClient).logout();
            tcpServerResponse.append(((FTPClient)tcpClient).getReplyString());
            handleDisconnect();
        }catch(IOException ioex) { showError(ioex.getMessage()); }
    }//GEN-LAST:event_jRadioButton8ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // login handling code for ftp, nntp, pop3:
        String username = loginUsername.getText();
        String password = loginPassword.getText();

        loginDialog.setVisible(false);
        try {
            int currentSel = tcpSelectionBox.getSelectedIndex();
            if(currentSel == 4) { // is FTP login
                ((FTPClient)tcpClient).login(username, password);
                tcpServerResponse.append(
                    ((FTPClient)tcpClient).getReplyString());
            } else if(currentSel == 5) { // is NNTP login
                ((NNTPClient)tcpClient).authenticate(username, password);
                tcpServerResponse.append(
                    ((NNTPClient)tcpClient).getReplyString());
            } else if(currentSel == 6) { // is POP3 login
                ((POP3Client)tcpClient).login(username, password);
                tcpServerResponse.append(
                    ((POP3Client)tcpClient).getReplyString());
            }
        }catch(IOException ioex) { showError(ioex.getMessage()); }
        finally{ loginUsername.setText(""); loginPassword.setText(""); }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Add your handling code here:
        loginDialog.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
        // ftp login
        if(!checkValidConnection()) return; 
        showDialog(loginDialog);
    }//GEN-LAST:event_jRadioButton4ActionPerformed

    private boolean checkValidConnection() {
        if(!connected) { showError("Please connect first!"); return false; }
        return true;
    }
    
    private void commandBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_commandBoxActionPerformed
        
        String commandString = commandBox.getText();
        if(commandString == null || commandString.length() == 0) return;
        try{
            ProtocolHandler.handleTCPProtocol(
                tcpSelectionBox.getSelectedIndex(), 
                tcpClient, this, commandString);
        }catch(IOException ioex) { showError(ioex.getMessage()); }
        finally{ commandBox.setText(""); }
    }//GEN-LAST:event_commandBoxActionPerformed

    /**
     * This code handles the action events for the Connect button 
     */
    private void tcpConnectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tcpConnectButtonActionPerformed

        // if already connected, simply disconnect
        if(connected) {
            try {
                handleDisconnect();
            }catch(IOException e) {
                showError(e.getMessage());
            }
            return;
        }
        
        // if not connected, try and make a connection        
        String server  = serverNameField.getText();
        String port    = portNumberField.getText();
        int portNumber;
        
        // check if the port number is a valid number
        try{
          portNumber = Integer.parseInt(port);
        }catch(NumberFormatException nfe){
            showError("Invalid port number: " + port);
            return;
        }
        
        // some basic checking of data
        if(server == null || server.trim().length() == 0 ||
           portNumber < 1){
          showError("Invalid connection attributes");
          return;
        }
        
        // now using the factory create an instance of the client
        // and connect
        try{
            tcpClient = 
              ClientFactory.getTCPClientInstance(
                  tcpSelectionBox.getSelectedIndex());
            tcpClient.connect(server, portNumber);
        }catch(SocketException se){
            showError(se.getMessage());
            return;
        }catch(IOException ioex){
            showError(ioex.getMessage());
            return;
        }
        
        // if we come here means we have connected
        connected = true;
        tcpConnectButton.setText("Disconnect");
        tcpSelectionBox.setEnabled(false);
        jTabbedPane1.setEnabledAt(1, false);
        
        try {
            ProtocolHandler.handleTCPProtocol(
                tcpSelectionBox.getSelectedIndex(), 
                tcpClient, 
                this);
        }catch (IOException io) {
            showError(io.getMessage());
            io.printStackTrace();
        }

    }//GEN-LAST:event_tcpConnectButtonActionPerformed

    private void udpSelectionBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_udpSelectionBoxActionPerformed
        // Add your handling code here:
        JComboBox box = (JComboBox)evt.getSource();
        int selectedProtocolIdx = box.getSelectedIndex();
        
        udpCommandPanel.removeAll();
        
        if(selectedProtocolIdx == 3){ // is TFTP
            udpCommandPanel.add(tftpPanel);
        }else{
            udpCommandPanel.add(udpStartButton);
        }
        
        repaint(); 
    }//GEN-LAST:event_udpSelectionBoxActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // change working directory for FTP
        if(!checkValidConnection()) return;
        
        singleValueLabel.setText("New Directory:");
        showDialog(singleValueDialog);
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void tcpSelectionBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tcpSelectionBoxActionPerformed
        // Add your handling code here:
        // get the protocol selected
        JComboBox box = (JComboBox)evt.getSource();
        int selectedProtocolIdx = box.getSelectedIndex();
        updateTCPPanels(selectedProtocolIdx);
    }//GEN-LAST:event_tcpSelectionBoxActionPerformed
    
    private void updateTCPPanels(int index){        
        // based on what protocol has been selected,
        // change the available commands panel
        presetPanel.removeAll();
        
        // this local boolean indicates if the protocol currently being
        // used requires a different command panel or not
        boolean isPanelDifferent = false;
        
        switch(index){
            case 4: {
                presetPanel.add(ftpPanel);
                isPanelDifferent = true;
                portNumberField.setText("21");
                break;
            }// ftp
            case 5: {
                presetPanel.add(nntpPanel);
                isPanelDifferent = true;
                portNumberField.setText("119");
                break;
            } // nntp
            case 6: {
                presetPanel.add(pop3Panel);
                isPanelDifferent = true;
                portNumberField.setText("110");
                break;
            }
            case 7: {
                presetPanel.add(smtpPanel);
                isPanelDifferent = true;
                portNumberField.setText("25");
                break;
            } // smtp
            case 0: {
                portNumberField.setText("19");
                break;
            } // chargen
            case 1: {
                portNumberField.setText("13");
                break;
            } // daytime
            case 2: {
                portNumberField.setText("7");                
                break;
            } // echo
            case 3: {
                portNumberField.setText("79");
                break;
            } // finger
            case 8: {
                portNumberField.setText("37");
                break;
            } // time
            case 9: {
                portNumberField.setText("43");
                break;
            } // Whois
        }
        
        if(isPanelDifferent) {
            commandBox.setEnabled(true);
        } else {
            commandBox.setEnabled(false);
            presetPanel.add(defaultPanel);
        }
        
        repaint();
    }
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new ProtocolRunner().show();
    }
    
    private void showError(String msg){
        JOptionPane.showMessageDialog(
          this, msg, "Error", JOptionPane.ERROR_MESSAGE);
        try {
            if(connected) handleDisconnect();
        }catch(IOException ioex) { System.err.println(ioex.getMessage()); }
    }
    
    void showDialog(JDialog dialog) {
        dialog.pack();
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }
    
    final void handleUDPStop() throws IOException {
        udpClient.close();
        started = false;
        udpServerResponse.setText("");
        udpStartButton.setText("Start");
        udpSelectionBox.setEnabled(true);
        jTabbedPane1.setEnabledAt(0, true);
        return;
    }
    
    final void handleDisconnect() throws IOException {
        tcpClient.disconnect();
        tcpServerResponse.setText("");
        connected = false;
        tcpConnectButton.setText("Connect");
        tcpSelectionBox.setEnabled(true);
        jTabbedPane1.setEnabledAt(1, true);
        return;
    }
    
    final JTextArea getTCPServerResponse() {
        return this.tcpServerResponse;
    }

    final JTextArea getUDPServerResponse() {
        return this.udpServerResponse;
    }
    
    final JDialog getSingleValueDialog() {
        return this.singleValueDialog;
    }

    final JLabel getSingleValueLabel() {
        return this.singleValueLabel;
    }    
    
    final JTextField getSingleValueText() {
        return this.singleValueField;
    }

    final String getUDPRemoteServer() {
        return this.udpRemoteServer;
    }
    
    // non GUI variables
    // identifies if a connection is made for tcp
    private boolean connected;
    
    // identifies if a udp session is started
    private boolean started;   
    
    // the remote udp server
    private String udpRemoteServer;
    
    // the current client for tcp connections
    SocketClient tcpClient;
    
    // the current client for udp connections
    DatagramSocketClient udpClient;
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField commandBox;
    private javax.swing.JPanel defaultPanel;
    private javax.swing.JTextField fromField;
    private javax.swing.ButtonGroup ftpButtonGroup;
    private javax.swing.JPanel ftpPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel17;
    private javax.swing.JPanel jPanel18;
    private javax.swing.JPanel jPanel19;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton10;
    private javax.swing.JRadioButton jRadioButton11;
    private javax.swing.JRadioButton jRadioButton12;
    private javax.swing.JRadioButton jRadioButton13;
    private javax.swing.JRadioButton jRadioButton14;
    private javax.swing.JRadioButton jRadioButton15;
    private javax.swing.JRadioButton jRadioButton16;
    private javax.swing.JRadioButton jRadioButton17;
    private javax.swing.JRadioButton jRadioButton18;
    private javax.swing.JRadioButton jRadioButton19;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton20;
    private javax.swing.JRadioButton jRadioButton21;
    private javax.swing.JRadioButton jRadioButton22;
    private javax.swing.JRadioButton jRadioButton23;
    private javax.swing.JRadioButton jRadioButton24;
    private javax.swing.JRadioButton jRadioButton25;
    private javax.swing.JRadioButton jRadioButton26;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JRadioButton jRadioButton7;
    private javax.swing.JRadioButton jRadioButton8;
    private javax.swing.JRadioButton jRadioButton9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JDialog loginDialog;
    private javax.swing.JPasswordField loginPassword;
    private javax.swing.JTextField loginUsername;
    private javax.swing.JTextArea messageArea;
    private javax.swing.JDialog multipleValueDialog;
    private javax.swing.ButtonGroup nntpButtonGroup;
    private javax.swing.JPanel nntpPanel;
    private javax.swing.ButtonGroup pop3ButtonGroup;
    private javax.swing.JPanel pop3Panel;
    private javax.swing.JTextField portNumberField;
    private javax.swing.JPanel presetPanel;
    private javax.swing.JTextField serverNameField;
    private javax.swing.JDialog singleValueDialog;
    private javax.swing.JTextField singleValueField;
    private javax.swing.JLabel singleValueLabel;
    private javax.swing.ButtonGroup smtpButtonGroup;
    private javax.swing.JPanel smtpPanel;
    private javax.swing.JTextField subjectField;
    private javax.swing.JButton tcpConnectButton;
    private javax.swing.JComboBox tcpSelectionBox;
    private javax.swing.JTextArea tcpServerResponse;
    private javax.swing.JPanel tftpPanel;
    private javax.swing.ButtonGroup tftpPanelButtonGroup;
    private javax.swing.JPanel udpCommandPanel;
    private javax.swing.JTextField udpRemoteServerField;
    private javax.swing.JComboBox udpSelectionBox;
    private javax.swing.JTextArea udpServerResponse;
    private javax.swing.JButton udpStartButton;
    private javax.swing.JTextField variableField;
    private javax.swing.JLabel variableLabel;
    // End of variables declaration//GEN-END:variables
    
}
------------------------------------------------------------------

/*
 * ClientFactory.java
 *
 * Created on December 31, 2003, 12:15 PM
 */

import org.apache.commons.net.*;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.pop3.POP3Client;
import org.apache.commons.net.nntp.NNTPClient;
import org.apache.commons.net.tftp.TFTPClient;

/**
 * Factory for creation of singleton clients.
 * @author  vgoyal
 */
class ClientFactory {
    
    public static SocketClient getTCPClientInstance(int clientType) {
       switch(clientType) {
            case 0: { // is chargen
                if(charGenTCPClient == null) {
                    charGenTCPClient = new CharGenTCPClient();
                }
                return charGenTCPClient;
            }
           case 1: { // is daytime 
               if(daytimeTCPClient == null) {
                   daytimeTCPClient = new DaytimeTCPClient();
               }
               return daytimeTCPClient;
           }
           case 2: { // is echo
               if(echoTCPClient == null) {
                   echoTCPClient = new EchoTCPClient();
               }
               return echoTCPClient;
           }
           case 3: { // is finger
               if(fingerClient == null) {
                   fingerClient = new FingerClient();
               }
               return fingerClient;
           }
           case 4: { // is ftp
               if(ftpClient == null) {
                   ftpClient = new FTPClient();
               }
               return ftpClient;
           }
           case 5: { // is nntp
               if(nntpClient == null) {
                   nntpClient = new NNTPClient();
               }
               return nntpClient;
           }
           case 6: { // is pop3
               if(pop3Client == null) {
                   pop3Client = new POP3Client();
               }
               return pop3Client;
           }
           case 7: { // is smtp
               if(smtpClient == null) {
                   smtpClient = new SMTPClient();
               }
               return smtpClient;
           }
           case 8: { // is time
               if(timeTCPClient == null) {
                   timeTCPClient = new TimeTCPClient();
               }
               return timeTCPClient;
           }
           case 9: { // is whois
               if(whoisClient == null) {
                   whoisClient = new WhoisClient();
               }
               return whoisClient;
           }
        }
        return null;
    }
    
    public static DatagramSocketClient getUDPClientInstance(int clientType) {
       switch(clientType) {
            case 0: { // is chargen
                if(charGenUDPClient == null) {
                    charGenUDPClient = new CharGenUDPClient();
                }
                return charGenUDPClient;
            }
           case 1: { // is daytime
               if(daytimeUDPClient == null) {
                   daytimeUDPClient = new DaytimeUDPClient();
               }
               return daytimeUDPClient;
           }
           case 2: { // is echo
               if(echoUDPClient == null) {
                   echoUDPClient = new EchoUDPClient();
               }
               return echoUDPClient;
           }
           case 3: { // is tftp
               if(tftpClient == null) {
                   tftpClient = new TFTPClient();
               }
               return tftpClient;
           }
           case 4: { // is time
               if(timeUDPClient == null) {
                   timeUDPClient = new TimeUDPClient();
               }
               return timeUDPClient;
           }
        }
       return null;
    }
    
    // TCP clients
    private static CharGenTCPClient charGenTCPClient;
    private static DaytimeTCPClient daytimeTCPClient;
    private static EchoTCPClient    echoTCPClient;
    private static FingerClient     fingerClient;
    private static FTPClient        ftpClient;
    private static NNTPClient       nntpClient;
    private static POP3Client       pop3Client;
    private static SMTPClient       smtpClient;
    private static TimeTCPClient    timeTCPClient;
    private static WhoisClient      whoisClient;
    
    // UDP clients
    private static CharGenUDPClient charGenUDPClient;
    private static DaytimeUDPClient daytimeUDPClient;
    private static EchoUDPClient    echoUDPClient;
    private static TFTPClient       tftpClient;
    private static TimeUDPClient    timeUDPClient;
}

-----------------------------------------------------------
/*
 * ProtocolHandler.java
 *
 * Created on December 28, 2003, 1:05 PM
 */

import org.apache.commons.net.*;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.nntp.*;
import org.apache.commons.net.pop3.*;
import org.apache.commons.net.smtp.*;
import org.apache.commons.net.tftp.*;

import java.util.Date;
import java.io.InputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * This class handles protocol commands that are either typed in or do not 
 * require much manual intervention.
 * @author  vgoyal
 */
public class ProtocolHandler {
    
    public static void handleTCPProtocol(
        int protocolId, SocketClient client, 
        ProtocolRunner runner, String commandString) 
        throws IOException {
        
        switch(protocolId) {
            case 0: { // is chargen
                handleTCPChargen((CharGenTCPClient)client, runner);
                return;
            }
            case 1: { // is daytime
                handleTCPDaytime((DaytimeTCPClient)client, runner);
                return;
            }
            case 2: { // is echo
                handleTCPEcho((EchoTCPClient)client, runner);
                return;
            }  
            case 3: { // is finger
                handleFinger((FingerClient)client, runner);
                return;
            }              
            case 4: { // is ftp
                handleFTP((FTPClient)client, runner, commandString);
                return;
            }
            case 5: { // is nntp
                handleNNTP((NNTPClient)client, runner, commandString);
                return;
            }
            case 6: { // is pop3
                handlePOP3((POP3Client)client, runner, commandString);
                return;
            }
            case 7: { // is smtp
                handleSMTP((SMTPClient)client, runner, commandString);
                return;
            }
            case 8: { // is Time
                handleTCPTime((TimeTCPClient)client, runner);
                return;
            }
            case 9: { // is Whois
                handleWhois((WhoisClient)client, runner);
                return;
            }
        }
    }    

    public static void handleTCPProtocol(
        int protocolId, SocketClient client, ProtocolRunner runner)  
        throws IOException {
            handleTCPProtocol(protocolId, client, runner, null);
    }

    
    public static void handleUDPProtocol(
        int protocolId, DatagramSocketClient client, ProtocolRunner runner)
        throws IOException {

        switch(protocolId) {
            case 0: { // is chargen
                handleUDPChargen((CharGenUDPClient)client, runner);
                return;
            }
            case 1: { // is daytime
                handleUDPDaytime((DaytimeUDPClient)client, runner);
                return;
            }
            case 2: { // is echo
                handleUDPEcho((EchoUDPClient)client, runner);
                return;
            }
            case 4: { // is time
                handleUDPTime((TimeUDPClient)client, runner);
                return;
            }
        }            
    }    
    
    private static void handleUDPChargen(
        CharGenUDPClient client, ProtocolRunner runner) 
        throws IOException {
            
        int packets = 500;        
        while(packets-- > 0) {
            client.send(InetAddress.getByName(runner.getUDPRemoteServer()));
            runner.getUDPServerResponse().append(new String(client.receive()));
        }
    }

    private static void handleUDPDaytime(
        DaytimeUDPClient client, ProtocolRunner runner) 
        throws IOException {            
            runner.getUDPServerResponse().append(
                client.getTime(
                    InetAddress.getByName(runner.getUDPRemoteServer())) 
                    + "\r\n");
    }

    private static void handleUDPEcho(
        EchoUDPClient client, ProtocolRunner runner) 
        throws IOException {
            
        // use the runner to get data from the user to send to echo server
        runner.getSingleValueLabel().setText("Enter echo data (UDP):");
        runner.showDialog(runner.getSingleValueDialog());
    }        
    
    private static void handleUDPTime(
        TimeUDPClient client, ProtocolRunner runner) 
        throws IOException {
            
        long origTime = client.getTime(InetAddress.getByName(
            runner.getUDPRemoteServer()));

        runner.getUDPServerResponse().append(
        "Time returned from server: " +  origTime + "\r\n" +
        "Converted to Date: " + 
        new Date(((origTime) - TimeUDPClient.SECONDS_1900_TO_1970)*1000L));
    }    

    private static void handleTCPChargen(
        CharGenTCPClient client, ProtocolRunner runner) 
        throws IOException {

        // simply grab the resulting inputstream to show the return by the
        // remote server for upto 500 lines
        BufferedReader chargenInput = 
            new BufferedReader(new InputStreamReader(client.getInputStream()));
        
        int lines = 500;
        while(lines-- > 0) {
            runner.getTCPServerResponse().append(
                chargenInput.readLine() + "\r\n");
        }
    }
    
    private static void handleTCPDaytime(
        DaytimeTCPClient client, ProtocolRunner runner) 
        throws IOException {            
            runner.getTCPServerResponse().append(client.getTime() + "\r\n");
    }

    private static void handleTCPTime(
        TimeTCPClient client, ProtocolRunner runner) 
        throws IOException {
            long origTime = client.getTime();
            
            // remember that the long time returned from the server is the 
            // seconds since midnight Jan 1900 and Date in Java is number of
            // seconds since midnight Jan 1970. NET TCPClient provides the time
            // in both formats but you cannot call them both one after another
            // as the internal getDate uses getTime, but the line has already
            // been closed if you have called getTime before.
            runner.getTCPServerResponse().append(
            "Time returned from server: " +  origTime + "\r\n" +
            "Converted to Date: " + 
            new Date(((origTime) - TimeTCPClient.SECONDS_1900_TO_1970)*1000L));
    }

    private static void handleTCPEcho(
        EchoTCPClient client, ProtocolRunner runner) 
        throws IOException {
            
        // use the runner to get data from the user to send to echo server
        runner.getSingleValueLabel().setText("Enter echo data:");
        runner.showDialog(runner.getSingleValueDialog());
    }    
    
    private static void handleFinger(
        FingerClient client, ProtocolRunner runner) 
        throws IOException {
            
        // use the runner to get data from the user to send to echo server
        runner.getSingleValueLabel().setText("Enter query:");
        runner.showDialog(runner.getSingleValueDialog());
    }

    private static void handleWhois(
        WhoisClient client, ProtocolRunner runner) 
        throws IOException {
            
        // use the runner to get data from the user to send to echo server
        runner.getSingleValueLabel().setText("Enter Whois query:");
        runner.showDialog(runner.getSingleValueDialog());
    }    
    
    private static void handleFTP(
        FTPClient client, 
        ProtocolRunner runner, 
        String commandString) 
        throws IOException {
        
        if(commandString == null) { // means just connected        
            
            // check if the server response was valid
            if(!FTPReply.isPositiveCompletion(client.getReplyCode())) {
                runner.handleDisconnect();
                return;
            }
            
        } else { // need to handle a command
            client.sendCommand(commandString);
        }
        
        runner.getTCPServerResponse().append(client.getReplyString());
    }

    private static void handleNNTP(
        NNTPClient client, 
        ProtocolRunner runner, 
        String commandString) 
        throws IOException {
        
        if(commandString == null) { // means just connected        
        
            // check if the server response was valid
            if(!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
                runner.handleDisconnect();
                return;
            }
            
        } else { // need to handle a command
            client.sendCommand(commandString);
        }
        
        runner.getTCPServerResponse().append(client.getReplyString());
    }

    private static void handlePOP3(
        POP3Client client, 
        ProtocolRunner runner, 
        String commandString) 
        throws IOException {
        
        if(commandString == null) { // means just connected 
                return;            
        } else { // need to handle a command
            client.sendCommand(commandString);
        }
        
        runner.getTCPServerResponse().append(client.getReplyString());
    }

    private static void handleSMTP(
        SMTPClient client, 
        ProtocolRunner runner, 
        String commandString) 
        throws IOException {
        
        if(commandString == null) { // means just connected 
            // check if the server response was valid
            if(!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
                runner.handleDisconnect();
                return;
            }
        } else { // need to handle a command
            client.sendCommand(commandString);
        }
        
        runner.getTCPServerResponse().append(client.getReplyString());
    } 
    
}



public class MultiProtocolHandler {
}


/*
 * Command.java
 *
 * Created on December 28, 2003, 3:10 PM
 */

import java.util.Vector;
import java.util.Iterator;
import java.lang.IllegalArgumentException;

/**
 * A Command is a protocol independent class that implements commands.
 * A Command may have arguments, like USER for FTP, or it may be standalone
 * as in logout, again for ftp. A command can only be created by calling the
 * Command Constructor with the name of the command. To add arguments to the
 * command, use the addArgument method.
 * @author  vgoyal
 */
public class Command {
    
    private String commandName;
    
    private Vector commandArguments;
    
    /** Creates a new instance of Command */
    public Command(String commandName) {
        if(commandName == null || commandName.length() == 0) {
            throw new IllegalArgumentException("Invalid Command Name");
        }
        
        this.commandName = commandName;
    }
    
    public String getCommandName() {
        return this.commandName;
    }
    
    public void addArgument(String argValue) {
        if(argValue == null || 
           argValue.length() == 0) {
           throw new IllegalArgumentException("Invalid Command Argument");
        }
        
        if(commandArguments == null) commandArguments = new Vector();
        
        commandArguments.add(argValue);
    }
    
    public int getCountCommandArguments() {
        if(commandArguments == null) return 0;
        else return commandArguments.size();
    }
    
    /**
     * Returns space delimited String containing all arguments to this command
     */
    public String getArguments() {
        if(getCountCommandArguments() == 0) return "";
        
        String returnVal = "";
        Iterator itr = commandArguments.iterator();
        while(itr.hasNext()){
            returnVal = returnVal + " " + itr.next();
        }
        
        return returnVal;
    }
    
}

           
       








ProtocolRunner.zip( 281 k)

Related examples in the same category