黑马程序员技术交流社区

标题: Udp多线程自娱自乐 [打印本页]

作者: vermouth    时间: 2015-1-19 11:26
标题: Udp多线程自娱自乐
也是好久没写过网络编程,好累,写了一个小时。。。
  1. /**
  2.        
  3. */
  4. import java.net.*;
  5. import java.io.*;
  6. class UdpChat{
  7.         public static void main(String [] args)throws Exception{
  8.                 DatagramSocket send = new DatagramSocket();
  9.                 DatagramSocket rec = new DatagramSocket(10007);
  10.                
  11.                 UdpSend us = new UdpSend(send);
  12.                 UdpRec ur = new UdpRec(rec);
  13.                
  14.                 new Thread(us).start();
  15.                 new Thread(ur).start();
  16.         }
  17. }

  18. class UdpSend implements Runnable{

  19.         private DatagramSocket ds = null;
  20.         public UdpSend(DatagramSocket ds){
  21.                 this.ds = ds;
  22.         }
  23.        
  24.         public  void run(){
  25.                 try{                       
  26.                         DatagramPacket dp = null;
  27.                        
  28.                         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  29.                        
  30.                         String line = null;
  31.                        
  32.                         byte [] buf = new byte[1024];
  33.                        
  34.                         while ((line = br.readLine())!=null){
  35.                                 buf = line.getBytes();
  36.                                 dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("172.16.8.255"),10007);
  37.                                 ds.send(dp);
  38.                                 if(line.equals("over"))
  39.                                         break;
  40.                         }
  41.                         ds.close();
  42.                 }
  43.                 catch(Exception e){
  44.                         throw new RuntimeException("rec ex");
  45.                 }       
  46.         }
  47. }

  48. class UdpRec implements Runnable{

  49.         private DatagramSocket ds = null;
  50.         public UdpRec(DatagramSocket ds){
  51.                 this.ds = ds;
  52.         }
  53.        
  54.         public void run(){
  55.        
  56.                 DatagramPacket dp = null;
  57.                
  58.                 byte [] buf = new byte[1024];
  59.                
  60.                 try{
  61.                         while(true){
  62.                                 dp = new DatagramPacket(buf,buf.length);
  63.                                 ds.receive(dp);
  64.                                
  65.                                 String ip = dp.getAddress().getHostAddress();
  66.                                 int port = dp.getPort();
  67.                                 String data = new String(dp.getData(),0,dp.getLength()).trim();
  68.                                 if (data.equals("over")){
  69.                                         System.out.println("88");
  70.                                 }
  71.                                 else {
  72.                                         System.out.println("receive from:"+ip+".."+"port"+" : "+data);
  73.                                 }
  74.                         }
  75.                 }
  76.                 catch(Exception e){
  77.                         throw new RuntimeException("rec ex");
  78.                 }
  79.         }
  80. }
复制代码


我把close放在finally里抛出SocketException,可是提示我不能抛出,为甚么呢?
作者: 兮兮之c    时间: 2015-1-25 00:34
(1)你知道你为什么能抛出 RuntimeException然后没报错吗??(2)你把close放到finally中,会报SocketException,这是JDK的设计,然后这个异常是受检查异常,必须对其进行处理(要么就进行catch,要么声明throws),然后我觉得你肯定是使用了throws,这样就会出现问题,因为Runnable接口中的run方法没有抛出异常,这样就和重写的准则违背了,所以报错了
作者: vermouth    时间: 2015-1-25 12:35
兮兮之c 发表于 2015-1-25 00:34
(1)你知道你为什么能抛出 RuntimeException然后没报错吗??(2)你把close放到finally中,会报SocketExc ...

我的意思是 开始把close写在finally 去catch了SocketException,但是编译不过 所以改成这样了

关于重写的问题 有点基础的都知道不可以那么写




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2