Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program Description: Write a Client program that communicates with the Server (host) running on io.uwplatt.edu (this is only reachable through my virtual lab) on port

Program Description:

Write a Client program that communicates with the Server (host) running on io.uwplatt.edu (this is only reachable through my virtual lab) on port 5520. I MUST use Java JFrame and meet the requirement described below.

Program Requirement:

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; 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.

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

* A JLabel and a JTextField for enter the Server address; set text to IO.UWPLATT.EDU

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

* Have a Connect/Disconnect JButton, and set text 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.

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

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();

jLabel3 = new javax.swing.JLabel();

jTextField1 = new javax.swing.JTextField();

jButton2 = new javax.swing.JButton();

jLabel4 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Server:");

serverName.setText("io.uwplatt.edu");

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.setEditable(false);

output.setColumns(20);

output.setRows(5);

jScrollPane1.setViewportView(output);

jLabel3.setText("Message to Server:");

jTextField1.setText("jTextField1");

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

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

jTextField1ActionPerformed(evt);

}

});

jButton2.setText("Send");

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

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

jButton2ActionPerformed(evt);

}

});

jLabel4.setText("ClientServer Communication:");

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)

.addGap(140, 140, 140))

.addComponent(jTextField1)

.addGroup(layout.createSequentialGroup()

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

.addComponent(jButton2)

.addComponent(jLabel3)

.addComponent(jLabel4))

.addGap(0, 0, Short.MAX_VALUE)))

.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))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)

.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

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

.addGap(18, 18, 18)

.addComponent(jButton2)

.addGap(33, 33, 33)

.addComponent(jLabel4)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

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

.addContainerGap())

);

pack();

}//

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

// TODO add your handling code here:

}

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

// connect and disconnect button

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 ");

}

}

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

// String messages to be sent with the jButton2 button below.

}

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

// button to send (String message) to the 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 (Instantiation Exception 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.JButton jButton2;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTextField jTextField1;

private javax.swing.JTextArea output;

private javax.swing.JTextField port;

private javax.swing.JTextField serverName;

// End of variables declaration

}

image text in transcribed

.2 e Refactor Run Debug Profile Team Tools Window Help ault confi BClient.java BMyJFrame.java 1 || Source Design History , ' lit-* 9 Select the root node in Navigator to access various useful settings of the form (in Server: io.uwplatt.edu port#: 5520 connect Message to Server TextField1 Send ClientServer Communication Output Program (run) BUILD SUCCESSFUL (EOtal time: 24 seconds) .2 e Refactor Run Debug Profile Team Tools Window Help ault confi BClient.java BMyJFrame.java 1 || Source Design History , ' lit-* 9 Select the root node in Navigator to access various useful settings of the form (in Server: io.uwplatt.edu port#: 5520 connect Message to Server TextField1 Send ClientServer Communication Output Program (run) BUILD SUCCESSFUL (EOtal time: 24 seconds)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Science For Dummies

Authors: Lillian Pierson ,Jake Porway

2nd Edition

1119327636, 978-1119327639

More Books

Students also viewed these Databases questions