The document describes a network programming report on a Java chat application. It includes the code for a Server class that implements a threaded server to handle multiple client connections simultaneously. The server opens a port to listen for clients and spins up a new thread for each accepted connection. It then reads and writes data between the server and client over buffered input/output streams. The report notes issues with messages from one client not being visible to others and connection errors that can occur between the client and server.
5. thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
} else if(ComboBox.getSelectedItem().equals("Server")){
ButtonOn.setText("Off");
getReadConnec();
Thread thread1= new Thread(this);
Thread thread2= new Thread(this);
Thread thread3= new Thread(this);
Thread thread4= new Thread(this);
Thread thread5= new Thread(this);
Thread thread6= new Thread(this);
Thread thread7= new Thread(this);
Thread thread8= new Thread(this);
Thread thread9= new Thread(this);
Thread thread10= new Thread(this);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start();
}else if(ButtonOn.getText().equals("Disconnect")){
getDisconnectedClient();
}else if(ButtonOn.getText().equals("Off")){
getDisconnectedServer();
}
}
private void getSend(){
try {
Server_Writer.write(Username.getText()+ ": "
+TextChat.getText());
Server_Writer.newLine();
Server_Writer.flush();
} catch (IOException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null,
ex);
}
ListChat.add(Username.getText()+ ": " +TextChat.getText());
//jClient.setText(Username.getText());
TextChat.setText("");
}
//Background color
public void getBackgroundColor(){
Color c = JColorChooser.showDialog(null,"Background
Color",jPanel.getBackground());
jPanel.setBackground(c);
6. }
//Konfirmasi keluar
public void getExit(){
int confirm =JOptionPane.showConfirmDialog(this,"Are you sure will
exit this application ?","Exit
Application",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if (confirm == JOptionPane.YES_OPTION){
System.exit(0);
}
}
/**
* 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">//GEN-
BEGIN:initComponents
private void initComponents() {
Dialog = new javax.swing.JDialog();
ColorChooser = new javax.swing.JColorChooser();
jPanel = new javax.swing.JPanel();
ComboBox = new javax.swing.JComboBox();
ButtonOn = new javax.swing.JButton();
Send = new javax.swing.JToggleButton();
JUsername = new javax.swing.JLabel();
Username = new javax.swing.JTextField();
TextChat = new java.awt.TextArea();
ListChat = new java.awt.List();
jMenuBar1 = new javax.swing.JMenuBar();
File = new javax.swing.JMenu();
BackgroundColor = new javax.swing.JMenuItem();
Exit = new javax.swing.JMenuItem();
Help = new javax.swing.JMenu();
About = new javax.swing.JMenuItem();
javax.swing.GroupLayout DialogLayout = new
javax.swing.GroupLayout(Dialog.getContentPane());
Dialog.getContentPane().setLayout(DialogLayout);
DialogLayout.setHorizontalGroup(
DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(DialogLayout.createSequentialGroup()
.addContainerGap()
.addComponent(ColorChooser,
javax.swing.GroupLayout.PREFERRED_SIZE, 601,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
DialogLayout.setVerticalGroup(
DialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
12. try {
ListChat.add(Server_Reader.readLine());
} catch (IOException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
Keterangan:
Masalah dari aplikasi chatting ini adalah apabila client 1 mengirim pesan ke server maka client 2
dan client yang lain nya tidak bisa melihat isi chat tersebut. Selain itu apabila, apabila client
mengirim pesan ke server. Biasa nya terjadi coonection refused : connect yang terjadi
dikaernakan jaringan dan juga server yang tidak menerima client.