Results 1 to 5 of 5
  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    257
    Rep Power
    3

    Exclamation Good Example for Multi user with single ServerSocket with GUI

    Good Evenig
    i have confusion about "Multi user with single ServerSocket with GUI"

    i can try in CUI , but in GUI ?

    can you show me a good example for i learn please..,

  2. #2
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    257
    Rep Power
    3

    Exclamation Re: Good Example for Multi user with single ServerSocket with GUI

    I wants to make it as a Multi User Login from a Single User Login (below code)


    This Is My code for single user with serversocket

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    
    /**
     *
     * @author
     */
    public class ServerMain
    {
        public static void main(String[] args)
        {
            serverFm sf=new serverFm();
            sf.setVisible(true);
            sf.service();
                    }
    }

    Code For Server

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * serverFm.java
     *
     * Created on May 1, 2012, 10:25:17 AM
     */
    /**
     *
     * @author
     */
    import java.net.*;
    import java.io.*;
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;
    public class serverFm extends javax.swing.JFrame {
        ServerSocket server = null;
        Socket client = null;
        BufferedReader in = null;
        PrintWriter out = null;
        String UName;
        String Passw;
        String getd;
    
        /** Creates new form serverFm */
        public serverFm() {
            initComponents();
            UName = "";
            Passw="";
            getd="";
        }
        public void service()
        {
            try
            {
                server = new ServerSocket(1099);
                client = server.accept();
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(rootPane,"Accept failed :");
                        }
            try
            {
                in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                out = new PrintWriter(client.getOutputStream(), true);
            } catch (IOException e)
            {
                JOptionPane.showMessageDialog(rootPane,"Read failed");
                System.exit(-1);
            }
            if(true)
            {
                try
                {
                    UName = in.readLine();
                    Passw=in.readLine();
                    getd=in.readLine();
                    
                    //Send data back to client
                     out. out.println(UName);
                    //out.println(Passw);
                             catch (IOException ex) {
                    Logger.getLogger(serverFm.class.getName()).log(Level.SEVERE, null, ex);
                }        } 
        }
    
        /** 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() {
    
            hai = new javax.swing.JTextField();
            pwws = new javax.swing.JPasswordField();
            jButton1 = new javax.swing.JButton();
            dttte = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosed(java.awt.event.WindowEvent evt) {
                    formWindowClosed(evt);
                }
            });
    
            jButton1.setText("Get");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(168, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(183, 183, 183))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(dttte, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(238, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(147, 147, 147)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(pwws, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(hai, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE))
                    .addContainerGap(96, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(dttte, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(hai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(pwws, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(26, 26, 26)
                    .addComponent(jButton1)
                    .addContainerGap(19, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    }                                        
    
    private void formWindowClosed(java.awt.event.WindowEvent evt) {                                  
    // TODO add your handling code here:
        try
            {
                in.close();
                out.close();
                server.close();
            }
            catch (IOException e)
            {
                JOptionPane.showMessageDialog(rootPane,"Could not close.");
                System.exit(-1);
            }
     
    }                                 
    
        /**
         * @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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            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(serverFm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(serverFm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(serverFm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(serverFm.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 serverFm().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JLabel dttte;
        private javax.swing.JTextField hai;
        private javax.swing.JButton jButton1;
        private javax.swing.JPasswordField pwws;
        // End of variables declaration                   
    }


    This Is the Client Code

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    
    /**
     *
     * @author 
     */
    public class ClientMain
    {
        public static void main(String[] args)
        {
            clientFm c = new clientFm();
            c.setVisible(true);
            c.serviceclint();
        }
    }
    Client Code

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * clientFm.java
     *
     * Created on May 1, 2012, 10:35:47 AM
     */
    import java.net.*;
    import java.io.*;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;
    /**
     *
     * @author
     */
    public class clientFm extends javax.swing.JFrame {
        Socket socket = null;
        private BufferedReader in = null;
        private PrintWriter out = null;
        private String UsrNam = null;
        private String PassWrd = null;
        private String resp = null;
        private Date dt=null;
        
    
        /** Creates new form clientFm */
        public clientFm() {
            initComponents();
            UsrNam = "";
            PassWrd="";
            resp="";
            dt=Calendar.getInstance().getTime();
        }
    public void serviceclint()
    {
            try {
                try
                {
                        socket = new Socket("192.168.1.8", 1099);
                        out = new PrintWriter(socket.getOutputStream(), true);
                        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        resp=in.readLine();
                        JOptionPane.showMessageDialog(rootPane, "Welcome" +resp);
                        
                    }
                    catch (UnknownHostException e)
                    {
                        System.out.println("Unknown host: ");
                                }
                    
                    catch  (IOException e)
                    {
                        System.out.println("No I/O");
                                
                }
                in.close();
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(clientFm.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
        /** 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() {
    
            unc = new javax.swing.JTextField();
            Passwwwd = new javax.swing.JPasswordField();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            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(76, 76, 76)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(Passwwwd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                        .addComponent(unc, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                    .addGap(114, 114, 114))
                .addGroup(layout.createSequentialGroup()
                    .addGap(106, 106, 106)
                    .addComponent(jButton1)
                    .addContainerGap(148, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(44, 44, 44)
                    .addComponent(unc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(Passwwwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(22, 22, 22))
            );
    
            pack();
        }// </editor-fold>                        
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
        UsrNam= unc.getText();
        PassWrd=Passwwwd.getText();
            out.println(UsrNam);
            unc.setText(new String(""));
            out.println(PassWrd);
            Passwwwd.setText(new String(""));
            out.println(dt);
            //Receive text from server
            
    }                                        
    
        /**
         * @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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            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(clientFm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(clientFm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(clientFm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(clientFm.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 clientFm().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JPasswordField Passwwwd;
        private javax.swing.JButton jButton1;
        private javax.swing.JTextField unc;
        // End of variables declaration                   
    }

    Any one can Help me..,

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Good Example for Multi user with single ServerSocket with GUI

    Quote Originally Posted by raj.mscking@gmail.com View Post
    i can try in CUI , but in GUI ?
    can you show me a good example for i learn please..,
    Seems to be a long way... GUI means Graphical User Interface... whatever "CUI" may be...
    I like likes!

  4. #4
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    257
    Rep Power
    3

    Default Re: Good Example for Multi user with single ServerSocket with GUI

    in Character User Interface "CUI"
    i can make a service class with thread to handle the Multi client,
    But in GUI
    i can start the server thereafter no further action at all & the server window become Black in color.., (you can see pictures in my previous forums also)

    you may help me to convert the above code access by multiple user in GUI..,
    Thank you..,

  5. #5
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    257
    Rep Power
    3

    Default Re: Good Example for Multi user with single ServerSocket with GUI

    this is the Server code for which i use in cui for handle multiple client

    Java Code:
                                     
            try
            {
             ServerSocket ss=new ServerSocket(1030);
                    try
                    {
                        JOptionPane.showMessageDialog(null,"Server is runing");
                   while(true)
               {
                   Socket s=ss.accept();
                   Service a=new Service(s);
                   a.start();
               }
                    }
                    catch(Exception e)
                    {
                        System.out.println(e);
                    }
            }
            catch(IOException ex)
            {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null,ex);
            }

    this is the thread for client handler class


    Java Code:
    package test;
    
    
    import tcp.*;
    import java.io.*;
    import java.net.*;
    import java.sql.*;
    import test.Server;
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author
     */
    public class Service extends Thread 
    {
            private Socket s=null;
        private PrintStream out= null;
        private DataInputStream in=null;
        BufferedReader input=null;
        public Service(Socket t)throws Exception             
            {
                 s=t;
                 out=new PrintStream(s.getOutputStream());
                 in=new DataInputStream(s.getInputStream());
            }
            public void run()
            {
                try
        {
                    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    PrintWriter output = new PrintWriter(s.getOutputStream(),true);
                    while(true)
                    {
                    String userName = input.readLine();
                    String password = input.readLine();
                    String getd=input.readLine();
                    out.println(UName);
                   
        }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                        
                        
                        
                    }
            }
    }

    can you help me..,

Similar Threads

  1. Showing multi frames within a single main frame
    By chandrupsekar in forum AWT / Swing
    Replies: 6
    Last Post: 11-24-2011, 03:55 PM
  2. Multi-Jpanels in single JFrame
    By _-Blackhawk-_ in forum AWT / Swing
    Replies: 10
    Last Post: 09-13-2011, 03:52 PM
  3. Single to multi-dimension array
    By Migy in forum New To Java
    Replies: 2
    Last Post: 04-05-2011, 06:12 PM
  4. Single user Login authentication
    By vinoopraj in forum Web Frameworks
    Replies: 0
    Last Post: 07-20-2010, 08:19 AM
  5. Single and Multi Selection Lists
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-02-2008, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •