Top Banner
PRAKTEK PEMROGRAMAN JARINGAN KOMPUTER PROGRAM MULTICLIENT CHATTING CLIENT-SERVER DENGAN MENGGUNAKAN BAHASA PEMROGRAMAN JAVA BERBASIS GRAFIS Oleh : Nama : Tri Lestari NIM : 061130701310 Kelas : 6 CD JURUSAN TEKNIK KOMPUTER POLITEKNIK NEGERI SRIWIJAYA PALEMBANG 2014
25

Laporan multiclient chatting client server

Dec 18, 2014

Download

Software

trilestari08

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Laporan multiclient chatting client server

PRAKTEK PEMROGRAMAN JARINGAN KOMPUTER

PROGRAM MULTICLIENT CHATTING CLIENT-SERVER

DENGAN MENGGUNAKAN BAHASA PEMROGRAMAN

JAVA BERBASIS GRAFIS

Oleh :

Nama : Tri Lestari

NIM : 061130701310

Kelas : 6 CD

JURUSAN TEKNIK KOMPUTER

POLITEKNIK NEGERI SRIWIJAYA

PALEMBANG

2014

Page 2: Laporan multiclient chatting client server

PROGRAM MULTICLIENT CHATTING CLIENT-SERVER

DENGAN MENGGUNAKAN BAHASA PEMROGRAMAN

JAVA BERBASIS GRAFIS

Program ini merupakan aplikasi multiclient untuk client dan server denga

berbasis grafis. Berikut listing program MultiClient Chatting Client-Server

berbasis grafis (gambar).

A. ChatServer.java

import java.io.*;import java.net.*;import java.util.*;

public class ChatServer {private static int uniqueId;private ArrayList<ChatServer.ClientThread> clients;private int port;private boolean keepGoing;

public ChatServer() {this.port = 9999;clients = new ArrayList();

}

public void start() {keepGoing = true;try {

ServerSocket serverSocket = newServerSocket(port);

while (keepGoing) {

Page 3: Laporan multiclient chatting client server

System.out.println("ChatServer waitingfor Clients on port " + port + ".");

Socket socket = serverSocket.accept();if (!keepGoing) {

break;}ChatServer.ClientThread t = new

ChatServer.ClientThread(socket);clients.add(t);t.start();send("login~" + t.username + "~" +

t.username + " sedang login...~Server~\n");}try {

serverSocket.close();for (int i = 0; i < clients.size();

++i) {ChatServer.ClientThread tc =

clients.get(i);try {

tc.sInput.close();tc.sOutput.close();tc.socket.close();

} catch (IOException ioE) {}

}} catch (Exception e) {

System.out.println("Exception closingthe server and clients: " + e);

}} catch (IOException e) {

Page 4: Laporan multiclient chatting client server

String msg = "Exception on newServerSocket: " + e + "\n";

System.out.println(msg);}

}

private synchronized void send(String message) {for (int i = clients.size(); --i >= 0;) {

ChatServer.ClientThread ct =clients.get(i);

if (!ct.writeMsg(message)) {clients.remove(i);System.out.println("Disconnected Client

" + ct.username + " removed from list.");}

}}

private String getClients() {String s = "";for (ClientThread clientThread : clients) {

s += clientThread.username + ":";}s += "---";System.out.println(s);return s;

}

private synchronized void remove(int id) {for (int i = 0; i < clients.size(); ++i) {

Page 5: Laporan multiclient chatting client server

ChatServer.ClientThread ct =clients.get(i);

if (ct.id == id) {clients.remove(i);return;

}}

}

public static void main(String[] args) {ChatServer server = new ChatServer();server.start();

}

private class ClientThread extends Thread {

private Socket socket;private ObjectInputStream sInput;private ObjectOutputStream sOutput;private int id;private String username;

public ClientThread(Socket socket) {id = ++uniqueId;this.socket = socket;System.out.println("Menciptakan Object

Input/Output Streams");try {

sOutput = newObjectOutputStream(socket.getOutputStream());

Page 6: Laporan multiclient chatting client server

sInput = newObjectInputStream(socket.getInputStream());

String message = (String)sInput.readObject();

username = message.split("~")[1];System.out.println(username + "

masuk.");} catch (IOException e) {

System.out.println("Exception creatingnew Input/output Streams: " + e);

} catch (ClassNotFoundException e) {}

}

@Overridepublic void run() {

boolean keepGoing = true;while (keepGoing) {

String message;try {

message =sInput.readObject().toString();

} catch (IOException e) {System.out.println(username + "

Exception reading Streams: " + e);break;

} catch (ClassNotFoundException e2) {break;

}

Page 7: Laporan multiclient chatting client server

String type = message.split("~")[0];String pengirim =

message.split("~")[1];String text = message.split("~")[2];String kepada = message.split("~")[3];String response;

switch (type) {case "postText":

response = "recieveText~" +pengirim + "~" + text + "~" + kepada + "~\n";

send(response);break;

case "postPrivateText":response =

"recievePrivateText~" + pengirim + "~" + text + "~" +kepada + "~\n";

send(response);break;

case "login":response = "login~" + pengirim

+ "~" + text + "~" + kepada + "~\n";send(response);break;

case "logout":response = "logout~" + pengirim

+ "~" + text + "~" + kepada + "~\n";send(response);break;

case "list":

Page 8: Laporan multiclient chatting client server

response = "list~server~" +getClients() + "~ ~ ~ ~ ~\n";

send(response);break;

}}

remove(id);close();

}

private void close() {try {

if (sOutput != null) {sOutput.close();

}} catch (Exception e) {}try {

if (sInput != null) {sInput.close();

}} catch (Exception e) {}try {

if (socket != null) {socket.close();

}} catch (Exception e) {}

}

Page 9: Laporan multiclient chatting client server

private boolean writeMsg(String msg) {if (!socket.isConnected()) {

close();return false;

}try {

sOutput.writeObject(msg);} catch (IOException e) {

System.out.println("Error sendingmessage to " + username);

System.out.println(e.toString());}return true;

}}

}

B. ChatClient.java

import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.Socket;import java.util.ArrayList;import java.util.List;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.ImageIcon;import javax.swing.JLabel;import javax.swing.table.DefaultTableModel;

Page 10: Laporan multiclient chatting client server

public class ChatClient extends javax.swing.JFrame {

/*** Creates new form ChatClient*/private ObjectInputStream input;private ObjectOutputStream output;private Socket socket;private String server, username;private int port;private List<String> clients;

public ChatClient() {clients = new ArrayList();initComponents();

}

public boolean start() {try {

socket = new Socket(server, port);} catch (Exception ec) {

System.out.println("Error connectiong toserver:" + ec);

return false;}

String msg = "Connection accepted " +socket.getInetAddress() + ":" + socket.getPort();

System.out.println(msg);

Page 11: Laporan multiclient chatting client server

try {input = new

ObjectInputStream(socket.getInputStream());output = new

ObjectOutputStream(socket.getOutputStream());} catch (IOException eIO) {

System.out.println("Exception creating newInput/output Streams: " + eIO);

return false;}

new ChatClient.ListenFromServer().start();

try {output.writeObject("login~" + username +

"~" + username + " sedang login...~server~\n");output.writeObject("list~" + username + "~"

+ username + " sedang login...~server~\n");

} catch (IOException eIO) {System.out.println("Exception doing login :

" + eIO);disconnect();return false;

}

return true;}

private void disconnect() {try {

Page 12: Laporan multiclient chatting client server

// TODO add your handling code here:output.writeObject("logout~" + username +

"~" + username + " sudah logout...~Server~\n");} catch (IOException ex) {

//Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);

}

try {if (input != null) {

input.close();}

} catch (Exception e) {}try {

if (output != null) {output.close();

}} catch (Exception e) {}try {

if (socket != null) {socket.close();

}} catch (Exception e) {}

}

/**

Page 13: Laporan multiclient chatting client server

* This method is called from within theconstructor to initialize the form.

* WARNING: Do NOT modify this code. The content ofthis method is always

* regenerated by the Form Editor.*/@SuppressWarnings("unchecked")// <editor-fold defaultstate="collapsed"

desc="Generated Code">private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();viewTextArea = new javax.swing.JTextArea();jScrollPane2 = new javax.swing.JScrollPane();clientTable = new javax.swing.JTable();postTextField = new javax.swing.JTextField();kirimButton = new javax.swing.JButton();lbljpg = new javax.swing.JLabel(new

ImageIcon("E:/rara.jpg"));jLabel2 = new javax.swing.JLabel();serverTextField = new javax.swing.JTextField();jLabel3 = new javax.swing.JLabel();portTextField = new javax.swing.JTextField();masukButton = new javax.swing.JButton();jLabel4 = new javax.swing.JLabel();usernameTextField = new

javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

Page 14: Laporan multiclient chatting client server

viewTextArea.setEditable(false);viewTextArea.setColumns(20);viewTextArea.setLineWrap(true);viewTextArea.setRows(5);viewTextArea.setFocusable(false);jScrollPane1.setViewportView(viewTextArea);

jScrollPane2.setViewportView(clientTable);

postTextField.addActionListener(newjava.awt.event.ActionListener() {

public voidactionPerformed(java.awt.event.ActionEvent evt) {

postTextFieldActionPerformed(evt);}

});

kirimButton.setText("Kirim");kirimButton.addActionListener(new

java.awt.event.ActionListener() {public void

actionPerformed(java.awt.event.ActionEvent evt) {kirimButtonActionPerformed(evt);

}});

jLabel2.setText("Server");

serverTextField.setText("10.17.0.0");

Page 15: Laporan multiclient chatting client server

jLabel3.setText("Port");

portTextField.setText("9999");

masukButton.setText("Masuk");masukButton.addActionListener(new

java.awt.event.ActionListener() {public void

actionPerformed(java.awt.event.ActionEvent evt) {masukButtonActionPerformed(evt);

}});

jLabel4.setText("Username");

usernameTextField.setText("Rara Ariesta");

javax.swing.GroupLayout layout = newjavax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);layout.setHorizontalGroup(

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

.addGroup(layout.createSequentialGroup().addContainerGap()

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

.addGroup(layout.createSequentialGroup()

Page 16: Laporan multiclient chatting client server

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

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()

.addComponent(postTextField)

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

.addComponent(kirimButton))

.addComponent(jScrollPane1))

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

.addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED_SIZE, 259,javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup().addComponent(lbljpg).addComponent(jLabel2)

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

.addComponent(serverTextField,javax.swing.GroupLayout.PREFERRED_SIZE, 167,javax.swing.GroupLayout.PREFERRED_SIZE)

Page 17: Laporan multiclient chatting client server

.addGap(18, 18, 18)

.addComponent(jLabel3)

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

.addComponent(portTextField,javax.swing.GroupLayout.PREFERRED_SIZE, 46,javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(jLabel4)

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

.addComponent(usernameTextField,javax.swing.GroupLayout.DEFAULT_SIZE, 194,Short.MAX_VALUE)

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

.addComponent(masukButton))).addContainerGap())

);layout.setVerticalGroup(

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

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()

.addContainerGap()

Page 18: Laporan multiclient chatting client server

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

.addComponent(lbljpg)

.addComponent(jLabel2)

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

.addComponent(jLabel3)

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

.addComponent(masukButton)

.addComponent(jLabel4)

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

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

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

.addComponent(jScrollPane2,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT_SIZE, 427,Short.MAX_VALUE)

Page 19: Laporan multiclient chatting client server

.addGroup(layout.createSequentialGroup().addComponent(jScrollPane1)

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

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

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

.addComponent(kirimButton)))).addContainerGap())

);

pack();}// </editor-fold>

private voidmasukButtonActionPerformed(java.awt.event.ActionEventevt) {

// TODO add your handling code here:this.server = serverTextField.getText();this.port = new

Integer(portTextField.getText());this.username = usernameTextField.getText();start();

Page 20: Laporan multiclient chatting client server

}

private voidkirimButtonActionPerformed(java.awt.event.ActionEventevt) {

// TODO add your handling code here:try {

String message = "postText~" + username +"~" + postTextField.getText() + "~all~\n";

output.writeObject(message);postTextField.setText("");

} catch (IOException ex) {

Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);

}}

private voidpostTextFieldActionPerformed(java.awt.event.ActionEventevt) {

// TODO add your handling code here:kirimButtonActionPerformed(evt);

}

/*** @param args the command line arguments*/public static void main(String args[]) {

/* Set the Nimbus look and feel */

Page 21: Laporan multiclient chatting client server

//<editor-fold defaultstate="collapsed" desc="Look and feel setting code (optional) ">

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

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

*/try {

for (javax.swing.UIManager.LookAndFeelInfoinfo :javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;}

}} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

Page 22: Laporan multiclient chatting client server

} catch(javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(ChatClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}//</editor-fold>

/* Create and display the form */java.awt.EventQueue.invokeLater(new Runnable()

{@Overridepublic void run() {

new ChatClient().setVisible(true);}

});}// Variables declaration - do not modifyprivate javax.swing.JTable clientTable;private javax.swing.JLabel jLabel2;private javax.swing.JLabel jLabel3;private javax.swing.JLabel jLabel4;private javax.swing.JScrollPane jScrollPane1;private javax.swing.JScrollPane jScrollPane2;private javax.swing.JButton kirimButton;private javax.swing.JButton masukButton;private javax.swing.JTextField portTextField;private javax.swing.JTextField postTextField;private javax.swing.JTextField serverTextField;private javax.swing.JTextField usernameTextField;

Page 23: Laporan multiclient chatting client server

private javax.swing.JTextArea viewTextArea;private JLabel lbljpg ;// End of variables declaration

class ListenFromServer extends Thread {

@Overridepublic void run() {

while (true) {try {

String msg = (String)input.readObject();

String res;String type = msg.split("~")[0];String pengirim =

msg.split("~")[1];String text = msg.split("~")[2];String kepada = msg.split("~")[3];switch (type) {

case "recieveText":res = pengirim + ": " +

text;

viewTextArea.setText(viewTextArea.getText() + res +"\n");

break;case "recievePrivateText":

res = pengirim + ": " +text;

if(kepada.equals(username)) {

Page 24: Laporan multiclient chatting client server

viewTextArea.setText(viewTextArea.getText() + res +"\n");

}break;

case "login":

viewTextArea.setText(viewTextArea.getText() + pengirim+ " sudah login..." + "\n");

clients.add(pengirim);break;

case "logout":

viewTextArea.setText(viewTextArea.getText() + pengirim+ " telah logout..." + "\n");

clients.remove(pengirim);break;

case "list":setTable(text);break;

}} catch (IOException e) {

System.out.println("Server hasclose the connection: " + e);

break;} catch (ClassNotFoundException e2) {}

}}

private void setTable(String text) {

Page 25: Laporan multiclient chatting client server

int rows = text.split(":").length - 1;Object[][] data = new Object[rows][1];for (int i = 0; i < rows; i++) {

String t = text.split(":")[i];data[i][0] = t;

}

String[] header = {"Clients"};

clientTable.setModel(newDefaultTableModel(data, header));

}}

}Pastikan kita sudah terhubung ke internet. Hasil compile dari program ini

adalah berikut: