A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 奋发吧小白 高级黑马   /  2014-8-31 09:00  /  647 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 奋发吧小白 于 2014-8-31 09:03 编辑

UDP接收端:
  1. import java.net.*;
  2. class Rec
  3. {
  4.         public static void main(String[] args)  throws Exception
  5.         {
  6.                 DatagramSocket ds = new DatagramSocket(10000);
  7.                 byte buf[] = new byte[1024];
  8.                 DatagramPacket dp = new DatagramPacket(buf,buf.length);
  9.                 ds.receive(dp);
  10.                 String ip = dp.getAddress().getHostAddress();
  11.                 String data = new String(dp.getData(),0,dp.getLength());
  12.                 int port = dp.getPort();
  13.                 System.out.println(ip+"::"+data+"::"+port);
  14.                 ds.close();
  15.         }
  16. }
复制代码
UDP发送端:
  1. import java.net.*;
  2. class Send
  3. {
  4.         public static void main(String [] args) throws Exception
  5.         {
  6.                 DatagramSocket ds = new DatagramSocket();
  7.                 byte [] buf = "nihaowolaole".getBytes();
  8.                 DatagramPacket dp =
  9.                         new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.104"),10000);
  10.                 ds.close();
  11.         }
  12. }
复制代码







2 个回复

正序浏览
ds.send (dp);
然后再关闭资源
回复 使用道具 举报
发送端没有发送吧,大哥?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马