本帖最后由 李计伟 于 2012-11-15 20:55 编辑
- public class SendTest {
- public static void main(String[] args) throws IOException {
- DatagramSocket ds = new DatagramSocket();
- byte[] t = "你好".getBytes();
- DatagramPacket dp = new DatagramPacket(t,t.length, InetAddress.getByName("192.168.1.168"), 9950);
- ds.send(dp);
- ds.close();
- }
- }
复制代码- public class ReciveTest {
- public static void main(String[] args) throws IOException {
- DatagramSocket ds = new DatagramSocket(9950);
- byte[] bt = new byte[1024];
- DatagramPacket dp = new DatagramPacket(bt, bt.length);
- ds.receive(dp);
- String ip = dp.getAddress().getHostAddress();
- String str = new String(dp.getData(), 0,dp.getLength());
- System.out.println(str+"from"+ip);
- ds.close();
- }
- }
复制代码 换宽带连接的ip控制台也没结果
|
|