Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: [Request]Explanation about sending and recieving data

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default [Request]Explanation about sending and recieving data

    Hey all,
    I am making a multiplayer game and I need to always send the place and state of the character from the client to the server and from the server to the client.
    The question is, how to do it?
    And can someone also explain the process in detail.
    Thanks in advance,
    Alex.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: [Request]Explanation about sending and recieving data

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: [Request]Explanation about sending and recieving data

    I've read this article before posting but I didn't understand how the communication is working, because how i see it, it scans input from the keyboard and sends it to the server
    but I need that every action the player does it sends the x,y,state to the server.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: [Request]Explanation about sending and recieving data

    And what part of that are you having trouble with? What have you tried so far? Where are you stuck?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: [Request]Explanation about sending and recieving data

    The problem is that I can't fully understand how the communication between the socket and the serversocket works.
    What I understood is that for example the server waits for me to write something and then it sends it to the client and the same happens in the client.
    But I need it to send data without scanning something from the keyboard.

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: [Request]Explanation about sending and recieving data

    I tried using the PrintWriter and the BufferedReader but it just waits for me to input something, but thats not what I want.
    I want it to send data automatically without me having to input anything.
    Here is the code:
    Client:
    try{
                IP = this.ipText.getText();
                port = Integer.decode(this.portText.getText());
                socket = null;
            try {
                System.err.println("Trying to connect to host...");
                socket = new Socket(IP, port);
                out = new PrintWriter(socket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
                System.err.println("Waiting for host to start game...");
                this.ipText.setVisible(false);
                this.portText.setVisible(false);
                this.Connect.setVisible(false);
                this.ipLbl.setText("Connected to " + IP);
                this.portLbl.setText("Waiting for host to start the game...");
                setSize(getWidth(),getHeight()-50);
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: "+IP+".");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: "+IP+".");
                System.exit(1);
            }
           /* try {
                socket.close();
            } catch (IOException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }*/
            } catch(Exception e){
                JOptionPane.showMessageDialog(null, "Error: Field's are empty or port contains illegal characters.");
            }

    Host:
     public Host() throws IOException {
            initComponents();
            serverSocket = null;
            try {
                serverSocket = new ServerSocket(port);
            } catch (IOException e) {
                System.err.println("Could not listen on port: "+port+".");
                System.exit(1);
            }
            try {
                System.err.println("Waiting for client...");
                clientSocket = serverSocket.accept();
                this.connectedMsg.setText(clientSocket.getInetAddress().getHostAddress() + " - has joined the game.");
                mk = new MortalKombat();
                servPlayer = new Player("Scorpion", mk, 50, mk.getFrameH()-200, false);
                mk.addPlayer(servPlayer, 1);
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.exit(1);
            }
            /*serverSocket.close();
            clientSocket.close();*/
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            start = new javax.swing.JButton();
            connectedMsg = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            start.setText("start");
            start.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    startActionPerformed(evt);
                }
            });
     
            connectedMsg.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
            connectedMsg.setText("Connected player");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(connectedMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(19, Short.MAX_VALUE))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(174, Short.MAX_VALUE)
                    .addComponent(start)
                    .addGap(171, 171, 171))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(connectedMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                    .addComponent(start)
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>                        
     
    private void startActionPerformed(java.awt.event.ActionEvent evt) {                                      
            try {
                out = new PrintWriter(clientSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); 
            } catch (IOException ex) {
                Logger.getLogger(Host.class.getName()).log(Level.SEVERE, null, ex);
            }
        mk.setVisible(true);
        this.setVisible(false);
        System.err.println("Game starting..");
    }

  7. #7

    Default Re: [Request]Explanation about sending and recieving data

    Your problem is that you are doing this with a TCP connection. TCP connections are guaranteed to pass data successfully from A to B and allow a one way transfer of data at any one time (unless you have two TCP connections). If you want to get around this, create UDP connections and use Datagram Packets. They don't require feedback from the client (as a rule in networking, the client is the computer receiving the data, whether it's actually the server or not) and won't hang if the client does not receive the packet, they just keep on transmitting.

    You send data out through UDP as a stream of bytes which you can then turn back into any object you wish on the other side.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Sending Data Between PC to Mobile
    By ultrabeat in forum Java Theory & Questions
    Replies: 0
    Last Post: July 23rd, 2011, 09:06 AM
  2. Can't form a simple POST or GET http request. No data returns.
    By goodguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 29th, 2011, 05:04 AM
  3. HELP with sending data within text file to client with printwriter
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2011, 02:42 PM
  4. Replies: 3
    Last Post: March 3rd, 2011, 02:17 PM
  5. Problem sending POST request with Java..
    By lost in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 09:16 PM