Page 3 of 3 First 123
  • Jump to page:
    #31
  1. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Originally Posted by NormR
    I would rewrite this code so that it was in its own class with methods that could be called from the other classes. I would NOT try to merge this code into the other code.

    You still do NOT have working client/server code that does this:

    I would get the testing program to work completely before trying to merge the code.
    The code that was posted on the other thread is very badly written. I would NOT try to add any more code to it. It should be broken up into smaller classes and those classes used in a main class that builds the GUI, gets user input and connects to a server.
    I am not getting the connection after the user clicks some buttons.

    Here is the code that you want:

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package programtesisbaru;
    
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    
    /**
     *
     * @author Tawi
     */
    class KoneksiKlienServer extends JFrame {
        JLabel statusKoneksiJaringan = new JLabel("Belum Terhubung ke Klien");
        JLabel informasiServer = new JLabel("");
        JLabel informasiKlien = new JLabel("");
        JLabel alamatServer = new JLabel("");
        JLabel alamatKlien = new JLabel("");
        JLabel namaServer = new JLabel("");
        JLabel namaKlien = new JLabel("");
        JLabel notifikasi = new JLabel("");
    public KoneksiKlienServer() {
        setLayout(null);
        setSize(1024,768);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null); 
        statusKoneksiJaringan.setBounds(0, 90, 200, 25);
        informasiServer.setBounds(0, 110, 200, 25);
        informasiKlien.setBounds(0, 130, 200, 25);
        namaServer.setBounds(0, 150, 200, 25);
        alamatServer.setBounds(0, 170, 200, 25);
        namaKlien.setBounds(0, 190, 200, 25);
        alamatKlien.setBounds(0, 210, 200, 25);
        notifikasi.setBounds(0, 230, 200, 25);
        add(statusKoneksiJaringan);
        add(informasiServer);
        add(informasiKlien);
        add(alamatServer);
        add(alamatKlien);
        add(notifikasi);
        try {
            ServerSocket koneksiJaringan = new ServerSocket(8000);
            informasiServer.setText("Server Dimulai Saat " + new Date() + '\n');
            int clientNo = 1;
            while (true) {
                Socket  terimaKoneksi = koneksiJaringan.accept();
                informasiKlien.setText("Memulai Klien " + clientNo + " pada" + new Date() + '\n');
                InetAddress alamat = terimaKoneksi.getInetAddress();
                InetAddress host; 
                host = InetAddress.getLocalHost();
                alamatServer.setText("Alamat server yaitu: " + host.getHostAddress()+ '\n');
                alamatKlien.setText("Alamat Klien Terakhir Terhubung yaitu: " + alamat.getHostAddress() + '\n');
                namaServer.setText("Nama Server yaitu: " + host.getHostName() + '\n');
                namaKlien.setText("Nama Klien yaitu:" + alamat.getHostName() + '\n');
                HandleClient tugas = new HandleClient(terimaKoneksi);
                tugas.run();
                clientNo++;
            }
        }
        catch (IOException ex) {
            System.err.println(ex);
            ex.printStackTrace();
        }
    }
    
        class HandleClient implements Runnable {
            private Socket socket;
            public HandleClient(Socket socket) {
                this.socket = socket;
            }
    
            public void run() {
                try {
                    DataInputStream inputdariKlien = new DataInputStream(socket.getInputStream());
                    DataOutputStream keluarkeKlien = new DataOutputStream(socket.getOutputStream());
                    while (true) {
                        double radius = inputdariKlien.readDouble();
                        double area = radius * radius * Math.PI;
                        keluarkeKlien.writeDouble(area);
                        notifikasi.setText("Radius yang Didapatkan dari Klien yaitu: " + radius + " dan Luas yang Ditemukan yaitu: " + area + '\n');
                    }
                }
                catch(IOException ex) {
                    System.err.println(ex);
                    ex.printStackTrace();
                }
            }
        }
     public static void main(String[] args) {  
            KoneksiKlienServer tesis = new KoneksiKlienServer();   
            tesis.setTitle("Program Pemilihan Obat dengan Cara Analytic Hierarchycal Process");  
            tesis.setSize(1024,768);  
            tesis.setVisible(true);  
            tesis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            tesis.setLocationRelativeTo(null); }
    }
    Well, how to merge the code? How to do the way that you told me? Please give me a code sample.
    Last edited by Tawijakarta; July 27th, 2013 at 11:13 PM.
  2. #32
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I am not getting the connection after the user clicks some buttons.
    The posted code does not have any buttons in it. How can the user click a button if there are no buttons???
  4. #33
  5. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Originally Posted by NormR
    The posted code does not have any buttons in it. How can the user click a button if there are no buttons???
    You want a working code. That is the reason to post this. I do not put any main in the code. I extend the code. So how to extend this code from the other code that make the button and other things in other codes to show up and work?
  6. #34
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    how to extend this code from the other code that make the button
    Make a fully working test program BEFORE trying to merge the client/server code with the rest of the project code. Write small classes for testing first. When the code works like you want it to, then work on using it in the larger program.
    How will the code with the buttons use the posted code? The posted code does not have any methods that the class with the buttons can call.
  8. #35
  9. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Why can't I see any buttons?

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package programtesisbaru;
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import javax.swing.*;
    
    /**
     *
     * @author Tawi
     */
    public class NewKoneksi extends JFrame{
        JLabel statusKoneksiJaringan = new JLabel("Belum Terhubung ke Klien");
        JButton keluar = new JButton("Keluar");
        JLabel informasiServer = new JLabel("");
        JLabel informasiKlien = new JLabel("");
        JLabel alamatServer = new JLabel("");
        JLabel alamatKlien = new JLabel("");
        JLabel namaServer = new JLabel("");
        JLabel namaKlien = new JLabel("");
        JLabel notifikasi = new JLabel("");
    public NewKoneksi() {
        setLayout(null);
        setSize(1024,768);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null); 
        statusKoneksiJaringan.setBounds(0, 90, 200, 25);
        informasiServer.setBounds(0, 110, 200, 25);
        informasiKlien.setBounds(0, 130, 200, 25);
        namaServer.setBounds(0, 150, 200, 25);
        alamatServer.setBounds(0, 170, 200, 25);
        namaKlien.setBounds(0, 190, 200, 25);
        alamatKlien.setBounds(0, 210, 200, 25);
        notifikasi.setBounds(0, 230, 200, 25);
        keluar.setBounds(700,10,100,25);  
        add(keluar);
        keluar.setVisible(true);
        keluar.addActionListener(new java.awt.event.ActionListener() {  
             @Override  
             public void actionPerformed(java.awt.event.ActionEvent evt) {keluar();}  
             private void keluar() {System.exit(0);}});
        add(statusKoneksiJaringan);
        add(informasiServer);
        add(informasiKlien);
        add(alamatServer);
        add(alamatKlien);
        add(notifikasi);
        try {
            ServerSocket koneksiJaringan = new ServerSocket(8000);
            informasiServer.setText("Server Dimulai Saat " + new Date() + '\n');
            int clientNo = 1;
            while (true) {
                Socket  terimaKoneksi = koneksiJaringan.accept();
                informasiKlien.setText("Memulai Klien " + clientNo + " pada" + new Date() + '\n');
                InetAddress alamat = terimaKoneksi.getInetAddress();
                InetAddress host; 
                host = InetAddress.getLocalHost();
                alamatServer.setText("Alamat server yaitu: " + host.getHostAddress()+ '\n');
                alamatKlien.setText("Alamat Klien Terakhir Terhubung yaitu: " + alamat.getHostAddress() + '\n');
                namaServer.setText("Nama Server yaitu: " + host.getHostName() + '\n');
                namaKlien.setText("Nama Klien yaitu:" + alamat.getHostName() + '\n');
                NewKoneksi.HandleClient tugas = new NewKoneksi.HandleClient(terimaKoneksi);
                tugas.run();
                clientNo++;
            }
        }
        catch (IOException ex) {
            System.err.println(ex);
            ex.printStackTrace();
        }
    }
    
        class HandleClient implements Runnable {
            private Socket socket;
            public HandleClient(Socket socket) {
                this.socket = socket;
            }
    
            public void run() {
                try {
                    DataInputStream inputdariKlien = new DataInputStream(socket.getInputStream());
                    DataOutputStream keluarkeKlien = new DataOutputStream(socket.getOutputStream());
                    while (true) {
                        double radius = inputdariKlien.readDouble();
                        double area = radius * radius * Math.PI;
                        keluarkeKlien.writeDouble(area);
                        notifikasi.setText("Radius yang Didapatkan dari Klien yaitu: " + radius + " dan Luas yang Ditemukan yaitu: " + area + '\n');
                    }
                }
                catch(IOException ex) {
                    System.err.println(ex);
                    ex.printStackTrace();
                }
            }
        }
     public static void main(String[] args) {  
            KoneksiKlienServer tesis = new KoneksiKlienServer();   
            tesis.setTitle("Program Pemilihan Obat dengan Cara Analytic Hierarchycal Process");  
            tesis.setSize(1024,768);  
            tesis.setVisible(true);  
            tesis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            tesis.setLocationRelativeTo(null); }
    }
  10. #36
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The posted code will not compile because of a missing class: KoneksiKlienServer
    If it does not compile, it can not be executed.
  12. #37
  13. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Why does my program stop to do any things after I click masuk button?
  14. #38
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    I can't compile the code in post#35 because of compiler error.

    click masuk button
    There isn't a button named masuk in the code posted in post#35.
  16. #39
  17. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    It is working and after I merge the code with masuk button, the code stop to do things after I click the masuk button. How to make it work again? How to solve this problem? Do I need a Core I7 notebook to test the code?
  18. #40
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Try debugging the code using the println() method to print out messages and variable values so you can see what the program is doing.
  20. #41
  21. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    I have two kinds of user. One user will update the database and the other will only use the database. How to determine the user? What should I do to be able to know the connected user is the database updater or the database user? Using the IP address will not help me to determine the user, do you have any solutions?
    Last edited by Tawijakarta; August 1st, 2013 at 01:27 AM.
Page 3 of 3 First 123
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo