94 |
private int remotePort = -1; |
private int remotePort = -1; |
95 |
|
|
96 |
/** |
/** |
97 |
* Is this a "connected" datagram socket? |
* Indicates when the socket is closed. |
98 |
*/ |
*/ |
99 |
private boolean connected = false; |
private boolean closed = false; |
100 |
|
|
101 |
|
/** |
102 |
|
* Creates a DatagramSocket from a specified DatagramSocketImpl instance |
103 |
|
* |
104 |
|
* @param impl The DatagramSocketImpl the socket will be created from |
105 |
|
* |
106 |
|
* @since 1.4 |
107 |
|
*/ |
108 |
|
protected DatagramSocket (DatagramSocketImpl impl) |
109 |
|
{ |
110 |
|
this.impl = impl; |
111 |
|
this.remoteAddress = null; |
112 |
|
this.remotePort = -1; |
113 |
|
} |
114 |
|
|
115 |
/** |
/** |
116 |
* Initializes a new instance of <code>DatagramSocket</code> that binds to |
* Initializes a new instance of <code>DatagramSocket</code> that binds to |
193 |
*/ |
*/ |
194 |
public void close() |
public void close() |
195 |
{ |
{ |
196 |
impl.close(); |
if (!closed) |
197 |
|
{ |
198 |
|
impl.close(); |
199 |
|
remoteAddress = null; |
200 |
|
remotePort = -1; |
201 |
|
closed = true; |
202 |
|
} |
203 |
} |
} |
204 |
|
|
205 |
/** |
/** |
408 |
* |
* |
409 |
* @since 1.2 |
* @since 1.2 |
410 |
*/ |
*/ |
411 |
public void connect (InetAddress address, int port) |
public void connect(InetAddress address, int port) |
|
throws SecurityException, IllegalArgumentException |
|
412 |
{ |
{ |
413 |
if (address == null) |
if (address == null) |
414 |
throw new IllegalArgumentException ("Connect address may not be null"); |
throw new IllegalArgumentException ("Connect address may not be null"); |
431 |
} |
} |
432 |
|
|
433 |
/** |
/** |
434 |
* This method disconnects this socket from the address/port it was |
* This method disconnects this socket from the address/port it was |
435 |
* connected to. If the socket was not connected in the first place, |
* connected to. If the socket was not connected in the first place, |
436 |
* this method does nothing. |
* this method does nothing. |
437 |
* |
* |
454 |
* @param p A <code>DatagramPacket</code> for storing the data |
* @param p A <code>DatagramPacket</code> for storing the data |
455 |
* |
* |
456 |
* @exception IOException If an error occurs. |
* @exception IOException If an error occurs. |
457 |
|
* @exception SocketTimeoutException If setSoTimeout was previously called |
458 |
|
* and the timeout has expired. |
459 |
|
* @exception PortUnreachableException If the socket is connected to a |
460 |
|
* currently unreachable destination. Note, there is no guarantee that the |
461 |
|
* exception will be thrown. |
462 |
|
* @exception IllegalBlockingModeException If this socket has an associated |
463 |
|
* channel, and the channel is in non-blocking mode. |
464 |
|
* @exception SecurityException If a security manager exists and its |
465 |
|
* checkAccept ethod doesn't allow the receive. |
466 |
*/ |
*/ |
467 |
public synchronized void receive(DatagramPacket p) throws IOException |
public synchronized void receive(DatagramPacket p) throws IOException |
468 |
{ |
{ |
469 |
|
if (impl == null) |
470 |
|
throw new IOException ("Cannot initialize Socket implementation"); |
471 |
|
|
472 |
|
if (remoteAddress != null && remoteAddress.isMulticastAddress ()) |
473 |
|
throw new IOException ( |
474 |
|
"Socket connected to a multicast address my not receive"); |
475 |
|
|
476 |
|
if (ch != null && !ch.isBlocking ()) |
477 |
|
throw new IllegalBlockingModeException (); |
478 |
|
|
479 |
impl.receive(p); |
impl.receive(p); |
480 |
|
|
481 |
SecurityManager s = System.getSecurityManager(); |
SecurityManager s = System.getSecurityManager(); |
500 |
*/ |
*/ |
501 |
public void send(DatagramPacket p) throws IOException |
public void send(DatagramPacket p) throws IOException |
502 |
{ |
{ |
503 |
if (!connected) |
// JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api. |
504 |
|
SecurityManager s = System.getSecurityManager(); |
505 |
|
if (s != null && !isConnected ()) |
506 |
{ |
{ |
507 |
SecurityManager s = System.getSecurityManager(); |
InetAddress addr = p.getAddress(); |
508 |
if (s != null) |
if (addr.isMulticastAddress()) |
509 |
{ |
s.checkMulticast(addr); |
510 |
InetAddress addr = p.getAddress(); |
else |
511 |
if (addr.isMulticastAddress()) |
s.checkConnect(addr.getHostAddress(), p.getPort()); |
|
s.checkMulticast(addr); |
|
|
else |
|
|
s.checkConnect(addr.getHostAddress(), p.getPort()); |
|
|
} |
|
512 |
} |
} |
513 |
|
|
514 |
|
if (isConnected ()) |
515 |
|
{ |
516 |
|
if (p.getAddress () != null && (remoteAddress != p.getAddress () || |
517 |
|
remotePort != p.getPort ())) |
518 |
|
throw new IllegalArgumentException ( |
519 |
|
"DatagramPacket address does not match remote address" ); |
520 |
|
} |
521 |
|
|
522 |
// FIXME: if this is a subclass of MulticastSocket, |
// FIXME: if this is a subclass of MulticastSocket, |
523 |
// use getTimeToLive for TTL val. |
// use getTimeToLive for TTL val. |
524 |
|
|
525 |
|
if (ch != null && !ch.isBlocking ()) |
526 |
|
throw new IllegalBlockingModeException (); |
527 |
|
|
528 |
impl.send(p); |
impl.send(p); |
529 |
} |
} |
530 |
|
|
556 |
} |
} |
557 |
|
|
558 |
/** |
/** |
559 |
|
* Checks if the datagram socket is closed. |
560 |
|
* |
561 |
|
* @since 1.4 |
562 |
|
*/ |
563 |
|
public boolean isClosed() |
564 |
|
{ |
565 |
|
return closed; |
566 |
|
} |
567 |
|
|
568 |
|
/** |
569 |
* Returns the datagram channel assoziated with this datagram socket. |
* Returns the datagram channel assoziated with this datagram socket. |
570 |
* |
* |
571 |
* @since 1.4 |
* @since 1.4 |