Page 1 of 2 12 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 with connection problems


    If I make a user code that will connect to a server and send a same variable but the content of every variable is not the same to the server simultaneously, will any contents of variable being replaced? Which one? How to solve this?
    Last edited by Tawijakarta; August 8th, 2013 at 09:51 AM.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Can you post a small program that compiles, executes and shows the problem?
    Also post the input and output from the program that shows what you are talking about.
    For example: the client sends "asdf" to the server and the server receives: "xsdq"
  4. #3
  5. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package klientesis;
    
    /**
     *
     * @author Tawi
     */
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class KlienTesis extends JFrame{
    
        /**
         * @param args the command line arguments
         */
        private JTextField jtf = new JTextField(10);
        private JTextArea jta = new JTextArea("This is the JTextArea\n\n\n\nSeveral lines\n");
        private DataInputStream fromServer;
        private DataOutputStream toServer;
        
        public static void main(String[] args) {
            new KlienTesis(); // TODO code application logic here
        }
        public KlienTesis() {
            JPanel p = new JPanel();
            setLayout(new BorderLayout());
            p.add(new JLabel("Masukkan Radius: "), BorderLayout.WEST);
            p.add(jtf, BorderLayout.CENTER);
            jtf.setHorizontalAlignment(JTextField.RIGHT);
            setLayout(new BorderLayout());
            p.add(new JScrollPane(jta), BorderLayout.CENTER);
            jtf.addActionListener(new ButtonListener());
            add(p);
            setTitle("Klien");
            setSize(1024,768);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            try {
                Socket hubungkan = new Socket("localhost", 8000);
                fromServer = new DataInputStream(hubungkan.getInputStream());
                toServer = new DataOutputStream(hubungkan.getOutputStream());
            }
            catch (IOException ex) {
                jta.append(ex.toString() + '\n');
                ex.printStackTrace();
            }
        }
    
        private class ButtonListener implements ActionListener {
           public void actionPerformed(ActionEvent ex) {
               try {
                   double radius = Double.parseDouble(jtf.getText().trim());
                   toServer.writeDouble(radius);
                   toServer.flush();
                   double area = fromServer.readDouble();
                   jta.append("Radius yaitu: " + radius + '\n');
                   jta.append("Luas yang Didapatkan dari Server yaitu: " + area + '\n');
               }
               catch (IOException e) {
                   System.err.println(e);
                   e.printStackTrace();
               }
           }
        }
    }
    Ok, assume that this code is used in three clients and these clients are not the same computers or in other words, they are not the local host, if they simultaneously send radius to the server, will any values get replaced?
    Last edited by Tawijakarta; August 8th, 2013 at 10:07 AM.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    if they simultaneously send radius to the server, will any values get replaced?
    Each client should connect at its own socket on the server and send its data to that socket so all the data sent should be received ok since it is received by its own socket.
    What does "values get replaced" mean?

    Where is the server code that this client program connects to?
    Can you show what the problem is by posting the data that is sent and received? What does the client send to the server?
    what does the server receive?

    NOTE: The testing code does NOT need to use GUI to send data or receive data.
  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
    Each client should connect at its own socket on the server and send its data to that socket so all the data sent should be received ok since it is received by its own socket.
    What does "values get replaced" mean?

    Where is the server code that this client program connects to?
    Can you show what the problem is by posting the data that is sent and received? What does the client send to the server?
    what does the server receive?

    NOTE: The testing code does NOT need to use GUI to send data or receive data.
    Well, let's assume that three computers (clients) use this code and simultaneously send radius. That will be three radius values that will be sent to the server. Will those values replace each other?
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Will those values replace each other?
    They could. It depends on the code on the server. The server needs to keep the data received from each client separate from what is received from other clients. One way would be to have a class that has a separate instance for handling each client.
  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
    They could. It depends on the code on the server. The server needs to keep the data received from each client separate from what is received from other clients. One way would be to have a class that has a separate instance for handling each client.
    Like what? Can you give me an example?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    When the server gets a connection at the accept() method, it can create an instance of the client handling class and pass it the socket it can use to communicate with the client. That way each client will have its own instance of the class to hold data, etc
  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
    When the server gets a connection at the accept() method, it can create an instance of the client handling class and pass it the socket it can use to communicate with the client. That way each client will have its own instance of the class to hold data, etc
    Well, after those clients have their own instances, will radius that is received from client A being replaced automatically by B or C or vice versa?
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    after those clients have their own instances, will radius that is received from client A being replaced automatically by B or C or vice versa?
    No. Each instance of the client handling class will have its own values that it has received from the client that it is connected to.
  20. #11
  21. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Originally Posted by NormR
    No. Each instance of the client handling class will have its own values that it has received from the client that it is connected to.
    How to recoqnize the value from A, B, or C?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    How to recoqnize the value from A, B, or C?
    Each client will have to send some identifying information to the server so the server will know who each of them are.
  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
    Each client will have to send some identifying information to the server so the server will know who each of them are.
    Well, the server can know but how will I know that the radius is from A, B, or C?

    Will the client send identifying information automatically without I need to make them to?
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Will the client send identifying information automatically without I need to make them to?
    No, your code will need to send the "A" or "B" etc to identify the client that is connected.
  28. #15
  29. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    257
    Rep Power
    0
    Can you give me some examples?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo