繁体   English   中英

客户端服务器聊天应用程序gui

[英]client server chat application gui

我正在使用tcp编写一个简单的客户端服务器聊天应用程序。 但是消息不会转发到客户端/服务器,而是像无限循环那样在终端中出现真正的隐秘错误,直到我终止进程或关闭应用程序。 任何人都可以建议对代码进行更改。

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

客户端和服务器的这一部分相同

现在主要

服务器主要方法

    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

客户主要方法

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

就无限错误循环而言,我认为这是因为您在应用程序启动时正在调用方法getMsg()

cli.getMsg();

永远运行这个while循环

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

while总是正确的,并且异常会被捕获并且堆栈跟踪会不断出现垃圾邮件,这可能是因为它试图获取尚不存在的消息,因为它会在启动之前在任何消息存在之前运行? 也许我不确定。

尝试删除try块的catch部分,看看是否可以发送消息而不会被堆栈跟踪发送垃圾邮件。 像这样...

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

这不是最佳实践,但是它可以使您满怀希望地前进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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