简体   繁体   中英

client server chat application gui

I am writing a simple client server chat application using tcp . But the messages are not forwarded to the client/server instead i get really cryptic errors in terminal like in infinite loop until I kill the process or close the application. Can anybody please suggest the changes in code.

class UIserver extends JFrame implements ActionListener {
JTextArea textArea;
JButton sendButton;
JTextField textField;
JScrollPane scrollpane ;
DataOutputStream dos = null;
DataInputStream dis = null;
Scanner scanner = new Scanner(System.in);


public UIserver(){
    this.setTitle("Server");
    this.setLayout(new FlowLayout());

    textArea = new JTextArea(30,50);
    textArea.setBackground(Color.white);
    textArea.setLayout(new FlowLayout());
    scrollpane = new JScrollPane(textArea);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
    this.add(scrollpane);

    sendButton = new JButton("Send");
    this.add(sendButton);
    sendButton.addActionListener(this);


    textField = new JTextField(30);
    this.add(textField);


    this.setVisible(true);
    this.setSize(600,600);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}//end ctor

public void setDataOutput(DataOutputStream dos){
    this.dos = dos;
}

public void setDataInput(DataInputStream dis){
    this.dis = dis;
}

public void getMsg(){

    new Thread(new Runnable(){
        public void run (){
            while(true){
                try {
                    String msg = dis.readUTF();
                    textArea.append("From Client :- "+msg+"\n");
                }catch(Exception e){e.printStackTrace();}       
            }//end while
        }
    }).start();

}//end getmsg

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == sendButton) {
        new Thread(new Runnable(){
            public void run (){
                while(true){
                    try{
                        String msgsend = scanner.nextLine();
                        textArea.append("To Client :- "+msgsend+"\n");
                        //dos.writeUTF(msgsend);    
                    }catch(Exception e){e.printStackTrace();}   

                }//end while        
            }
        }).start();
    }//end if

}//end actionPerformed

This part is same for both client and server

and now main

Server main method

    public static void main(String[] args) throws Exception {
    UIserver usi = new UIserver();

    ServerSocket socket = new ServerSocket(5000);
    Socket server = socket.accept();

    DataInputStream dis = new DataInputStream(server.getInputStream());
    DataOutputStream dos = new DataOutputStream(server.getOutputStream());

    usi.setDataInput(dis);
    usi.setDataOutput(dos); 
    usi.getMsg();
}//end main

client main method

public static void main(String[] args) throws Exception {
    UIclient cli = new UIclient();

    Socket socket = new Socket("localhost",5000);

    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    DataInputStream dis = new DataInputStream(socket.getInputStream());

    cli.setDataInput(dis);
    cli.setDataOutput(dos);
    cli.getMsg();
}//end main

As far as the infinite error loops, I think it's because you are calling your method getMsg() on startup of the application

cli.getMsg();

which runs this while loop forever

while(true){
    try {
        String msg = dis.readUTF();
        textArea.append("From Client :- "+msg+"\n");
    }catch(Exception e){e.printStackTrace();}       
}

The while is always true and the exception is getting caught and endless spamming of the stack trace, possibly because it's trying to get messages that don't exist yet because it runs at startup before any messages exist?? Maybe, i'm not sure.

Try removing the catch part of the try block and see if you can send messages without getting spammed with the stack trace. Like this...

while(true){
    try {
        String msg = dis.readUTF();
        textArea.append("From Client :- "+msg+"\n");
    }
    finally{}
}

This is not best practice but it's something to get you moving forward hopefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM