0

I'm doing a simple chat application with TCP in java.

In my code i have add a GUI but with some reasons it doesn't work even i have connected to server.

I can't figure out what is the problem.

I run the GUI and it works.

Hope that someone can help.

Server:

public class Server {

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(3);
            Socket client = server.accept();
            System.out.println("Connected");
            String str;
            BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            while(true){
                str = "Connected to server";
                bw.write(str);
                bw.newLine();
                str = br.readLine();
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Client:

public class Client1 implements MouseListener {
    private JFrame jF;
    private JTextArea area;
    private JTextField field;
    private JButton send;
    String str;
    BufferedReader br;
    BufferedWriter bw;
    private Socket client;
    public Client1(){
        try {
            client = new Socket("localhost", 3);
            br = new BufferedReader(new InputStreamReader(client.getInputStream()));
            bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            str = br.readLine();

            jF = new JFrame("Simple Chat");
            jF.setSize(500, 500);
            jF.setLayout(null);
            jF.setResizable(true);
            jF.setLocationRelativeTo(null);

            area = new JTextArea();
            area.append(str);
            area.setLocation(0, 0);
            area.setSize(500,400);
            jF.add(area);

            field = new JTextField();
            field.setLocation(0,400);
            field.setSize(400,100);
            jF.add(field);

            send = new JButton("Send");
            send.setLocation(400, 400);
            send.setSize(100,100);
            send.addMouseListener(this);
            jF.add(send);

            jF.setVisible(true);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        new Client1();
    }
}
Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
aU sUd
  • 35
  • 8
  • For [example](http://stackoverflow.com/a/3245805/230513). – trashgod Nov 30 '15 at 17:32
  • @trashgod i'm thinking about 2 client connected to 1 server, chat with each other. I'm not sure how can i update the textarea in each client to not appear their own chat twice. That's my problem now . – aU sUd Nov 30 '15 at 18:25
  • where did you override mouselistener methods? if you click "send" it should connect to server and update your text area with socket's outputstream right? – SomeDude Nov 30 '15 at 20:35
  • 2
    Swing is a single threaded frame work, your call to `br.readLine` will block the EDT potentially preventing it from updating the UI in any, meaningful manner. You're also think in a very procedural manner, where as GUIs are event driven environments. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for more details – MadProgrammer Nov 30 '15 at 21:31

0 Answers0