Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Chat bug

  1. #1
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post

    Chat bug

    Hey guys i tried to make a java chat. It actually worked but it got a weird bug.
    It's hard to explain but i'll give my best.

    First i run the server.
    Then 2 clients : chat A chat B.

    At chat A i type : "Hello". and the server receives "Hello".
    Then at chat B i type : "Hi". and the server receives "Hi".
    Then at chat A i type : "Yo"... server receives "Yo"

    But when i type "Yay" at chat B the server does not receive that. It only receives data from chat A.


    ------------------

    Output example at server :

    Chat A :
    Hello
    Yo
    wtf is happening?
    lalala

    Chat B :
    Hi
    Yay

    Server log :
    Hello
    Hi
    Yo
    (should say Yay but i doesn't)
    wtf is happening?
    lalala

    ServerSide script
    Code:
    import java.awt.List;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class Servidor {
    
        ServerSocket server;
        Scanner scanner;
        PrintWriter[] escritores = new PrintWriter[10];
        int indexEscritores = 0;
        
        private void encaminharParaTodos(String texto){
            for (int i = 0; i < indexEscritores; i++){
                try{
                    escritores[i].println(texto);
                    escritores[i].flush();
                }catch(Exception yfasfd){}
            }
        }
        
        public Servidor() {
            try {
                server = new ServerSocket(5000);
                while (true) {
                    Socket s = server.accept();
                    new Thread(new EscutaCliente(s)).start();
                    PrintWriter p = new PrintWriter(s.getOutputStream());
                    escritores[indexEscritores++] = p;
                }
            } catch (Exception e) {
            }
        }
    
        private class EscutaCliente implements Runnable {
    
            public EscutaCliente(Socket socket) {
                try {
                    scanner = new Scanner(socket.getInputStream());
                } catch (Exception y) {}
            }
    
            @Override
            public void run() {
                try{
                    String texto;
                    while((texto = scanner.nextLine()) != null){
                        System.out.println(texto);
                        encaminharParaTodos(texto);
                    }
                }catch(Exception z){}
            }
        }
        
        public static void main(String[] args) {
            new Servidor();
        }
    }
    Client Script

    Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Scanner;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    
    public class Cliente extends JFrame {
        
        JTextField comandoParaEnviar;
        Socket socket;
        PrintWriter escritor;
        JTextArea textoRecebido;
        Scanner leitura;
        
        private class EscutaServidor implements Runnable{
    
            @Override
            public void run() {
                try{
                    String texto;
                    while((texto = leitura.nextLine()) != null){
                        textoRecebido.insert(texto + "\n", 0);
                    }
                }catch(Exception e){}
            }
             
        }
        
        public Cliente() {
            
            super("Cliente");
            
            Font fonte = new Font("Serif", Font.PLAIN, 26);
            comandoParaEnviar = new JTextField();
            comandoParaEnviar.setFont(fonte);
    
            //addKeyListenet
            comandoParaEnviar.addKeyListener(new KeyAdapter() {
                @Override
                public void keyReleased(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                        escritor.println(comandoParaEnviar.getText());
                        escritor.flush();
                        comandoParaEnviar.setText("");
                        comandoParaEnviar.requestFocus();
                    }
                }            
            });
            
            JButton botao = new JButton("Enviar");
            botao.setFont(fonte);
            botao.addActionListener(new EnviarListener());
            Container envio = new JPanel();
            envio.setLayout(new BorderLayout());
            
            envio.add(BorderLayout.CENTER, comandoParaEnviar);
            envio.add(BorderLayout.EAST, botao);
            
            
    
            //chamar configuracao de rede
            configurarRede();
            
            
            textoRecebido = new JTextArea();
            textoRecebido.setFont(fonte);
            textoRecebido.setEditable(false);
            JScrollPane scroll = new JScrollPane(textoRecebido);
            getContentPane().add(BorderLayout.CENTER, scroll);
            getContentPane().add(BorderLayout.SOUTH, envio);
            
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(500, 300);
            setVisible(true);
        }
        
        private class EnviarListener implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                escritor.println(comandoParaEnviar.getText());
                escritor.flush();
                comandoParaEnviar.setText("");
                comandoParaEnviar.requestFocus();
            }
        }
        
        private void configurarRede() {
            try {
                socket = new Socket("127.0.0.1", 5000);
                escritor = new PrintWriter(socket.getOutputStream());
                leitura = new Scanner(socket.getInputStream());
                new Thread(new EscutaServidor()).start();
            } catch (Exception e) {
            }
        }
        
        public static void main(String[] args) {
            new Cliente();
        }
    }
    Last edited by sorlaker; 01-16-2013 at 06:35 PM.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Posts
    173
    Thanks
    24
    Thanked 1 Time in 1 Post
    Figure out how to do that here thanks. http://www.java2s.com/Code/Java/Swin...tringvalue.htm


  •  

    Posting Permissions

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