【问题标题】:client server chat application gui客户端服务器聊天应用程序 GUI
【发布时间】:2017-10-02 17:40:38
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: java swing server client


    【解决方案1】:

    就无限错误循环而言,我认为这是因为您在应用程序启动时调用了您的方法 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{}
    }
    

    这不是最佳做法,但它可以让您充满希望地向前迈进。

    【讨论】:

    • 是的。至少在理论上可能是 getmsg() 的情况。
    猜你喜欢
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多