本帖最后由 ☆星痕泪 于 2013-11-6 19:11 编辑
我Send继承复写run方法里,当socket 的对象 调用send方法把Packet数据包发送出去,毕老师是在while循环外关闭流。我先没想这个 ,就在catch后加了finally,在这finally里面关闭流,最后导致我在doc里写“886”等结束时没法预料那样关闭,想请高手详细解释一下,灰常感谢!
- <P>public void run()
- {
- try
- {
- BufferedReader br =
- new BufferedReader(new InputStreamReader(System.in));
-
- String line = null;
- while ((line = br.readLine())!=null)
- {
- if("baibai".equals(line))
- break;
- byte[] buy = line.getBytes();
- DatagramPacket dp =
- new DatagramPacket(buy,buy.length,InetAddress.getByName("192.168.0.255"),10001);
- ds.send(dp);
- }
-
- }
- catch (Exception e)
- {
- throw new RuntimeException("数据发送异常");
- }
- finally
- {
- ds.close();
- }</P>
- <P>//补充:程序本身能够运行,我就是像知道有finally和没你finally的区别,在这里。</P>
复制代码 |