繁体   English   中英

没有消息的Java聊天服务器

[英]Java chat server without messages

所以我从教程中获得了一个基本的聊天信息,一切都很好,除了一件事没有消息。 我的意思是服务器也正在运行客户端...但是发送按钮或其他任何问题吗?是的,是的.server很慢,有时甚至死机了。我使用了Netbeans的frame选项。

这是客户

package chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import java.net.Socket;

 public class chat_client extends javax.swing.JFrame {

static DataInputStream din;
static DataOutputStream dout;
static Socket s;


public chat_client() {
    initComponents();
}


private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try {
    String msgout = "";
   msgout = msg_text.getText().trim();
   dout.writeUTF(msgout);
    } catch(Exception e) {
           }

}                                        

public static void main(String args[]) {




    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new chat_client().setVisible(true);
        }
    });

    try {
         s = new Socket("127.0.0.1",1201); //The Local address because the Computer is the same

         din = new DataInputStream(s.getInputStream());
         dout = new DataOutputStream(s.getOutputStream());
         String msgin = "";


         while(msgin.equals("exit")) {
             msgin = din.readUTF();
             msg_area.setText(msg_area.getText().trim()+"\n Server:\t"+ msgin);

         }
    }catch (Exception e) {
}
}


}

这是服务器

package chat;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class chat_server extends javax.swing.JFrame {

 static ServerSocket ss;
 static Socket s;
 static DataInputStream din;
  static DataOutputStream dout;

  public chat_server() {
    initComponents();
}




    msg_text.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            msg_textActionPerformed(evt);
        }
    });

    msg_send.setText("Send");
    msg_send.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            msg_sendActionPerformed(evt);
        }
    });



private void msg_textActionPerformed(java.awt.event.ActionEvent evt) {                                         

}                                        

private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try {




    String msgout = "";
   msgout = msg_text.getText().trim();
   dout.writeUTF(msgout); // sending the server message to the client
  } catch (Exception e) {


  }
}                                        


public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new chat_server().setVisible(true);
        }
    });

    String msgin = "";
    try {
         ss = new ServerSocket(1201); // The server starting port
          s = ss.accept();             // allow Connection


          din = new DataInputStream(s.getInputStream());
         dout = new DataOutputStream(s.getOutputStream());

         while(!msgin.equals("exit")) {
             msg_area.setText(msg_area.getText().trim()+"\n"+msgin);  //Displaying message

    }

    }catch(Exception e) {

    }
}

                  }

在客户端代码中,在main方法中,while循环的条件是错误的,您错过了! 否定它(就像您实际上在服务器上所做的一样)。 因此应该是while (!msgin.equals("exit"))

在服务器代码中,您没有从输入流中读取(实际上是在客户端上读取),因此错过了msgin = din.readUTF(); while循环中的语句。
这也是服务器运行缓慢,不断循环并在文本上写入空文本的原因,而在读取输入流中的下一个输入时应该阻止该文本。

您可以对代码进行进一步的改进:

  • 对类名称使用驼峰式大小写 (例如,使用chatClient代替chat_client)
  • 使用日志记录机制(例如log4j ,或在此示例情况下,使用System.out.println("log here the "+variable)调用)来获取动作和交互的反馈
  • 不要将catch块留空 :您可能会错过异常。 因此,至少在catch块中添加System.out.err(e);类的语句System.out.err(e); (但同样,最好使用具有正确日志记录级别的日志库)
  • 发送消息后,从客户端的文本字段中清除文本(当前文本保留在那里)。 msg_text.setText(""); msg_sendActionPerformed方法的try块msg_sendActionPerformed就足够了。

暂无
暂无

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

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