为什么聊天室只接受一次数据?
程序开始运行要自己指定IP地址 
package chat;import java.awt.*;
import java.awt.event.*;
import java.net.*;/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class chat {
    Frame f = new Frame("我的聊天室");
    TextField tf1;
    List lis = new List();
    DatagramSocket ds;
    byte[] a = new byte[1024];
    public chat() {
        try {
            ds = new DatagramSocket(3000);
        } catch (Exception e) {}
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        DatagramPacket dp = new DatagramPacket(a,1024);                        ds.receive(dp);
                        lis.add(new String(a), 0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }    public static void main(String args[]) {
        new chat().init();
    }    public void init() {
        f.setBounds(0, 0, 500, 500);
        f.setLayout(new GridLayout(2, 1));
        f.add(lis);
        Panel p = new Panel(new GridLayout(1, 2));
        tf1 = new TextField(15);
        TextField tf2 = new TextField(15);
        p.add(tf1);
        p.add(tf2);
        f.add(p);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                f.setVisible(false);
                ds.close();
                System.exit(0);            }
        });
        tf2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DatagramPacket dp = null; //
                try {
                    dp = new DatagramPacket(e.getActionCommand().getBytes(), 0,
                                            e.getActionCommand().length(),
                                            InetAddress.getByName(tf1.getText()),
                                            3000);
                } catch (Exception ex) {}
                try {
                    ds = new DatagramSocket();
                } catch (Exception ex) {}
                try {
                    ds.send(dp);
                } catch (Exception ex) {}
                ((TextField) e.getSource()).setText("");
            }
        });
    }
}

解决方案 »

  1.   

    tf2.addActionListener(new ActionListener() 的actionPerformed方法中的ds = new DatagramSocket();需要重新定义变量,否则改变了chat线程中的ds对象.
      

  2.   

    ds被你自己改变了撒。
    改为:
    tf2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    DatagramPacket dp = null; //
                    DatagramSocket ds_ = null;
                    try {
                        dp = new DatagramPacket(e.getActionCommand().getBytes(), 0,
                                                e.getActionCommand().length(),
                                                InetAddress.getByName(tf1.getText()),
                                                3000);
                    } catch (Exception ex) {}
                    try {
                     ds_ = new DatagramSocket();
                    } catch (Exception ex) {}
                    try {
                     ds_.send(dp);
                    } catch (Exception ex) {}
                    ((TextField) e.getSource()).setText("");
                }
            });
      

  3.   

    而且前面有个地方也要改一下,不然会出错:
    new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        try {
                        a = new byte[1024];
                         DatagramPacket dp= new DatagramPacket(a,1024);
                            ds.receive(dp);
                            System.out.println(new String(a, 0, a.length));
                            lis.add(new String(a, 0, a.length));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();