import java.util.*;
import java.io.*;
import java.net.*;
public class clientinterface extends javax.swing.JFrame {
public String input;
/** Creates new form clientinterface */
public clientinterface() throws Exception {
initComponents();
jTextArea1.setEditable(false);
Socket s = new Socket("127.0.0.1", 9000);
OutputStream os= s.getOutputStream();
//to write string,otherwise, need towrite byte array
DataOutputStream serverWriter = new DataOutputStream(os);
// The next 2 lines create a buffer reader that
// reads from the standard input. (to read stream FROM SERVER)
InputStreamReader isrServer = new InputStreamReader(s.getInputStream());
BufferedReader serverReader = new BufferedReader(isrServer);
//Read welcome message from server. 1 line only
String welcome = serverReader.readLine();
jTextArea1.append(welcome);
//create buffer reader to read input from user. Read the user input to string 'sentence'
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
String sentence;
//Read user input command.
jTextArea1.append("\nUser input :");
/*
String repeat = "";
while (true) {
if(!input.equals(null)) {
jTextArea1.append("\n" + input);
sentence = input;
input = null;
break;
}
}
// keep repeating until an empty line is read.
while (sentence.compareTo("") != 0) {
// Send a user input to server
serverWriter.writeBytes(sentence +"\n");
// Read server's reply below and output to screen.
String response = serverReader.readLine();
//Read all lines of the output
while (response.compareTo("") != 0) {
jTextArea1.append("\n" + response);
response = serverReader.readLine();
}
//read user input again
jTextArea1.append("\nUser input :");
sentence = jTextField1.getText();
}
// Send an empty line to server to end communication.
serverWriter.writeBytes("\n");
s.close();
*/}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jDialog1 = new javax.swing.JDialog();
jDialog2 = new javax.swing.JDialog();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jToggleButton3 = new javax.swing.JToggleButton();
jPanel2 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jToggleButton1 = new javax.swing.JToggleButton();
jLabel1 = new javax.swing.JLabel();
jToggleButton2 = new javax.swing.JToggleButton();
javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1Layout.setHorizontalGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jDialog1Layout.setVerticalGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane());
jDialog2.getContentPane().setLayout(jDialog2Layout);
jDialog2Layout.setHorizontalGroup(
jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jDialog2Layout.setVerticalGroup(
jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "WZ & YN Messenger Service", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jToggleButton3.setText("Clear Text");
jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
.addComponent(jToggleButton3, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jToggleButton3)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Input", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jToggleButton1.setText("Send");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel1.setText("Key in your commands here");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel1))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
jToggleButton2.setText("Disconnect/Exit");
jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton2))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jToggleButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText("");
}
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String in;
String newLine = "\n";
//Get input and send it, to be displayed on the text area
in = jTextField1.getText();
jTextField1.setText("");
jTextArea1.append(in + newLine);
input = in;
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
String in;
String newLine = "\n";
//Get input and send it, to be displayed on the text area
in = jTextField1.getText();
jTextField1.setText("");
jTextArea1.append(in + newLine);
input = in;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new clientinterface().setVisible(true);
} catch (Exception e) {}
}
});
}
// Variables declaration - do not modify
private javax.swing.JDialog jDialog1;
private javax.swing.JDialog jDialog2;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JToggleButton jToggleButton2;
private javax.swing.JToggleButton jToggleButton3;
// End of variables declaration
}
import java.util.*;
import java.io.*;
import java.net.*;
class TCPServer {
public static void main (String args[]) throws Exception {
// throws Exception here because don't want to deal
// with errors in the rest of the code for simplicity.
// (Note: NOT a good practice
//Create 10 persons to simulate a database of 10 registered users
person[] users = new person[10];
users[0] = new person("Pauline");
users[1] = new person("Dave");
users[2] = new person("Sheila");
users[3] = new person("Jasmine");
users[4] = new person("Priscilla");
users[5] = new person("Tom");
users[6] = new person("Jerry");
users[7] = new person("Heng");
users[8] = new person("Phoebe");
users[9] = new person("Luci");
//Welcome socket ---- SOCKET 1
ServerSocket serverSocket = new ServerSocket(9000);
person current = null;
// waits for a new connection. Accepts connection from multiple clients
while (true) {
System.out.println("waiting for connection at 9000");
//Connection socket --- SOCKET 2
Socket s = serverSocket.accept();
System.out.println("connection established from " + s.getInetAddress());
//create output stream to write to/send TO CLIENT
DataOutputStream output = new DataOutputStream(s.getOutputStream());
//Welcome Message
output.writeBytes("Welcome to WZ & YN MESSENGER v1 Today is 29 October"+ "\n");
// create a BufferedReader object to read strings from
// the socket. (read strings FROM CLIENT)
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String input = br.readLine();
// keep repeating until an empty line is read.
while (input.compareTo("") != 0) {
//Wrong command no '/' infront
if (input.charAt(0)!='/') {
output.writeBytes("Wrong Command."+ "\n");
output.writeBytes("Type /command to see the available commands." + "\n" + "\n");
input = br.readLine();
}
//Commands with '/' infront
else {
//When /command is executed.
if (input.substring(1, input.length()).equals("command")) {
output.writeBytes("/command : Look at all available commands." + "\n");
output.writeBytes("/signin : Sign in with your nick." + "\n");
output.writeBytes("/chat : Chat with the person with given nick." + "\n");
output.writeBytes("/endchat : End current chat session." + "\n");
output.writeBytes("/friends : List down all your friends" + "\n");
output.writeBytes("/add : Add friend." + "\n");
output.writeBytes("/remove : Remove friend." + "\n");
output.writeBytes("/quit : Disconnect from server." + "\n" +"\n");
input = br.readLine();
}
//When /signin is executed.
else if (input.substring(1, input.length()).equals("signin")) {
output.writeBytes("Enter your name :" + "\n" +"\n");
String name;
name = br.readLine();
for (int i = 0; i < 10; i++)
if (users.getName().equals(name))
current = users[i];
output.writeBytes("You have successfully logged in as " + current.getName() + "\n" +"\n");
input = br.readLine();
}
//When /add is executed.
else if (input.substring(1, input.length()).equals("add")) {
if (current == null)
output.writeBytes("You have not signed in." + "\n" +"\n");
else {
output.writeBytes("Enter the name of your friend :" + "\n" +"\n");
String friend;
friend = br.readLine();
for (int i = 0; i < 10; i++) {
if (users[i].name.equals(friend)){
current.addFriend(users[i]);
output.writeBytes("Your friend " + friend + "has been added"+ "\n" +"\n");
break;
}
if (!users[i].name.equals(friend) && i == 9)
output.writeBytes("Your friend does not exist in our server." + "\n" +"\n");
}
}
input = br.readLine();
}
/*
else if (input.substring(1, input.length()).equals("friends")) {
listFriends(current, output);
}
*/
else {
output.writeBytes("Wrong Command."+ "\n");
output.writeBytes("Type /command to see the available commands." + "\n" + "\n");
input = br.readLine();
}
}
}
// close current connection
s.close();
}
}The point of these 2 program is to allow client to connect to server using socket class. My problem is regarding my 1st program code. How do I get user keyboard input when prompted by the server? The bulk of my Client code is in the clientinterface() method and I don't know how to allow the input event to happen at that particular time when an input is needed. Run TCPServer and then Clientinterface and you will know what I mean.
Thank you.