import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;public class MyChat extends Frame{
List lst = new List(6);
TextField ip = new TextField(15);
TextField text = new TextField(20);
    DatagramSocket socket;
MyChat(){
//from EchoServer.java
try
{
socket = new DatagramSocket( 3000 );
}
catch (Exception e)
{
System.err.println ("Unable to bind port");
}
this.add(lst,"Center");
Panel p = new Panel();
this.add(p,"South");
p.add(ip);
p.add(text);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
text.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
byte[] buf;
buf = text.getText().getBytes();
DatagramPacket packet = new DatagramPacket(buf,buf.length);
packet.setPort(3000);
packet.setAddress(InetAddress.getByName(ip.getText()));
socket.send(packet);
}catch(Exception iof){}
text.setText("");
}
});

Server r1 = new Server();
Thread server = new Thread(r1);
server.start();
}
class Server implements Runnable{

public void run(){
while(true){
try{
//lst.add("iop");
byte [] buf = new byte[1024];
            DatagramPacket pac = new DatagramPacket(buf,1024);
            DatagramSocket soc = new DatagramSocket(3000);
soc.receive(pac);
lst.add("iop");
lst.add(new String(buf,0,pac.getLength())+"Packet received from "+pac.getAddress()+","+pac.getPort());
}catch(IOException ioe){}
}
}
}
public static void main(String args[]) {
System.out.println("Starting MyChat...");
MyChat mainFrame = new MyChat();
mainFrame.setSize(350, 400);
mainFrame.setTitle("MyChat");
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}

}
张孝祥的:正确的例子/*
 * @(#)Chat.java 1.0 04/09/23
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */
package Chat;import java.awt.*;
import java.awt.event.*;
import java.net.*;class Chat extends Frame {
List lst = new List(6);
TextField tfIP = new TextField(15);
TextField tfData = new TextField(20);
DatagramSocket ds = null; public Chat() {
try
{
     ds = new DatagramSocket(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
this.add(lst,"Center");
Panel p = new Panel();
this.add(p,"South");
p.setLayout(new BorderLayout());
p.add(tfIP,"West");
p.add(tfData,"East");



new Thread(new Runnable()
{
public void run()
{
byte [] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,1024);
while(true)
{
    try
    {
    
     ds.receive(dp);
        lst.add(new String(/*dp.getData()*/buf,0,dp.getLength()) + " from " + dp.getAddress().getHostAddress() /*InnetAdress.getHostAdress()=dp.getAdress(),可查UDP*/ + ":" + dp.getPort(),0);
        } 
        catch(Exception e)
        {
            if(!ds.isClosed())
            {
            e.printStackTrace();
            }
        }
        }
}
}).start();

tfData.addActionListener(new ActionListener()//可与“WindowAdapter()比较”
{
  public void actionPerformed(ActionEvent e)
  {
   byte [] buf;
   buf = tfData.getText().getBytes();//!!!!!!tfData.getText()的返回值.getBytes()
   try
   {
   DatagramPacket dp = new DatagramPacket(buf,buf.length,
   InetAddress.getByName(tfIP.getText()),3000);
   ds.send(dp); 
    }
    catch(Exception ex)
    {
     ex.printStackTrace();
    }
   tfData.setText("");
  
  }
});


addWindowListener(new WindowAdapter() {//应该是WindowAdapter()创建WindowListener对象作为参数给addWindowListener
public void windowClosing(WindowEvent e) {
ds.close();
dispose();//能直接用吗?无实例化?
System.exit(0);
}
});
} public static void main(String args[]) {
System.out.println("Starting Chat...");
Chat mainFrame = new Chat();
mainFrame.setSize(300, 400);
mainFrame.setTitle("Chat");
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}
}

解决方案 »

  1.   

    class Server implements Runnable{
    public void run(){
       while(true){
       try{
                 //lst.add("iop"); 
                 byte [] buf = new byte[1024];
       DatagramPacket pac = new DatagramPacket(buf,1024);
        // DatagramSocket soc = new DatagramSocket(3001);   
       socket.receive(pac);
       lst.add("iop");
     lst.add(new String(buf,0,pac.getLength())+"Packet received from "+pac.getAddress()+","+pac.getPort());
     }catch(IOException ioe){
    ioe.printStackTrace();
     }
     }
     }
    }