【问题标题】:java chat server see online accountsjava聊天服务器查看在线帐户
【发布时间】:2011-11-09 20:50:10
【问题描述】:

我正在 Java 上实现一个基本的聊天服务器。其实我已经完成了一些基本的事情并试图改进它。

现在我想尝试制作一个显示谁在线的列表。我有个问题。我在JTextPane 组件上得到字符串,当我按“ENTER”时它会发送消息,但文本光标会移到下一行。我使用setCaretPosition(0);,但它没有用。

看下图就明白问题所在了。

这是我的服务器和服务器线程代码;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class Server {

    private ServerSocket ss;
    private Socket s;
    Map<Socket,DataOutputStream> list = new HashMap<Socket,DataOutputStream>();

    public Server(int port_number) throws IOException{

        create_Server(port_number);
    }

    public static void main(String[] args) throws IOException {

        int port_number=23;

        new Server(port_number);
    }

    private void create_Server(int port_number) throws IOException{

        ss = new ServerSocket(port_number);

        System.out.println("Server is ready!");

        while(true){

            s=ss.accept();

            System.out.println(s.getLocalAddress().getHostName() + " was connected!");

                        send_con_mes();

            list.put(s,new DataOutputStream(s.getOutputStream()) );

            new ServerThread(s,this).start();
        }

    }

        private void send_con_mes() throws IOException{

                Set<Socket> sckt_list = list.keySet();
        Iterator<Socket> itr = sckt_list.iterator();
                DataOutputStream daos;

                String str;
                Socket sckt;

                while(itr.hasNext()){

                    sckt=itr.next();
                    str = sckt.getLocalAddress().getHostName() + " was connected..." ;

                    daos = list.get(sckt);
                    daos.writeUTF(str);

                }

        }

    public void send_to_All(String msg, Socket socket) throws IOException{

        synchronized(list){

            Set<Socket> sckt_list = list.keySet();
            Iterator<Socket> itr = sckt_list.iterator();
            DataOutputStream daos;

            while(itr.hasNext()){

                Socket sck = itr.next();

                if(sck!=socket){

                    daos = list.get(sck);
                    daos.writeUTF(msg);

                }
            }
        }
    }

    public void remove_Connection(Socket s) throws IOException{

        synchronized(list){

            list.remove(s);
            System.out.println(s.getLocalAddress().getHostName() + " was disconnected!");
            s.close();
        }
    }

}

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;


public class ServerThread extends Thread {

    private Socket s;
    private Server srv;

    public ServerThread(Socket s,Server srv){
        this.s = s;
        this.srv = srv;
    }

    @Override
    public void run() {

        String msg;
                DataInputStream dis;

        try {
            dis = new DataInputStream(s.getInputStream());

            while(true){

                msg = dis.readUTF();
                srv.send_to_All(msg, s);

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            try {
                srv.remove_Connection(s);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

所以我想我必须在 server.java 中实现列表,但问题是如何传递列表并在 client.java 中使用它。这是我的客户代码:

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Client.java
 *
 * Created on 02.Eyl.2011, 16:46:44
 */
/**
 *
 * @author BURAKTAS
 */
public class Client extends javax.swing.JFrame implements Runnable {

    private DataOutputStream dos;
    private DataInputStream dis;
    private Socket s;
    private String Client_name;
    private String Ip_addr;
    private DefaultListModel listModel=new DefaultListModel();


    /** Creates new form Client */
    public Client() {
        initComponents();
        Screen.setEditable(false);

        acc_list.setVisibleRowCount(6);
        listModel.addElement(Client_name);



        start_Chat();

    }

    @Override
    public void run() {

        try {

            while(true){

            String message = dis.readUTF();
            Screen.append(message + "\n");
            }

        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }

    }



    public void start_Chat() {
        try {

            Ip_addr = JOptionPane.showInputDialog("Enter the IP number of the server to connect : ");
            s = new Socket(Ip_addr, 23);

            Client_name = JOptionPane.showInputDialog("Enter your Nickname : ");

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

            new Thread(Client.this).start();

        } catch (UnknownHostException ex) {
            JOptionPane.showMessageDialog(rootPane, "Socket can not connected", "ERROR", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(rootPane, "Socket can not connected", "ERROR", JOptionPane.ERROR_MESSAGE);
            start_Chat();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        font_chooser = new javax.swing.JColorChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        Screen = new javax.swing.JTextArea();
        Send_Button = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        acc_list = new javax.swing.JList();
        disco_button = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        Text_Field = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BrkChat_Server vs 1.0");
        setResizable(false);

        Screen.setColumns(20);
        Screen.setRows(5);
        jScrollPane1.setViewportView(Screen);

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

        acc_list.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        acc_list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane2.setViewportView(acc_list);

        disco_button.setText("Disconnect");
        disco_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disco_buttonActionPerformed(evt);
            }
        });

        Text_Field.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        Text_Field.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                Text_FieldKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(Text_Field);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(disco_button, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                    .addComponent(Send_Button, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Send_Button, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(disco_button, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>

private void write(){

            try {

            Text_Field.setCaretPosition(0);
            String str = Text_Field.getText();

            dos.writeUTF(Client_name + " : " + str);
            Screen.append(Client_name + " : " + str + "\n");
            Text_Field.setText("");
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
}

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

    write();

}                                           

private void disco_buttonActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            s.close();
            this.setVisible(false);
            System.exit(0);
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
}

private void Text_FieldKeyPressed(java.awt.event.KeyEvent evt) {

        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            write();

        }
}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new Client().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextArea Screen;
    private javax.swing.JButton Send_Button;
    private javax.swing.JTextPane Text_Field;
    private javax.swing.JList acc_list;
    private javax.swing.JButton disco_button;
    private javax.swing.JColorChooser font_chooser;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration

}

如果您能帮助我,我将不胜感激。无论如何,谢谢。

【问题讨论】:

  • 附注 - camelCase - 你的变量不应该以大写开头。

标签: java list chat


【解决方案1】:

这是因为您清除了侦听器keyPressed 方法中的文本字段。因此,关键事件仍然存在,并且随后添加了文本(在本例中为换行符)。快速而简单的解决方法是在您致电write() 之前添加evt.consume();

【讨论】:

  • 是的,我明白,它正在工作。小问题解决了 :) 谢谢老兄。
【解决方案2】:

实现这一点的一种方法是实现一些包含用户名列表的事件,该用户名列表会在用户每次连接/断开连接时发送给所有用户。为简单起见,您还可以从 FTP 实现中取出一个页面,并为每个用户使用两个通道 - 一个用于文本,另一个用于系统命令。

无论哪种方式,您基本上都应该序列化包含所有必要信息的事件并将其发送给所有客户端。然后那些基本上只需要反序列化它,检查他们有什么样的事件并相应地处理它。这样一来,您就可以让 Java 序列化 API 处理所有复杂的工作——尽管您也可以定义自己的协议并使用 protobuf 或其他东西(更高效;更多工作)。

【讨论】:

  • 好吧,我大约 10 天前开始使用 java 的网络主题。所以我对网络有点初学者:)。我不知道如何从 FTP 中取出一个页面,即使使用两个通道,我也不了解两个通道的情况,我想我必须将列表传递给客户端 GUI 代码。无论如何,我想我必须在网络上进一步提高自己...... :) 但无论如何谢谢
  • FTP 部分只是关于使用两个通道(如果需要,可以使用套接字)的 FTP 协议,您不必那样做。是的,您仍然必须将列表传递给用户 - 这就是第二段的内容。序列化列表 -> 发送给所有用户 -> 反序列化 -> 使用列表。事件方法只允许您稍后添加其他事件。
  • 好的,现在我明白了,是的,我猜这在逻辑上是正确的。我会试试看 :)。谢谢队友
猜你喜欢
  • 2014-01-22
  • 1970-01-01
  • 2017-07-06
  • 2015-02-17
  • 2021-07-23
  • 2016-11-07
  • 1970-01-01
  • 2015-09-25
  • 1970-01-01
相关资源
最近更新 更多