Question: import java.io.*; import java.net.*; public class Client extends javax.swing.JFrame { /** * Creates new form Client */ public Client() { initComponents(); } /** * This

import java.io.*;

import java.net.*;

public class Client extends javax.swing.JFrame {

/** * Creates new form Client */

public Client() {

initComponents();

}

/** *

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.

*/

@SuppressWarnings("unchecked")

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

serverName = new javax.swing.JTextField();

jLabel2 = new javax.swing.JLabel();

port = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jScrollPane1 = new javax.swing.JScrollPane();

output = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Server:");

serverName.setText("io.winds-100");

serverName.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

serverNameActionPerformed(evt);

}

});

jLabel2.setText("port#:");

port.setText("5520");

jButton1.setText("connect");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

output.setColumns(20);

output.setRows(5);

jScrollPane1.setViewportView(output);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout);

layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup()

.addGap(23, 23, 23)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1)

.addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel1)

.addComponent(jLabel2))

.addGap(44, 44, 44)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(port)

.addComponent(serverName, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE) .addComponent(jButton1)))

.addContainerGap())

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup()

.addGap(45, 45, 45)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1)

.addComponent(serverName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(23, 23, 23)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton1))

.addGap(32, 32, 32)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(49, Short.MAX_VALUE)) );

pack();

}

private void serverNameActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try

{

Socket mySocket = new Socket (serverName.getText(),

Integer.parseInt(port.getText()));

//System.out.println(mySocket.getPort());

output.append("connected to Server ");

}

catch (IOException e)

{

output.append("Connected to Server ");

}

}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

}

}

} catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); }

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Client().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTextArea output;

private javax.swing.JTextField port;

private javax.swing.JTextField serverName;

// End of variables declaration

}

Using the code above I need help getting started with the following. Program is done on NetBeans

1. The Server is running a proprietary protocol called Knock Knock Protocol. It will only respond when your Client is following the protocol correctly. When your Client is successfully connected to the Server, You must follow the protocol as follows.

* First message sent to the Server must be Knock Knock; this is like saying hello to the Server. The message is not case-sensitive on the Server side. The Server will send whos there? back to the Client, but if you send anything else, the Server simply doesnt understand and will send ???.

* Next, the Client must identify itself to the Server in response to the message whos there?. For example, Banana. After that, the Server will send Banana who? back to the Client.

* When the Server send Banana who? the Client can send a Knock Knock joke to the Server. The Server is not very smart, but she is very kind, so whatever joke you sent, she will always LOL!

* Because the Server is very accommodating, the Client can tell her another Knock Knock joke even if the previous one is not funny. In this case, the Client must send Knock Knock again!

* The Client can send a quit message anytime to disconnect from the Server, who would send a Good Bye! message back to the Client. The quit message is not case-sensitive.

2. Your GUI (JFrame) should include the following Java Swing components.

* A JLabel and a JTextField for entering the Server address; set text to UW-WINDS-100.

* A JLabel and a JTextField for entering the port number, set text to 5520.

* Have a Connect/Disconnect JButton, and set text to Connect. When the Connect button is clicked, connect to the port and server specified. Also, change the text to Disconnect. When Disconnect is clicked, close the connection and change the text back to "Connect. If the connection dies for any reasons, the button must display Connect. And whenever the Client is connected to the Server, the button must display Disconnect. Make sure your Connect/Disconnect button is in the correct state. * A JLabel, JTextField and a Send button for sending a message (String) to the Server.

* A JLabel and a read-only JTextArea (in the Design view in NetBeans, uncheck the editable box) for displaying messages. DO NOT print anything to System.out. Use the append() method of JTextArea to display all messages, including the communications between the Client and the Server, error messages and the connection status (either connected to ., or disconnected). DO NOT CLEAR the messages in the JTextArea. * A sample GUI for the Client is on page 2.

3. Your program must not crash under any situation and must always be in a sane state. You must try-catch everything and print out appropriate error messages identifying the specific exception.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!