Amigos merupakan salah satu aplikasi chatting bersifat multi chat yang dibuat dengan berbasis java dan aplikasi ini dapat dijalankan di PC. Cara menggunakan aplikasi ini yaitu pertama: start server, membuka port untuk client agar dapat connect melalui port tersebut. Client mengisi username, kemudian memilih tombol connect untuk terhubung ke sever. Client lain pun connect dengan cara yang sama. Lalu server mengidentifikasi apakah client-client tersebut berhasil connect atau tidak, server juga dapat melihat siapa saja yang online pada saat itu.
Adapun kelebihan dari aplikasi ini adalah sebagai berikut.
Dapat melakukan chat dimana saja selama ada koneksi internet
Dapat melakukan multi chat, sehingga bisa chatting beramai ramai
Tidak perlu melakukan registrasi akun
Dapat diterapkan pada PC apa saja yang bisa menerima aplikasi Jav
Adapun kekurangan dari aplikasi ini adalah sebagai berikut.
Harus set IP secara manual
Tidak dapat melakukan single chat antar teman
Tidak mempunyai akun yang tetap
Berikut screenshoot program
Chat_client.java
package chat_client;
import java.net.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class client_frame extends javax.swing.JFrame
{
String username, address = "localhost";
ArrayList<String> users = new ArrayList();
int port = 8080;
Boolean isConnected = false;
Socket sock;
BufferedReader reader;
PrintWriter writer;
//--------------------------//
public void ListenThread()
{
Thread IncomingReader = new Thread(new IncomingReader());
IncomingReader.start();
}
//--------------------------//
public void userAdd(String data)
{
users.add(data);
}
//--------------------------//
public void userRemove(String data)
{
ta_chat.append(data + " is now offline.\n");
}
//--------------------------//
public void writeUsers()
{
String[] tempList = new String[(users.size())];
users.toArray(tempList);
for (String token:tempList)
{
//users.append(token + "\n");
}
}
//--------------------------//
public void sendDisconnect()
{
String bye = (username + ": :Disconnect");
}
//--------------------------//
public void Disconnect()
{
try
{
ta_chat.append("Disconnected.\n");
} catch(Exception ex) {
ta_chat.append("Failed to disconnect. \n");
}
isConnected = false;
tf_username.setEditable(true);
}
public client_frame()
{
initComponents();
}
//--------------------------//
public class IncomingReader implements Runnable
{
@Override
public void run()
{
String[] data;
String stream, done = "Done", connect = "Connect", disconnect = "Disconnect", chat = "Chat";
try
{
while ((stream = reader.readLine()) != null)
{
data = stream.split(":");
if (data[2].equals(chat))
{
ta_chat.append(data[0] + ": " + data[1] + "\n");
ta_chat.setCaretPosition(ta_chat.getDocument().getLength());
}
else if (data[2].equals(connect))
{
ta_chat.removeAll();
userAdd(data[0]);
}
else if (data[2].equals(disconnect))
{
userRemove(data[0]);
}
else if (data[2].equals(done))
{
//users.setText("");
writeUsers();
users.clear();
}
}
}catch(Exception ex) { }
}
}
//--------------------------//
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lb_username = new javax.swing.JLabel();
tf_username = new javax.swing.JTextField();
b_connect = new javax.swing.JButton();
b_disconnect = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
b_send = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Chat - Client's frame");
setName("client"); // NOI18N
setResizable(false);
lb_username.setText(" Username");
tf_username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tf_usernameActionPerformed(evt);
}
});
b_connect.setText("Connect");
b_connect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_connectActionPerformed(evt);
}
});
b_disconnect.setText("Disconnect");
b_disconnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_disconnectActionPerformed(evt);
}
});
ta_chat.setColumns(20);
ta_chat.setRows(5);
jScrollPane1.setViewportView(ta_chat);
b_send.setText("SEND");
b_send.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_sendActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.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()
.addComponent(tf_chat, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b_send, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(lb_username, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(tf_username, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(215, 215, 215)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(b_disconnect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(b_connect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b_connect)
.addComponent(lb_username)
.addComponent(tf_username))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(b_disconnect)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tf_chat)
.addComponent(b_send, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE))
.addGap(22, 22, 22))
);
pack();
}// </editor-fold>
private void tf_usernameActionPerformed(java.awt.event.ActionEvent evt) {
}
private void b_connectActionPerformed(java.awt.event.ActionEvent evt) {
if (isConnected == false)
{
username = tf_username.getText();
tf_username.setEditable(false);
try
{
sock = new Socket(address, port);
InputStreamReader streamreader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamreader);
writer = new PrintWriter(sock.getOutputStream());
writer.println(username + ":has connected.:Connect");
writer.flush();
isConnected = true;
}
catch (Exception ex)
{
ta_chat.append("Cannot Connect! Try Again. \n");
tf_username.setEditable(true);
}
ListenThread();
} else if (isConnected == true)
{
ta_chat.append("You are already connected. \n");
}
}
private void b_disconnectActionPerformed(java.awt.event.ActionEvent evt) {
sendDisconnect();
Disconnect();
}
private void b_sendActionPerformed(java.awt.event.ActionEvent evt) {
String nothing = "";
if ((tf_chat.getText()).equals(nothing)) {
tf_chat.setText("");
tf_chat.requestFocus();
} else {
try {
writer.println(username + ":" + tf_chat.getText() + ":" + "Chat");
writer.flush(); // flushes the buffer
} catch (Exception ex) {
ta_chat.append("Message was not sent. \n");
}
tf_chat.setText("");
tf_chat.requestFocus();
}
tf_chat.setText("");
tf_chat.requestFocus();
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new client_frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton b_connect;
private javax.swing.JButton b_disconnect;
private javax.swing.JButton b_send;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lb_username;
private javax.swing.JTextArea ta_chat;
private javax.swing.JTextField tf_chat;
private javax.swing.JTextField tf_username;
// End of variables declaration
}
package chat_server;
import java.io.*;
import java.net.*;
import java.util.*;
public class server_frame extends javax.swing.JFrame
{
ArrayList clientOutputStreams;
//Menangkap client yang connect pada saat itu
public class ClientHandler implements Runnable
{
BufferedReader reader;
Socket sck;
PrintWriter client;
//
public ClientHandler(Socket clientSocket, PrintWriter user)
{
client = user;
try
{
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
}
catch (Exception ex)
{
ta_chat.append("error... \n");
}
}
//Mengimplementasi procedur dari client Handler, yang terdapat procedure run untuk connect,disc,chat
//ketika client sudah terdeteksi online
@Override
public void run()
{
String message, connect = "Connect", disconnect = "Disconnect", chat = "Chat" ;
String[] data;
try
{
while ((message = reader.readLine()) != null)
{
ta_chat.append("Received: " + message + "\n");
data = message.split(":");
for (String token:data)
{
ta_chat.append(token + "\n");
}
if (data[2].equals(connect))
{
tellEveryone((data[0] + ":" + data[1] + ":" + chat));
userAdd(data[0]);
}
else if (data[2].equals(disconnect))
{
tellEveryone((data[0] + ":has disconnected." + ":" + chat));
userRemove(data[0]);
}
else if (data[2].equals(chat))
{
tellEveryone(message);
}
else
{
ta_chat.append("error.... \n");
}
}
}
catch (Exception ex)
{
ta_chat.append("Lost a connection. \n");
ex.printStackTrace();
clientOutputStreams.remove(client);
}
}
}
public server_frame()
{
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
ta_chat = new javax.swing.JTextArea();
b_start = new javax.swing.JButton();
b_users = new javax.swing.JButton();
b_clear = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Chat - Server's frame");
setName("server"); // NOI18N
setResizable(false);
ta_chat.setColumns(20);
ta_chat.setRows(5);
jScrollPane1.setViewportView(ta_chat);
b_start.setText("START");
b_start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_startActionPerformed(evt);
}
});
b_users.setText("Online Users");
b_users.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_usersActionPerformed(evt);
}
});
b_clear.setText("Clear");
b_clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_clearActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.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)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(b_start, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(b_clear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(b_users, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b_start)
.addComponent(b_users))
.addGap(18, 18, 18)
.addComponent(b_clear)
.addGap(20, 20, 20))
);
pack();
}// </editor-fold>
private void b_startActionPerformed(java.awt.event.ActionEvent evt) {
Thread starter = new Thread(new ServerStart());
starter.start();
ta_chat.append("Server started...\n");
}
private void b_usersActionPerformed(java.awt.event.ActionEvent evt) {
ta_chat.append("\n Online users : \n");
for (String current_user : users)
{
ta_chat.append(current_user);
ta_chat.append("\n");
}
}
private void b_clearActionPerformed(java.awt.event.ActionEvent evt) {
ta_chat.setText("");
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run() {
new server_frame().setVisible(true);
}
});
}
// Server mulai
public class ServerStart implements Runnable
{
@Override
public void run()
{
clientOutputStreams = new ArrayList();
users = new ArrayList();
try
{
ServerSocket serverSock = new ServerSocket(8080);
while (true)
{
//inisiasi objek dan permisalan ada client yang connect dengan server
Socket clientSock = serverSock.accept();
PrintWriter writer = new PrintWriter(clientSock.getOutputStream());
clientOutputStreams.add(writer);
Thread listener = new Thread(new ClientHandler(clientSock, writer));
listener.start();
ta_chat.append("Got a connection. \n");
}
}
catch (Exception ex)
{
ta_chat.append("Error making a connection. \n");
}
}
}
public void userAdd (String data)
{
String message, add = ": :Connect", done = "Server: :Done", name = data;
ta_chat.append("Before " + name + " added. \n");
users.add(name);
ta_chat.append("After " + name + " added. \n");
String[] tempList = new String[(users.size())];
users.toArray(tempList);
for (String token:tempList)
{
message = (token + add);
tellEveryone(message);
}
tellEveryone(done);
}
public void userRemove (String data)
{
String message, add = ": :Connect", done = "Server: :Done", name = data;
users.remove(name);
String[] tempList = new String[(users.size())];
users.toArray(tempList);
for (String token:tempList)
{
message = (token + add);
tellEveryone(message);
}
tellEveryone(done);
}
public void tellEveryone(String message)
{
Iterator it = clientOutputStreams.iterator();
while (it.hasNext())
{
try
{
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
ta_chat.append("Sending: " + message + "\n");
writer.flush();
ta_chat.setCaretPosition(ta_chat.getDocument().getLength());
}
catch (Exception ex)
{
ta_chat.append("Error telling everyone. \n");
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton b_clear;
private javax.swing.JButton b_start;
private javax.swing.JButton b_users;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea ta_chat;
// End of variables declaration }
Apabila ada pertanyaan terkait diatas bisa menghubungi kam