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

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0

    Please help me to connect


    What code should I use to see the IPs that are connected to the internet so I can choose the IP that I want to connect to? Can we create multiple connection (request or accept multiple connection)? Can I ask the user to click a button or do some things first before I connect him/her to the other computers?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    What code should I use to see the IPs that are connected to the internet
    There are millions and millions of IP addresses on the internet. What do you want to see them for? The PING command can be used to determine if one IP address has a responding server connected to it.
  4. #3
  5. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    I want to provide the users two ways to connect to the server, which are: First, choose the server from the list and second enter the IP address, how to do that?

    Please answer all of the questions with sample codes, ok?
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    Does the server have an address? Like the URL: www.Myserver.com
    That can be used with the URL class.
    If the server uses an IP address, it can be used with the URL or Socket class.

    See the API doc and the tutorials for how to use those classes.
  8. #5
  9. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Originally Posted by NormR
    Does the server have an address? Like the URL: www.Myserver.com
    That can be used with the URL class.
    If the server uses an IP address, it can be used with the URL or Socket class.

    See the API doc and the tutorials for how to use those classes.
    What code should I use to make the user click a button before I connect them?
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    What code should I use to make the user click a button before I connect them?
    There is nothing special about connecting a program to a server. Use ordinary programming techniques to detect user input and to control the program connecting to a server.
    One consideration might be to use a background thread to get the connection to prevent the getting of the connection from holding up the GUI thread of the listener method.
    Last edited by NormR; July 24th, 2013 at 02:07 PM.
  12. #7
  13. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Originally Posted by NormR
    There is nothing special about connecting a program to a server. Use ordinary programming techniques to detect user input and to control the program connecting to a server.
    Like what?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    One consideration might be to use a background thread to get the connection to prevent the getting of the connection from holding up the GUI thread of the listener method.
  16. #9
  17. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Originally Posted by NormR
    One consideration might be to use a background thread to get the connection to prevent the getting of the connection from holding up the GUI thread of the listener method.
    How exactly do I have to do to be able to do this?
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    How exactly do I have to do to be able to do this?
    This is very confusing. The other code on the http://forums.devshed.com/java-help-9/please-help-with-my-program-944098-8.html thread does the same thing except it connects to a DB instead of connecting to a server.

    You know how to use button listeners. Where are you having a problem?

    For the first version of the code, forget about using a background thread. Add that code later, after the code making the connection works.
    Last edited by NormR; July 24th, 2013 at 02:27 PM.
  20. #11
  21. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Yes, I know the technique to use an action listener. The problem is I want two computers to be not connected first and after the user clicks a button, the two computers will connect each other. How to do that? A code in a book will connect it automatically without the user clicks any button. How to connect the user after the user clicks a button? How determine the clients address? Will getHostAddress() give me the client's IP address?

    Code:
    import java.net.*;
    import java.io.*;
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    
    /**
     *
     * @author Tawi
     */
    class KoneksiKlienServer extends JFrame {
        JLabel statusKoneksiJaringan = new JLabel("Belum Terhubung ke Internet");
        JLabel informasiServer = new JLabel("");
        JLabel informasiKlien = new JLabel("");
        JLabel alamatServer = new JLabel("");
        JLabel alamatKlien = new JLabel("");
    public KoneksiKlienServer() {
        setLayout(null);
        setSize(1024,768);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        statusKoneksiJaringan.setBounds(0, 90, 200, 25);
        informasiServer.setBounds(0, 110, 200, 25);
        informasiKlien.setBounds(0, 130, 200, 25);
        alamatServer.setBounds(0, 150, 200, 25);
        alamatKlien.setBounds(0, 170, 200, 25);
        add(statusKoneksiJaringan);
        add(informasiServer);
        add(informasiKlien);
        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();
                alamatServer.setText("Nama server yaitu: " + alamat.getHostName() + '\n');
            }
    }
    }
    These codes are not finished. Do you thing these codes will not connect the user automatically? Do you think terimaKoneksi.open() and terimaKoneksi.close() will work?
    Last edited by Tawijakarta; July 26th, 2013 at 02:58 AM.
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    How to connect the user after the user clicks a button?
    Put the code that does the connect in the listener method for the button.

    How determine the clients address?
    When the client connects to the server, the server gets the client's address.
  24. #13
  25. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Originally Posted by NormR
    Put the code that does the connect in the listener method for the button.


    When the client connects to the server, the server gets the client's address.
    Well, I want to show the user the address of the clients. how to do that?

    One more, the codes work well before I extend the KoneksiKlienServer, why? Why does the code only show "Belum Terhubung ke Internet" and "Server dimulai Saat Fri Jul 26 21:..."?

    These are the codes:

    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 Internet");
        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 = null ; 
                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();
                }
            }
        }
    }
    Last edited by Tawijakarta; July 26th, 2013 at 11:01 AM.
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,717
    Rep Power
    348
    the codes work well before I extend the KoneksiKlienServer, why?
    Does that mean the code does not work sometime? Is there an error message?
    Or what happens that you are not happy with?
    What do you expect the code to do diffrently?
    Why does the code only show "Belum Terhubung ke Internet" and "Server dimulai Saat Fri Jul 26 21:..."?
    Is there something you expect to be shown that is not shown? Why do you expect any more to be shown?
    How are you testing the code?
  28. #15
  29. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Originally Posted by NormR
    Does that mean the code does not work sometime? Is there an error message?
    Or what happens that you are not happy with?
    What do you expect the code to do diffrently?
    Is there something you expect to be shown that is not shown? Why do you expect any more to be shown?
    How are you testing the code?
    No, there are no error messages. I expect the code to show a three JLabels, two JTextFields, a Masuk button, a Keluar button. The fact is it does not show them. What is wrong? The code works well before I extends the "KoneksiKlienServer". What is wrong?

    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();
                }
            }
        }
    }
    This is the code that is being extended. What should I do to fix the code?

    One more, can I send an array over the internet?
    Last edited by Tawijakarta; July 27th, 2013 at 11:21 AM.
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo