我是刚刚学到UDP的。刚照着写了个程序能编译但是运行的时候TestUdp这个发送程序出现找不到地址的异常,接受程序UdpRece很正常。我机子没有上网但是我自己设了IP为192.168.0.2 难道单机不能编写着UDP程序吗?请那位大虾指点一下 感激!
--------------------------------------------------------
import java.net.*;
public class TestUdp {
  public static void main(String[] args) throws Exception {
    DatagramSocket ds = new DatagramSocket();
    String strInfo ="www.it315.org";    DatagramPacket dp = new DatagramPacket(strInfo.getBytes(),
                 strInfo.getBytes().length,InetAddress.getByName("192.168.0.2"),3000);
    ds.send(dp);
    ds.close();
  }
}
--------------------------------------------------------------
import java.net.*;
public class UdpRece  {
  public static void main(String[] args) throws Exception {
  DatagramSocket ds = new DatagramSocket(3000);
  byte[] buf = new byte[1024];
  DatagramPacket dp = new DatagramPacket(buf,1024);
  ds.receive(dp);
     System.out.println( new String(dp.getData(),0,dp.getLength()));
  }
}