别人的例子:
import java.net.*;
import java.io.*;public class UDPServer
{
  protected static int defaultPort = 0;
  protected static int defaultBufferLength = 65507;  public static void main(String[] args)
  {
    DatagramPacket incoming;    int port;
    int len;    try {
      port = Integer.parseInt(args[0]);
    }
    catch (Exception e) {
      port = defaultPort;
    }
    try {
      len = Integer.parseInt(args[1]);
    }
    catch (Exception e) {
      len = defaultBufferLength;
    }    try {
      DatagramSocket ds = new DatagramSocket(port);
      byte[] buffer = new byte[len];
      while (true)
      {
        incoming = new DatagramPacket(buffer, buffer.length);
        try
        {
          ds.receive(incoming);
          respond(ds, incoming);
        }
        catch (IOException e) {
          System.err.println(e);
        }
      } // end while
    }  // end try
    catch (SocketException se) {
      System.err.println(se);
    }  // end catch  }  // end main  public static void respond(DatagramSocket ds, DatagramPacket dp) {
    ;
  }}

解决方案 »

  1.   

    UPDClient端:
    import java.net.*;
    import java.io.*;/**
     * This class allows you to send and receive Strings and byte arrays via UDP
     * without concerning yourself with DatagramPackets and DatagramSockets.
     * @version 1.0 of June 1, 1996
     * @author Elliotte Rusty Harold
     */
    public class UDPClient {  InetAddress ia;
      int port;
      DatagramSocket ds;  /**
       * Creates a new UDPClient.
       * @param ia The InetAddress of the remote host to which data will be sent
       * @param port The port on the remote host to which data will be sent
       * @throws SocketException
       */
       public UDPClient(InetAddress ia, int port) throws SocketException {    this.ia = ia;
        this.port = port;
        ds = new DatagramSocket();  }  public UDPClient(String hostname, int port) throws UnknownHostException, SocketException {    this(InetAddress.getByName(hostname), port);  }  /**
       * This method sends data to the remote host via UDP. If the byte is longer than
       * the maximum reliable length of a UDP Datagram (64900) bytes then an
       * IOException is thrown
       * @param buffer A byte array containing the data to be sent
       * @throws IOException
       */
      public void send(byte[] buffer) throws IOException {    if (buffer.length > 64900) throw new IOException();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, port);
        ds.send(dp);
        System.out.println("send(byte[] buffer) ");  }  /**
       *
       * This method sends an ISO-Latin-1 string to the remote host via UDP.
       * The string will be truncated to ISO-Latin-1 even if it's Unicode.
       * @param s The string to be sent
       * @throws IOException
       */
      public void send(String s) throws IOException
      {
        System.out.println("send(String s) throws IOException ");
        byte[] data = new byte[s.length()];
        s.getBytes(0, s.length(), data, 0);
        send(data);  }  /**
       * This method sends an empty datagram to the remote host via UDP.
       * @throws IOException
       */
      public void send() throws IOException {    byte[] b = new byte[1];
        send(b);  }  /**
       * This method blocks until a UDP Datagram is received from the host with which
       * this UDPClient communicates. This can be an indefinite amount of time if
       * the host is unreachable so calls to this method should be placed in a separate
       * thread from the main program.
       * @return the data received as a byte array
       * @throws IOException
       */
      public synchronized byte[] receive() throws IOException {    byte[] buffer = new byte[65507];
        DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
        ds.receive(incoming);
        // a client should only receive data from the host to
        while ( !incoming.getAddress().equals(ia)) {
          ds.receive(incoming);
        }
        return incoming.getData();  }  /**
       * This method blocks until a UDP Datagram is received from the host with which
       * this UDPClient communicates. This can be an indefinite amount of time if
       * the host is unreachable so calls to this method should be placed in a separate
       * thread from the main program. When data is received it is
       * converted into an ISO-latin-1 String and returned.
       * @return the data received as a byte array
       * @throws IOException
       */
      public synchronized String receiveString() throws IOException {    byte[] data = receive();
        return new String(data, 0, 0, data.length);  }  /**
       * @return the port which this object sends data to
       */
      public int getPort() {    return port;  }  /**
       * @return the port which this client is bound to
       */
      public int getLocalPort() {    return ds.getLocalPort();  }  /**
       * @return the InetAddress which this client sends data to
       */
      public InetAddress getAddress() {    return ia;  }  /**
       * @return a String showning the remote host and port which this client sends data to
       */
      public String toString() {    return ia + ":" + port;  }   public static void main(String args[])
        {
        try
        {
           InetAddress ia = InetAddress.getByName("ganja.saili.com");
           System.out.println("ia:" + ia.getHostAddress() +":"+ia.getHostName());
           UDPClient uu = new UDPClient(ia,30000);
           uu.send(args[0]);
           }
           catch(Exception e)
           {
           }
        }}
      

  2.   

    UDPClient端:
    import java.net.*;
    import java.io.*;/**
     * This class allows you to send and receive Strings and byte arrays via UDP
     * without concerning yourself with DatagramPackets and DatagramSockets.
     * @version 1.0 of June 1, 1996
     * @author Elliotte Rusty Harold
     */
    public class UDPClient {  InetAddress ia;
      int port;
      DatagramSocket ds;  /**
       * Creates a new UDPClient.
       * @param ia The InetAddress of the remote host to which data will be sent
       * @param port The port on the remote host to which data will be sent
       * @throws SocketException
       */
       public UDPClient(InetAddress ia, int port) throws SocketException {    this.ia = ia;
        this.port = port;
        ds = new DatagramSocket();  }  public UDPClient(String hostname, int port) throws UnknownHostException, SocketException {    this(InetAddress.getByName(hostname), port);  }  /**
       * This method sends data to the remote host via UDP. If the byte is longer than
       * the maximum reliable length of a UDP Datagram (64900) bytes then an
       * IOException is thrown
       * @param buffer A byte array containing the data to be sent
       * @throws IOException
       */
      public void send(byte[] buffer) throws IOException {    if (buffer.length > 64900) throw new IOException();
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, port);
        ds.send(dp);
        System.out.println("send(byte[] buffer) ");  }  /**
       *
       * This method sends an ISO-Latin-1 string to the remote host via UDP.
       * The string will be truncated to ISO-Latin-1 even if it's Unicode.
       * @param s The string to be sent
       * @throws IOException
       */
      public void send(String s) throws IOException
      {
        System.out.println("send(String s) throws IOException ");
        byte[] data = new byte[s.length()];
        s.getBytes(0, s.length(), data, 0);
        send(data);  }  /**
       * This method sends an empty datagram to the remote host via UDP.
       * @throws IOException
       */
      public void send() throws IOException {    byte[] b = new byte[1];
        send(b);  }  /**
       * This method blocks until a UDP Datagram is received from the host with which
       * this UDPClient communicates. This can be an indefinite amount of time if
       * the host is unreachable so calls to this method should be placed in a separate
       * thread from the main program.
       * @return the data received as a byte array
       * @throws IOException
       */
      public synchronized byte[] receive() throws IOException {    byte[] buffer = new byte[65507];
        DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
        ds.receive(incoming);
        // a client should only receive data from the host to
        while ( !incoming.getAddress().equals(ia)) {
          ds.receive(incoming);
        }
        return incoming.getData();  }  /**
       * This method blocks until a UDP Datagram is received from the host with which
       * this UDPClient communicates. This can be an indefinite amount of time if
       * the host is unreachable so calls to this method should be placed in a separate
       * thread from the main program. When data is received it is
       * converted into an ISO-latin-1 String and returned.
       * @return the data received as a byte array
       * @throws IOException
       */
      public synchronized String receiveString() throws IOException {    byte[] data = receive();
        return new String(data, 0, 0, data.length);  }  /**
       * @return the port which this object sends data to
       */
      public int getPort() {    return port;  }  /**
       * @return the port which this client is bound to
       */
      public int getLocalPort() {    return ds.getLocalPort();  }  /**
       * @return the InetAddress which this client sends data to
       */
      public InetAddress getAddress() {    return ia;  }  /**
       * @return a String showning the remote host and port which this client sends data to
       */
      public String toString() {    return ia + ":" + port;  }   public static void main(String args[])
        {
        try
        {
           InetAddress ia = InetAddress.getByName("ganja.saili.com");
           System.out.println("ia:" + ia.getHostAddress() +":"+ia.getHostName());
           UDPClient uu = new UDPClient(ia,30000);
           uu.send(args[0]);
           }
           catch(Exception e)
           {
           }
        }}