1 7 8 package java.net; 9 10 import java.io.IOException ; 11 import java.io.InterruptedIOException ; 12 import java.util.Enumeration ; 13 14 65 public 66 class MulticastSocket extends DatagramSocket { 67 86 public MulticastSocket() throws IOException { 87 this(new InetSocketAddress (0)); 88 } 89 90 111 public MulticastSocket(int port) throws IOException { 112 this(new InetSocketAddress (port)); 113 } 114 115 140 public MulticastSocket(SocketAddress bindaddr) throws IOException { 141 super((SocketAddress ) null); 142 143 setReuseAddress(true); 145 146 if (bindaddr != null) { 147 bind(bindaddr); 148 } 149 } 150 151 155 private Object ttlLock = new Object (); 156 157 161 private Object infLock = new Object (); 162 163 166 private InetAddress infAddress = null; 167 168 169 184 @Deprecated 185 public void setTTL(byte ttl) throws IOException { 186 if (isClosed()) 187 throw new SocketException ("Socket is closed"); 188 getImpl().setTTL(ttl); 189 } 190 191 203 public void setTimeToLive(int ttl) throws IOException { 204 if (ttl < 0 || ttl > 255) { 205 throw new IllegalArgumentException ("ttl out of range"); 206 } 207 if (isClosed()) 208 throw new SocketException ("Socket is closed"); 209 getImpl().setTimeToLive(ttl); 210 } 211 212 223 @Deprecated 224 public byte getTTL() throws IOException { 225 if (isClosed()) 226 throw new SocketException ("Socket is closed"); 227 return getImpl().getTTL(); 228 } 229 230 238 public int getTimeToLive() throws IOException { 239 if (isClosed()) 240 throw new SocketException ("Socket is closed"); 241 return getImpl().getTimeToLive(); 242 } 243 244 262 public void joinGroup(InetAddress mcastaddr) throws IOException { 263 if (isClosed()) { 264 throw new SocketException ("Socket is closed"); 265 } 266 267 SecurityManager security = System.getSecurityManager(); 268 if (security != null) { 269 security.checkMulticast(mcastaddr); 270 } 271 272 if (!mcastaddr.isMulticastAddress()) { 273 throw new SocketException ("Not a multicast address"); 274 } 275 276 getImpl().join(mcastaddr); 277 } 278 279 296 public void leaveGroup(InetAddress mcastaddr) throws IOException { 297 if (isClosed()) { 298 throw new SocketException ("Socket is closed"); 299 } 300 301 SecurityManager security = System.getSecurityManager(); 302 if (security != null) { 303 security.checkMulticast(mcastaddr); 304 } 305 306 if (!mcastaddr.isMulticastAddress()) { 307 throw new SocketException ("Not a multicast address"); 308 } 309 310 getImpl().leave(mcastaddr); 311 } 312 313 337 public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) 338 throws IOException { 339 if (isClosed()) 340 throw new SocketException ("Socket is closed"); 341 342 if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress )) 343 throw new IllegalArgumentException ("Unsupported address type"); 344 345 if (oldImpl) 346 throw new UnsupportedOperationException (); 347 348 SecurityManager security = System.getSecurityManager(); 349 if (security != null) { 350 security.checkMulticast(((InetSocketAddress )mcastaddr).getAddress()); 351 } 352 353 if (!((InetSocketAddress )mcastaddr).getAddress().isMulticastAddress()) { 354 throw new SocketException ("Not a multicast address"); 355 } 356 357 getImpl().joinGroup(mcastaddr, netIf); 358 } 359 360 383 public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) 384 throws IOException { 385 if (isClosed()) 386 throw new SocketException ("Socket is closed"); 387 388 if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress )) 389 throw new IllegalArgumentException ("Unsupported address type"); 390 391 if (oldImpl) 392 throw new UnsupportedOperationException (); 393 394 SecurityManager security = System.getSecurityManager(); 395 if (security != null) { 396 security.checkMulticast(((InetSocketAddress )mcastaddr).getAddress()); 397 } 398 399 if (!((InetSocketAddress )mcastaddr).getAddress().isMulticastAddress()) { 400 throw new SocketException ("Not a multicast address"); 401 } 402 403 getImpl().leaveGroup(mcastaddr, netIf); 404 } 405 406 415 public void setInterface(InetAddress inf) throws SocketException { 416 if (isClosed()) { 417 throw new SocketException ("Socket is closed"); 418 } 419 synchronized (infLock) { 420 getImpl().setOption(SocketOptions.IP_MULTICAST_IF, inf); 421 infAddress = inf; 422 } 423 } 424 425 438 public InetAddress getInterface() throws SocketException { 439 if (isClosed()) { 440 throw new SocketException ("Socket is closed"); 441 } 442 synchronized (infLock) { 443 InetAddress ia = 444 (InetAddress )getImpl().getOption(SocketOptions.IP_MULTICAST_IF); 445 446 450 if (infAddress == null) { 451 return ia; 452 } 453 454 457 if (ia.equals(infAddress)) { 458 return ia; 459 } 460 461 466 try { 467 NetworkInterface ni = NetworkInterface.getByInetAddress(ia); 468 Enumeration addrs = ni.getInetAddresses(); 469 while (addrs.hasMoreElements()) { 470 InetAddress addr = (InetAddress )(addrs.nextElement()); 471 if (addr.equals(infAddress)) { 472 return infAddress; 473 } 474 } 475 476 480 infAddress = null; 481 return ia; 482 } catch (Exception e) { 483 return ia; 484 } 485 } 486 } 487 488 498 public void setNetworkInterface(NetworkInterface netIf) 499 throws SocketException { 500 501 synchronized (infLock) { 502 getImpl().setOption(SocketOptions.IP_MULTICAST_IF2, netIf); 503 infAddress = null; 504 } 505 } 506 507 516 public NetworkInterface getNetworkInterface() throws SocketException { 517 NetworkInterface ni 518 = (NetworkInterface )getImpl().getOption(SocketOptions.IP_MULTICAST_IF2); 519 if (ni.getIndex() == 0) { 520 InetAddress [] addrs = new InetAddress [1]; 521 addrs[0] = InetAddress.anyLocalAddress(); 522 return new NetworkInterface (addrs[0].getHostName(), 0, addrs); 523 } else { 524 return ni; 525 } 526 } 527 528 542 public void setLoopbackMode(boolean disable) throws SocketException { 543 getImpl().setOption(SocketOptions.IP_MULTICAST_LOOP, Boolean.valueOf(disable)); 544 } 545 546 554 public boolean getLoopbackMode() throws SocketException { 555 return ((Boolean )getImpl().getOption(SocketOptions.IP_MULTICAST_LOOP)).booleanValue(); 556 } 557 558 605 @Deprecated 606 public void send(DatagramPacket p, byte ttl) 607 throws IOException { 608 if (isClosed()) 609 throw new SocketException ("Socket is closed"); 610 synchronized(ttlLock) { 611 synchronized(p) { 612 if (connectState == ST_NOT_CONNECTED) { 613 SecurityManager security = System.getSecurityManager(); 617 if (security != null) { 618 if (p.getAddress().isMulticastAddress()) { 619 security.checkMulticast(p.getAddress(), ttl); 620 } else { 621 security.checkConnect(p.getAddress().getHostAddress(), 622 p.getPort()); 623 } 624 } 625 } else { 626 InetAddress packetAddress = null; 628 packetAddress = p.getAddress(); 629 if (packetAddress == null) { 630 p.setAddress(connectedAddress); 631 p.setPort(connectedPort); 632 } else if ((!packetAddress.equals(connectedAddress)) || 633 p.getPort() != connectedPort) { 634 throw new SecurityException ("connected address and packet address" + 635 " differ"); 636 } 637 } 638 byte dttl = getTTL(); 639 try { 640 if (ttl != dttl) { 641 getImpl().setTTL(ttl); 643 } 644 getImpl().send(p); 646 } finally { 647 if (ttl != dttl) { 649 getImpl().setTTL(dttl); 650 } 651 } 652 } } } } 656 | Popular Tags |