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 4 of 4

Thread: java Client Server program by using socket problem

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java Client Server program by using socket problem

    hello all respected members here
    i am writing a code for server and client in java upltill now i am able to send data from client to server but now i want to recive data from server ..server is sending but i dnt know where to write code in client side.. i use netbeans and drag and drop to make graphical user interface, i am using buttons on client sides only and when button click i send specific data to server but when data come to client i dnt know where to recive .. becuse in client side i only used actionPerformed function and write code in that function..please guide thanks


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: java Client Server program by using socket problem

    Set up the client to catch incoming messages the same way you would on the server to listen for the client. If you are sending specific data on the button click, then you have already set up the server to listen to incoming messages. Do the same thing on the client using the Socket you write to when sending the button press data.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Client Server program by using socket problem

    sir i know how to write code to recive data but dnt know where to write...mean on which location in code i should write becuse netbeans creat its own code when we use drag and drop i have posted client code here .. i just write code int actionPerformed event i dnt know about the remaing code which betbeans makes it self.
    __________________________________________________ ____________________________________

    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Engr.Ali Raza
     */
    public class control extends javax.swing.JFrame {
     
        /**
         * Creates new form control
         */
        public control() {
            initComponents();
        }
     
        /**
         * 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() {
     
            jLabel7 = new javax.swing.JLabel();
            jLabel5 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jButton4 = new javax.swing.JButton();
            jLabel2 = new javax.swing.JLabel();
            filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 32767));
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setPreferredSize(new java.awt.Dimension(700, 550));
            getContentPane().setLayout(null);
     
            jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/umer sab.png"))); // NOI18N
            getContentPane().add(jLabel7);
            jLabel7.setBounds(170, 200, 320, 34);
     
            jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/names.png"))); // NOI18N
            getContentPane().add(jLabel5);
            jLabel5.setBounds(170, 120, 370, 30);
     
            jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sup.png"))); // NOI18N
            getContentPane().add(jLabel6);
            jLabel6.setBounds(160, 160, 290, 30);
     
            jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/group members.png"))); // NOI18N
            getContentPane().add(jLabel4);
            jLabel4.setBounds(160, 80, 300, 40);
     
            jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pu (2).png"))); // NOI18N
            getContentPane().add(jLabel3);
            jLabel3.setBounds(150, 10, 500, 60);
     
            jButton1.setBackground(new java.awt.Color(204, 51, 0));
            jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            jButton1.setForeground(new java.awt.Color(255, 255, 51));
            jButton1.setText("Moving Up");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
            getContentPane().add(jButton1);
            jButton1.setBounds(40, 310, 130, 60);
     
            jButton2.setBackground(new java.awt.Color(204, 51, 0));
            jButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            jButton2.setForeground(new java.awt.Color(255, 255, 51));
            jButton2.setText("Fire ");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
            getContentPane().add(jButton2);
            jButton2.setBounds(270, 440, 130, 60);
     
            jButton3.setBackground(new java.awt.Color(204, 51, 0));
            jButton3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            jButton3.setForeground(new java.awt.Color(255, 255, 102));
            jButton3.setText("Stop");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
            getContentPane().add(jButton3);
            jButton3.setBounds(270, 310, 130, 60);
     
            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Punjab-University-Logo-or-Monogram.png"))); // NOI18N
            jLabel1.setText("jLabel1");
            getContentPane().add(jLabel1);
            jLabel1.setBounds(10, 10, 130, 160);
     
            jButton4.setBackground(new java.awt.Color(204, 51, 0));
            jButton4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
            jButton4.setForeground(new java.awt.Color(255, 255, 51));
            jButton4.setText("Moving Down");
            jButton4.setMargin(new java.awt.Insets(2, 8, 2, 8));
            jButton4.setMaximumSize(new java.awt.Dimension(120, 50));
            jButton4.setPreferredSize(new java.awt.Dimension(150, 50));
            jButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton4ActionPerformed(evt);
                }
            });
            getContentPane().add(jButton4);
            jButton4.setBounds(510, 310, 140, 60);
     
            jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/world-best-army-picture-and-wallpaper.jpg"))); // NOI18N
            jLabel2.setMaximumSize(new java.awt.Dimension(2000, 2000));
            jLabel2.setPreferredSize(new java.awt.Dimension(800, 700));
            getContentPane().add(jLabel2);
            jLabel2.setBounds(0, 0, 800, 563);
            getContentPane().add(filler1);
            filler1.setBounds(0, 238, 0, 0);
     
            pack();
        }// </editor-fold>                        
     
        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
             Socket clientSocket = null;
            try {
                clientSocket = new Socket("ALIRAZA-HP",6789);
            } catch (UnknownHostException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
             DataOutputStream outToServer = null;
            try {
                outToServer = new DataOutputStream(clientSocket.getOutputStream());
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outToServer.writeBytes("d");
                clientSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                        
     
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            Socket clientSocket = null;
            try {
                clientSocket = new Socket("ALIRAZA-HP",6789);
            } catch (UnknownHostException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
             DataOutputStream outToServer = null;
            try {
                outToServer = new DataOutputStream(clientSocket.getOutputStream());
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outToServer.writeBytes("s");
                clientSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                        
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
             Socket clientSocket = null;
            try {
                clientSocket = new Socket("ALIRAZA-HP",6789);
            } catch (UnknownHostException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
             DataOutputStream outToServer = null;
            try {
                outToServer = new DataOutputStream(clientSocket.getOutputStream());
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outToServer.writeBytes("u");
                clientSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                        
     
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
             Socket clientSocket = null;
            try {
                clientSocket = new Socket("ALIRAZA-HP",6789);
            } catch (UnknownHostException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
             DataOutputStream outToServer = null;
            try {
                outToServer = new DataOutputStream(clientSocket.getOutputStream());
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                outToServer.writeBytes("f");
                clientSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(control.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                        
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(control.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(control.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(control.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(control.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new control().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.Box.Filler filler1;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        // End of variables declaration                   
    }
    __________________________________________________ ______
    this is my code tell me where i write code for reciving.. thanks for your response ..

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: java Client Server program by using socket problem

    The only answer I can think of, that does not involve writing the code, is "step away from the drag-and-drop and learn the code".
    The Java tutorials
    Without sounding harsh that is my best advice to anyone learning Java

Similar Threads

  1. getting closed server socket in client thread
    By shanalikhan in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 13th, 2013, 05:18 PM
  2. unable to get server socket in client thread
    By shanalikhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 13th, 2013, 01:47 PM
  3. Replies: 0
    Last Post: February 24th, 2011, 05:31 AM
  4. client/server socket
    By Java_dude_101 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 18th, 2011, 12:16 AM
  5. Client Socket on Application Server
    By xevimaresma in forum Java Theory & Questions
    Replies: 0
    Last Post: April 12th, 2010, 07:00 AM

Tags for this Thread