Aplikasi Chatting Multi Client dengan Java |
1.Chat Server Return
package javachatgui; import java.io.IOException; import java.io.PrintWriter; import java.net.*; import java.util.Scanner; public class Chat_Server_Return implements Runnable { //Variabel Global Socket SOCK; private Scanner INPUT; private PrintWriter OUT; String MESSAGE = ""; public Chat_Server_Return (Socket X) { this.SOCK = X; } public void CheckConnection() throws IOException { if(!SOCK.isConnected()) { for(int i = 1; i<= Chat_Server.ConnectionArray.size();i++) { if(Chat_Server.ConnectionArray.get(i) == SOCK) { Chat_Server.ConnectionArray.remove(i); } } for(int i = 1; i<= Chat_Server.ConnectionArray.size();i++) { Socket TEMP_SOCK = (Socket) Chat_Server.ConnectionArray.get(i-1); PrintWriter TEMP_OUT = new PrintWriter (TEMP_SOCK.getOutputStream()); TEMP_OUT.println(TEMP_SOCK.getLocalAddress().getHostName() +"Disconnected!"); TEMP_OUT.flush(); //Melihat yang DC pada server System.out.println(TEMP_SOCK.getLocalAddress().getHostName() +"Disconnected!"); } } } public void run() { try { try{ INPUT = new Scanner(SOCK.getInputStream()); OUT = new PrintWriter(SOCK.getOutputStream()); while(true) { CheckConnection(); if(!INPUT.hasNext()) { return; } MESSAGE = INPUT.nextLine(); System.out.println("Client berbicara :"+MESSAGE); for(int i = 1; i<= Chat_Server.ConnectionArray.size();i++) { Socket TEMP_SOCK = (Socket) Chat_Server.ConnectionArray.get(i-1); PrintWriter TEMP_OUT = new PrintWriter (TEMP_SOCK.getOutputStream()); TEMP_OUT.println(MESSAGE); TEMP_OUT.flush(); System.out.println("Mengirim Ke " +TEMP_SOCK.getLocalAddress().getHostName()); } } } finally { SOCK.close(); } } catch(Exception X) {System.out.print(X);} } }
2.Chat Server
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javachatgui; import java.io.IOException; import java.io.PrintWriter; import java.net.*; import java.util.ArrayList; import java.util.Scanner; public class Chat_Server { //Variabel Global public static ArrayListConnectionArray = new ArrayList (); public static ArrayList CurrentUsers = new ArrayList (); public static void main(String[] args) throws IOException { try { final int PORT = 444; ServerSocket SERVER = new ServerSocket(PORT); System.out.println("Menunggu Clients..."); while(true) { Socket SOCK = SERVER.accept(); ConnectionArray.add(SOCK); System.out.println("Client terhubung dari : " + SOCK.getLocalAddress().getHostName()); AddUserName(SOCK); Chat_Server_Return CHAT = new Chat_Server_Return(SOCK); Thread X = new Thread(CHAT); X.start(); } } catch(Exception X) {System.out.print(X);} } public static void AddUserName(Socket X)throws IOException { Scanner INPUT = new Scanner(X.getInputStream()); String UserName = INPUT.nextLine(); CurrentUsers.add(UserName); for(int i = 1; i<= Chat_Server.ConnectionArray.size();i++) { Socket TEMP_SOCK = Chat_Server.ConnectionArray.get(i-1); PrintWriter OUT = new PrintWriter (TEMP_SOCK.getOutputStream()); OUT.println("#?!" +CurrentUsers); OUT.flush(); } } }
3.Chat_Client_GUI
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javachatgui; import java.io.PrintWriter; import java.net.*; import javax.swing.*; import javax.swing.JOptionPane; public class Chat_Client_GUI { //global private static Chat_Client ChatClient; public static String UserName = "Anonymous"; //AES ALGORITHM AESAlgorithm aesAlgo; byte[] keyValue = new byte[]{'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'}; /*public Chat_Client_GUI() { initComponents(); aesAlgo = new AESAlgorithm("MySecretKey"); }*/ //GUI Global - Main Window public static JFrame MainWindow = new JFrame(); private static JButton B_ABOUT = new JButton(); private static JButton B_CONNECT = new JButton(); private static JButton B_DISCONNECT = new JButton(); private static JButton B_HELP = new JButton(); private static JButton B_SEND = new JButton(); private static JButton B_ENCRYPT = new JButton(); private static JButton B_DECRYPT = new JButton(); private static JLabel L_Message = new JLabel("Message: "); public static JTextField TF_Message = new JTextField(20); private static JLabel L_Conversation = new JLabel(); public static JTextArea TA_CONVERSATION = new JTextArea(); private static JScrollPane SP_CONVERSATION = new JScrollPane(); private static JLabel L_ONLINE = new JLabel(); public static JList JL_ONLINE = new JList(); private static JScrollPane SP_ONLINE = new JScrollPane(); private static JLabel L_LoggedInAs = new JLabel(); private static JLabel L_LoggedInAsBox = new JLabel(); //GUI Globals - Login Window public static JFrame LogInWindow = new JFrame(); public static JTextField TF_UserNameBox = new JTextField(20); private static JButton B_ENTER = new JButton("Masuk"); private static JLabel L_EnterUserName = new JLabel("Masukan Username = "); private static JPanel P_LogIn = new JPanel(); public static void main(String[] args) { BuildMainWindow(); Initialize(); } public static void Connect() { try { final int PORT = 444; final String HOST = "localhost"; Socket SOCK = new Socket(HOST,PORT); System.out.println("Kamu Terkoneksi ke : "+HOST); ChatClient = new Chat_Client(SOCK); //Mengirim Nama Ke List PrintWriter OUT = new PrintWriter(SOCK.getOutputStream()); OUT.println(UserName); OUT.flush(); Thread X = new Thread(ChatClient); X.start(); } catch(Exception E) { System.out.print(E); JOptionPane.showMessageDialog(null,"Server Tidak Menjawab"); System.exit(0); } } public static void Initialize() { //untuk Menmbuat tombol mati/atau menyala terlebih dahulu B_SEND.setEnabled(false); B_DISCONNECT.setEnabled(false); B_CONNECT.setEnabled(true); } public static void BuildLogInWindow() { LogInWindow.setTitle("Siapa Nama Anda? : "); LogInWindow.setSize(400,100); LogInWindow.setLocation(250,200); LogInWindow.setResizable(false); P_LogIn = new JPanel(); P_LogIn.add(L_EnterUserName); P_LogIn.add(TF_UserNameBox); P_LogIn.add(B_ENTER); LogInWindow.add(P_LogIn); Login_Action(); LogInWindow.setVisible(true); } public static void BuildMainWindow() { MainWindow.setTitle(UserName +"Chat Box"); MainWindow.setSize(450,500); MainWindow.setLocation(220,180); MainWindow.setResizable(false); ConfigureMainWindow(); MainWindow_Action(); MainWindow.setVisible(true); } public static void ConfigureMainWindow() { MainWindow.setBackground(new java.awt.Color(255, 255, 255)); MainWindow.setSize(550,350); MainWindow.getContentPane().setLayout(null); B_SEND.setBackground(new java.awt.Color(0, 0, 255)); B_SEND.setForeground(new java.awt.Color(255 ,255 ,255)); B_SEND.setText("Kirim"); MainWindow.getContentPane().add(B_SEND); B_SEND.setBounds(250, 40, 81, 25); B_ENCRYPT.setBackground(new java.awt.Color(0, 0, 255)); B_ENCRYPT.setForeground(new java.awt.Color(255 ,255 ,255)); B_ENCRYPT.setText("Enkripsi"); MainWindow.getContentPane().add(B_ENCRYPT); B_ENCRYPT.setBounds(10, 275, 81, 25); B_DECRYPT.setBackground(new java.awt.Color(0, 0, 255)); B_DECRYPT.setForeground(new java.awt.Color(255 ,255 ,255)); B_DECRYPT.setText("dekripsi"); MainWindow.getContentPane().add(B_DECRYPT); B_DECRYPT.setBounds(100, 275, 81, 25); B_DISCONNECT.setBackground(new java.awt.Color(0, 0, 255)); B_DISCONNECT.setForeground(new java.awt.Color(255, 255, 255)); B_DISCONNECT.setText("DISCONNECT"); MainWindow.getContentPane().add(B_DISCONNECT); B_DISCONNECT.setBounds(10, 40, 110, 25); B_CONNECT.setBackground(new java.awt.Color(0, 0, 255)); B_CONNECT.setForeground(new java.awt.Color(255, 255, 255)); B_CONNECT.setText("CONNECT"); MainWindow.getContentPane().add(B_CONNECT); B_CONNECT.setBounds(130, 40, 110, 25); B_HELP.setBackground(new java.awt.Color(0, 0, 255)); B_HELP.setForeground(new java.awt.Color(255, 255, 255)); B_HELP.setText("Help"); MainWindow.getContentPane().add(B_HELP); B_HELP.setBounds(420, 40, 70, 25); B_ABOUT.setBackground(new java.awt.Color(0, 0, 255)); B_ABOUT.setForeground(new java.awt.Color(255, 255, 255)); B_ABOUT.setText("ABOUT"); MainWindow.getContentPane().add(L_Message); TF_Message.setForeground(new java.awt.Color(0, 0, 255)); TF_Message.requestFocus(); MainWindow.getContentPane().add(TF_Message); TF_Message.setBounds(70, 4, 260, 30); L_Conversation.setHorizontalAlignment(SwingConstants.CENTER); L_Conversation.setText("Conversation"); MainWindow.getContentPane().add(L_Conversation); L_Conversation.setBounds(100, 70, 140, 16); TA_CONVERSATION.setColumns(20); TA_CONVERSATION.setFont(new java.awt.Font("Tahoma", 0, 12)); TA_CONVERSATION.setForeground(new java.awt.Color(0, 0, 255)); TA_CONVERSATION.setLineWrap(true); TA_CONVERSATION.setRows(5); TA_CONVERSATION.setEditable(false); SP_CONVERSATION.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); SP_CONVERSATION.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); SP_CONVERSATION.setViewportView(TA_CONVERSATION); MainWindow.getContentPane().add(SP_CONVERSATION); SP_CONVERSATION.setBounds(10, 90, 330, 180); L_ONLINE.setHorizontalAlignment(SwingConstants.CENTER); L_ONLINE.setToolTipText(""); MainWindow.getContentPane().add(L_ONLINE); L_ONLINE.setBounds(350, 70, 130, 16); // String [] TestNames = {"Dimas","Juang","Ipat","Kkh"}; JL_ONLINE.setForeground(new java.awt.Color(0, 0, 255)); // JL_ONLINE.setListData(TestNames); SP_ONLINE.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); SP_ONLINE.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); SP_ONLINE.setViewportView(JL_ONLINE); MainWindow.getContentPane().add(SP_ONLINE); SP_ONLINE.setBounds(350, 90, 130, 180); L_LoggedInAs.setFont(new java.awt.Font("Tahoma", 0, 12)); L_LoggedInAs.setText("Yang Baru Bergabung"); MainWindow.getContentPane().add(L_LoggedInAs); L_LoggedInAs.setBounds(348, 0, 140, 15); L_LoggedInAsBox.setHorizontalAlignment(SwingConstants.CENTER); L_LoggedInAsBox.setFont(new java.awt.Font("Tahoma", 0, 12)); L_LoggedInAsBox.setForeground(new java.awt.Color(255, 0, 0)); L_LoggedInAsBox.setBorder( BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); MainWindow.getContentPane().add(L_LoggedInAsBox); L_LoggedInAsBox.setBounds(340, 17, 150, 20); } public static void Login_Action() { B_ENTER.addActionListener( new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { ACTION_B_ENTER(); } } ); } public static void ACTION_B_ENTER() { if(!TF_UserNameBox.getText().equals("")) { UserName = TF_UserNameBox.getText().trim(); L_LoggedInAsBox.setText(UserName); Chat_Server.CurrentUsers.add(UserName); MainWindow.setTitle(UserName +" Chat Box"); LogInWindow.setVisible(false); B_SEND.setEnabled(true); B_ENCRYPT.setEnabled(true); B_DECRYPT.setEnabled(false); B_DISCONNECT.setEnabled(true); B_CONNECT.setEnabled(false); Connect(); } else { {JOptionPane.showMessageDialog(null, "Tolong Masukan Nama Anda");} } } public static void MainWindow_Action() { B_SEND.addActionListener( new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { ACTION_B_SEND(); } } ); B_ENCRYPT.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ACTION_B_ENCRYPT(); } } ); B_DECRYPT.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ACTION_B_DECRYPT(); } } ); B_DISCONNECT.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ACTION_B_DISCONNECT(); } } ); B_CONNECT.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BuildLogInWindow(); } } ); B_HELP.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ACTION_B_HELP(); } } ); B_ABOUT.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ACTION_B_HELP(); } } ); } public static void ACTION_B_ENCRYPT() { if(!TF_Message.getText().equals("")) { ChatClient.SEND(TF_Message.getText()); TF_Message.requestFocus(); } } public static void ACTION_B_SEND() { if(!TF_Message.getText().equals("")) { ChatClient.SEND(TF_Message.getText()); TF_Message.requestFocus(); } } public static void ACTION_B_DECRYPT() { if(!TF_Message.getText().equals("")) { ChatClient.SEND(TF_Message.getText()); TF_Message.requestFocus(); } } public static void ACTION_B_DISCONNECT() { try { ChatClient.DISCONNECT(); } catch(Exception Y) { Y.printStackTrace();} } public static void ACTION_B_HELP() { JOptionPane.showMessageDialog(null, "Multi Client Chat Program"); } }
4.Chat_Client
package javachatgui; import java.io.*; import java.util.Scanner; import java.net.*; import javax.swing.JOptionPane; public class Chat_Client implements Runnable { Socket SOCK; Scanner INPUT; Scanner SEND = new Scanner(System.in); Scanner DECRYPT = new Scanner(System.in); PrintWriter OUT; public Chat_Client(Socket X) { this.SOCK = X; } public void run() { try { try { INPUT = new Scanner(SOCK.getInputStream()); OUT = new PrintWriter(SOCK.getOutputStream()); OUT.flush(); CheckStream(); } finally { SOCK.close(); } } catch(Exception X) { System.out.print("X"); } } public void DISCONNECT() throws IOException { OUT.println(Chat_Client_GUI.UserName +"Has Disconnect"); OUT.flush(); SOCK.close(); JOptionPane.showMessageDialog(null,"Connected"); System.exit(0); } public void CheckStream() { while(true) { RECEIVE(); } } public void RECEIVE() { if(INPUT.hasNext()) { String MESSAGE = INPUT.nextLine(); if(MESSAGE.contains("#?!")) { String TEMP1 = MESSAGE.substring(3); TEMP1 = TEMP1.replace("[",""); TEMP1 = TEMP1.replace("]",""); String[] CurrentUsers = TEMP1.split(", "); Chat_Client_GUI.JL_ONLINE.setListData(CurrentUsers); } else { Chat_Client_GUI.TA_CONVERSATION.append(MESSAGE +"\n"); } } } public void SEND(String X) { OUT.println(Chat_Client_GUI.UserName +": " +X); OUT.flush(); Chat_Client_GUI.TF_Message.setText(""); } public void DECRYPT(String X) { OUT.println(Chat_Client_GUI.UserName +": " +X); OUT.flush(); Chat_Client_GUI.TF_Message.setText(""); } }
5.AES Algorithm
package javachatgui; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AESAlgorithm { public static String algo = "AES"; public byte[] keyValue; public AESAlgorithm(byte key[]){ keyValue = key; } public Key generateKey() throws Exception { Key key = new SecretKeySpec(keyValue, algo); return key; } public String encrypt(String msg) throws Exception{ Key key = generateKey(); Cipher c = Cipher.getInstance(algo); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(msg.getBytes()); String encryptedValue = new String(Base64.getEncoder().encode(encVal)); return encryptedValue; } public String decrypt(String msg)throws Exception{ Key key = generateKey(); Cipher c = Cipher.getInstance(algo); c.init(Cipher.DECRYPT_MODE, key); byte[] decordedValue = Base64.getDecoder().decode(msg); byte[] decValue = c.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; } }
Minta SourceCode atau bertanya langsung tulis saja di kolom komentar
Minta program dan source codenya gan
ReplyDeleteemail di : sandy.intisel@gmail.com
dah bang coba cek
ReplyDeleteMinta program dan source code pleasenya gan.
ReplyDeleteemail saya : almantarigan90@gmail.com
terima kasih kang
ReplyDeletecek bang sudah
ReplyDeletehttps://drive.google.com/file/d/0B6HdcdV-m5BaNWh5R2pZbXJqZk0/view?usp=sharing
ReplyDelete