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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孙国军 中级黑马   /  2012-4-26 10:24  /  1530 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. /*
  2. 需求:
  3. 通过udp建立简单的通讯软件;
  4. */

  5. import java.io.*;
  6. import java.net.*;
  7. class UdpSend implements Runnable
  8. {
  9. private DatagramSocket ds;
  10. UdpSend(DatagramSocket ds)
  11. {
  12. this.ds=ds;
  13. }
  14. public void run()
  15. {
  16. BufferedReader bufr=null;
  17. try
  18. {
  19. bufr=new BufferedReader(new InputStreamReader(System.in));
  20. String line=null;
  21. while ((line=bufr.readLine())!=null)
  22. {
  23. byte[] buf=line.getBytes();
  24. try
  25. {
  26. DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.0.4"),10000);
  27. ds.send(dp);
  28. }
  29. catch (UnknownHostException ue)
  30. {
  31. throw new RuntimeException("您指定的主机地址未知");
  32. }

  33. }
  34. }
  35. catch (IOException io)
  36. {
  37. throw new RuntimeException("读取键盘数据失败");
  38. }
  39. finally
  40. {
  41. try
  42. {
  43. if (bufr!=null)
  44. {
  45. bufr.close();
  46. }
  47. }
  48. catch (IOException io)
  49. {
  50. throw new RuntimeException("读取流关闭失败");
  51. }
  52. ds.close();
  53. }
  54. }
  55. }


  56. class UdpReceive implements Runnable
  57. {
  58. private DatagramSocket ds;
  59. UdpReceive(DatagramSocket ds)
  60. {
  61. this.ds=ds;
  62. }
  63. public void run()
  64. {
  65. while (true)
  66. {
  67. byte[] buf=new byte[1024];
  68. DatagramPacket dp=new DatagramPacket(buf,buf.length);
  69. try
  70. {
  71. ds.receive(dp);
  72. }
  73. catch (IOException i)
  74. {
  75. throw new RuntimeException("接受数据包失败");
  76. }
  77. String ip=dp.getAddress().getHostAddress();

  78. System.out.println(ip);

  79. String line=new String(dp.getData(),0,dp.getLength());

  80. System.out.println(line);

  81. if (line.equals("over"))
  82. {
  83. break;
  84. }

  85. ds.close();
  86. }

  87. }
  88. }


  89. class UdpSocket
  90. {
  91. public static void main(String[] args)
  92. {
  93. DatagramSocket dsSend=null;

  94. DatagramSocket dsReceive=null;
  95. try
  96. {
  97. dsSend=new DatagramSocket(8888);

  98. dsReceive=new DatagramSocket(10000);
  99. }
  100. catch (SocketException se)
  101. {
  102. throw new RuntimeException("Socket服务创建失败");
  103. }
  104. UdpSend us=new UdpSend(dsSend);

  105. UdpReceive ur=new UdpReceive(dsReceive);

  106. new Thread(us).start();

  107. new Thread(ur).start();

  108. }
  109. }
复制代码



具体异常的状况在DOS命令行的截图处

2 个回复

倒序浏览
本帖最后由 林德燚 于 2012-4-26 11:33 编辑
  1. public void run()

  2. {

  3.         
  4.                 byte[] buf=new byte[1024];
  5.                 DatagramPacket dp=new DatagramPacket(buf,buf.length);
  6.                 try
  7.                 {
  8.                 while (true){
  9.                 ds.receive(dp);
  10.                 String ip=dp.getAddress().getHostAddress();
  11.                 System.out.println(ip);
  12.                 String line=new String(dp.getData(),0,dp.getLength());
  13.                 System.out.println(line);
  14.                 if (line.equals("over")){break;}
  15.                 }}
  16.                 catch (IOException i){
  17.                         throw new RuntimeException("接受数据包失败");
  18.                 }
  19.                 ds.close();
  20.         
  21. }
复制代码
你接收语句有错,应该把while循环放在try语句里,尽量让死循环语句范围小点,只有在调用receive方法是才会阻塞;
还有你发送类中循环语句也不正确,你这是在键盘输入,直接读取一行就好,不要判断读取是否为null;

评分

参与人数 2技术分 +1 黑马币 +9 收起 理由
职业规划-刘倩老师 + 1
孙国军 + 9 很给力!

查看全部评分

回复 使用道具 举报
林德燚 发表于 2012-4-26 11:31
你接收语句有错,应该把while循环放在try语句里,尽量让死循环语句范围小点,只有在调用receive方法是才会 ...

我找你说的,修改了一下,运行成功

但是具体为什么循环放在try外面就不可以啊???

能不能说详细点啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马