171 connectState = ST_CONNECTED_NO_IMPL;
172 }
173 }
174
175 connectedAddress = address;
176 connectedPort = port;
177 }
178
179
180 /**
181 * Constructs a datagram socket and binds it to any available port
182 * on the local host machine. The socket will be bound to the
183 * {@link InetAddress#isAnyLocalAddress wildcard} address,
184 * an IP address chosen by the kernel.
185 *
186 * <p>If there is a security manager,
187 * its {@code checkListen} method is first called
188 * with 0 as its argument to ensure the operation is allowed.
189 * This could result in a SecurityException.
190 *
191 * @exception SocketException if the socket could not be opened,
192 * or the socket could not bind to the specified local port.
193 * @exception SecurityException if a security manager exists and its
194 * {@code checkListen} method doesn't allow the operation.
195 *
196 * @see SecurityManager#checkListen
197 */
198 public DatagramSocket() throws SocketException {
199 this(new InetSocketAddress(0));
200 }
201
202 /**
203 * Creates an unbound datagram socket with the specified
204 * DatagramSocketImpl.
205 *
206 * @param impl an instance of a <B>DatagramSocketImpl</B>
207 * the subclass wishes to use on the DatagramSocket.
208 * @since 1.4
209 */
210 protected DatagramSocket(DatagramSocketImpl impl) {
211 if (impl == null)
212 throw new NullPointerException();
213 this.impl = impl;
214 checkOldImpl();
215 }
216
217 /**
218 * Creates a datagram socket, bound to the specified local
219 * socket address.
220 * <p>
221 * If, if the address is {@code null}, creates an unbound socket.
222 *
223 * <p>If there is a security manager,
224 * its {@code checkListen} method is first called
225 * with the port from the socket address
226 * as its argument to ensure the operation is allowed.
227 * This could result in a SecurityException.
228 *
229 * @param bindaddr local socket address to bind, or {@code null}
230 * for an unbound socket.
231 *
232 * @exception SocketException if the socket could not be opened,
233 * or the socket could not bind to the specified local port.
234 * @exception SecurityException if a security manager exists and its
235 * {@code checkListen} method doesn't allow the operation.
236 *
237 * @see SecurityManager#checkListen
238 * @since 1.4
239 */
240 public DatagramSocket(SocketAddress bindaddr) throws SocketException {
241 // create a datagram socket.
242 createImpl();
243 if (bindaddr != null) {
244 try {
245 bind(bindaddr);
246 } finally {
247 if (!isBound())
248 close();
249 }
250 }
251 }
252
253 /**
254 * Constructs a datagram socket and binds it to the specified port
255 * on the local host machine. The socket will be bound to the
256 * {@link InetAddress#isAnyLocalAddress wildcard} address,
257 * an IP address chosen by the kernel.
258 *
259 * <p>If there is a security manager,
260 * its {@code checkListen} method is first called
261 * with the {@code port} argument
262 * as its argument to ensure the operation is allowed.
263 * This could result in a SecurityException.
264 *
265 * @param port port to use.
266 * @exception SocketException if the socket could not be opened,
267 * or the socket could not bind to the specified local port.
268 * @exception SecurityException if a security manager exists and its
269 * {@code checkListen} method doesn't allow the operation.
270 *
271 * @see SecurityManager#checkListen
272 */
273 public DatagramSocket(int port) throws SocketException {
274 this(port, null);
275 }
276
277 /**
278 * Creates a datagram socket, bound to the specified local
279 * address. The local port must be between 0 and 65535 inclusive.
280 * If the IP address is 0.0.0.0, the socket will be bound to the
281 * {@link InetAddress#isAnyLocalAddress wildcard} address,
282 * an IP address chosen by the kernel.
283 *
284 * <p>If there is a security manager,
285 * its {@code checkListen} method is first called
286 * with the {@code port} argument
287 * as its argument to ensure the operation is allowed.
288 * This could result in a SecurityException.
289 *
290 * @param port local port to use
291 * @param laddr local address to bind
292 *
293 * @exception SocketException if the socket could not be opened,
294 * or the socket could not bind to the specified local port.
295 * @exception SecurityException if a security manager exists and its
296 * {@code checkListen} method doesn't allow the operation.
297 *
298 * @see SecurityManager#checkListen
299 * @since 1.1
300 */
301 public DatagramSocket(int port, InetAddress laddr) throws SocketException {
302 this(new InetSocketAddress(laddr, port));
303 }
304
305 private void checkOldImpl() {
306 if (impl == null)
307 return;
308 // DatagramSocketImpl.peekData() is a protected method, therefore we need to use
309 // getDeclaredMethod, therefore we need permission to access the member
310 try {
311 AccessController.doPrivileged(
312 new PrivilegedExceptionAction<>() {
313 public Void run() throws NoSuchMethodException {
314 Class<?>[] cl = new Class<?>[1];
315 cl[0] = DatagramPacket.class;
618 * Sends a datagram packet from this socket. The
619 * {@code DatagramPacket} includes information indicating the
620 * data to be sent, its length, the IP address of the remote host,
621 * and the port number on the remote host.
622 *
623 * <p>If there is a security manager, and the socket is not currently
624 * connected to a remote address, this method first performs some
625 * security checks. First, if {@code p.getAddress().isMulticastAddress()}
626 * is true, this method calls the
627 * security manager's {@code checkMulticast} method
628 * with {@code p.getAddress()} as its argument.
629 * If the evaluation of that expression is false,
630 * this method instead calls the security manager's
631 * {@code checkConnect} method with arguments
632 * {@code p.getAddress().getHostAddress()} and
633 * {@code p.getPort()}. Each call to a security manager method
634 * could result in a SecurityException if the operation is not allowed.
635 *
636 * @param p the {@code DatagramPacket} to be sent.
637 *
638 * @exception IOException if an I/O error occurs.
639 * @exception SecurityException if a security manager exists and its
640 * {@code checkMulticast} or {@code checkConnect}
641 * method doesn't allow the send.
642 * @exception PortUnreachableException may be thrown if the socket is connected
643 * to a currently unreachable destination. Note, there is no
644 * guarantee that the exception will be thrown.
645 * @exception java.nio.channels.IllegalBlockingModeException
646 * if this socket has an associated channel,
647 * and the channel is in non-blocking mode.
648 * @exception IllegalArgumentException if the socket is connected,
649 * and connected address and packet address differ.
650 *
651 * @see java.net.DatagramPacket
652 * @see SecurityManager#checkMulticast(InetAddress)
653 * @see SecurityManager#checkConnect
654 * @revised 1.4
655 * @spec JSR-51
656 */
657 public void send(DatagramPacket p) throws IOException {
658 InetAddress packetAddress = null;
659 synchronized (p) {
660 if (isClosed())
661 throw new SocketException("Socket is closed");
662 checkAddress (p.getAddress(), "send");
663 if (connectState == ST_NOT_CONNECTED) {
664 // check the address is ok with the security manager on every send.
665 SecurityManager security = System.getSecurityManager();
666
667 // The reason you want to synchronize on datagram packet
668 // is because you don't want an applet to change the address
697 }
698 }
699
700 /**
701 * Receives a datagram packet from this socket. When this method
702 * returns, the {@code DatagramPacket}'s buffer is filled with
703 * the data received. The datagram packet also contains the sender's
704 * IP address, and the port number on the sender's machine.
705 * <p>
706 * This method blocks until a datagram is received. The
707 * {@code length} field of the datagram packet object contains
708 * the length of the received message. If the message is longer than
709 * the packet's length, the message is truncated.
710 * <p>
711 * If there is a security manager, a packet cannot be received if the
712 * security manager's {@code checkAccept} method
713 * does not allow it.
714 *
715 * @param p the {@code DatagramPacket} into which to place
716 * the incoming data.
717 * @exception IOException if an I/O error occurs.
718 * @exception SocketTimeoutException if setSoTimeout was previously called
719 * and the timeout has expired.
720 * @exception PortUnreachableException may be thrown if the socket is connected
721 * to a currently unreachable destination. Note, there is no guarantee that the
722 * exception will be thrown.
723 * @exception java.nio.channels.IllegalBlockingModeException
724 * if this socket has an associated channel,
725 * and the channel is in non-blocking mode.
726 * @see java.net.DatagramPacket
727 * @see java.net.DatagramSocket
728 * @revised 1.4
729 * @spec JSR-51
730 */
731 public synchronized void receive(DatagramPacket p) throws IOException {
732 synchronized (p) {
733 if (!isBound())
734 bind(new InetSocketAddress(0));
735 if (connectState == ST_NOT_CONNECTED) {
736 // check the address is ok with the security manager before every recv.
737 SecurityManager security = System.getSecurityManager();
738 if (security != null) {
739 while(true) {
740 String peekAd = null;
741 int peekPort = 0;
742 // peek at the packet to see who it is from.
743 if (!oldImpl) {
934 * network implementation as a hint to size the underlying
935 * network I/O buffers. The SO_SNDBUF setting may also be used
936 * by the network implementation to determine the maximum size
937 * of the packet that can be sent on this socket.
938 * <p>
939 * As SO_SNDBUF is a hint, applications that want to verify
940 * what size the buffer is should call {@link #getSendBufferSize()}.
941 * <p>
942 * Increasing the buffer size may allow multiple outgoing packets
943 * to be queued by the network implementation when the send rate
944 * is high.
945 * <p>
946 * Note: If {@link #send(DatagramPacket)} is used to send a
947 * {@code DatagramPacket} that is larger than the setting
948 * of SO_SNDBUF then it is implementation specific if the
949 * packet is sent or discarded.
950 *
951 * @param size the size to which to set the send buffer
952 * size. This value must be greater than 0.
953 *
954 * @exception SocketException if there is an error
955 * in the underlying protocol, such as an UDP error.
956 * @exception IllegalArgumentException if the value is 0 or is
957 * negative.
958 * @see #getSendBufferSize()
959 */
960 public synchronized void setSendBufferSize(int size)
961 throws SocketException{
962 if (!(size > 0)) {
963 throw new IllegalArgumentException("negative send size");
964 }
965 if (isClosed())
966 throw new SocketException("Socket is closed");
967 getImpl().setOption(SocketOptions.SO_SNDBUF, size);
968 }
969
970 /**
971 * Get value of the SO_SNDBUF option for this {@code DatagramSocket}, that is the
972 * buffer size used by the platform for output on this {@code DatagramSocket}.
973 *
974 * @return the value of the SO_SNDBUF option for this {@code DatagramSocket}
975 * @exception SocketException if there is an error in
976 * the underlying protocol, such as an UDP error.
977 * @see #setSendBufferSize
978 */
979 public synchronized int getSendBufferSize() throws SocketException {
980 if (isClosed())
981 throw new SocketException("Socket is closed");
982 int result = 0;
983 Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);
984 if (o instanceof Integer) {
985 result = ((Integer)o).intValue();
986 }
987 return result;
988 }
989
990 /**
991 * Sets the SO_RCVBUF option to the specified value for this
992 * {@code DatagramSocket}. The SO_RCVBUF option is used by
993 * the network implementation as a hint to size the underlying
994 * network I/O buffers. The SO_RCVBUF setting may also be used
995 * by the network implementation to determine the maximum size
996 * of the packet that can be received on this socket.
997 * <p>
998 * Because SO_RCVBUF is a hint, applications that want to
999 * verify what size the buffers were set to should call
1000 * {@link #getReceiveBufferSize()}.
1001 * <p>
1002 * Increasing SO_RCVBUF may allow the network implementation
1003 * to buffer multiple packets when packets arrive faster than
1004 * are being received using {@link #receive(DatagramPacket)}.
1005 * <p>
1006 * Note: It is implementation specific if a packet larger
1007 * than SO_RCVBUF can be received.
1008 *
1009 * @param size the size to which to set the receive buffer
1010 * size. This value must be greater than 0.
1011 *
1012 * @exception SocketException if there is an error in
1013 * the underlying protocol, such as an UDP error.
1014 * @exception IllegalArgumentException if the value is 0 or is
1015 * negative.
1016 * @see #getReceiveBufferSize()
1017 */
1018 public synchronized void setReceiveBufferSize(int size)
1019 throws SocketException{
1020 if (size <= 0) {
1021 throw new IllegalArgumentException("invalid receive size");
1022 }
1023 if (isClosed())
1024 throw new SocketException("Socket is closed");
1025 getImpl().setOption(SocketOptions.SO_RCVBUF, size);
1026 }
1027
1028 /**
1029 * Get value of the SO_RCVBUF option for this {@code DatagramSocket}, that is the
1030 * buffer size used by the platform for input on this {@code DatagramSocket}.
1031 *
1032 * @return the value of the SO_RCVBUF option for this {@code DatagramSocket}
1033 * @exception SocketException if there is an error in the underlying protocol, such as an UDP error.
1034 * @see #setReceiveBufferSize(int)
1035 */
1036 public synchronized int getReceiveBufferSize()
1037 throws SocketException{
1038 if (isClosed())
1039 throw new SocketException("Socket is closed");
1040 int result = 0;
1041 Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
1042 if (o instanceof Integer) {
1043 result = ((Integer)o).intValue();
1044 }
1045 return result;
1046 }
1047
1048 /**
1049 * Enable/disable the SO_REUSEADDR socket option.
1050 * <p>
1051 * For UDP sockets it may be necessary to bind more than one
1052 * socket to the same socket address. This is typically for the
1053 * purpose of receiving multicast packets
1054 * (See {@link java.net.MulticastSocket}). The
1055 * {@code SO_REUSEADDR} socket option allows multiple
1056 * sockets to be bound to the same socket address if the
1057 * {@code SO_REUSEADDR} socket option is enabled prior
1058 * to binding the socket using {@link #bind(SocketAddress)}.
1059 * <p>
1060 * Note: This functionality is not supported by all existing platforms,
1061 * so it is implementation specific whether this option will be ignored
1062 * or not. However, if it is not supported then
1063 * {@link #getReuseAddress()} will always return {@code false}.
1064 * <p>
1065 * When a {@code DatagramSocket} is created the initial setting
1066 * of {@code SO_REUSEADDR} is disabled.
1067 * <p>
1068 * The behaviour when {@code SO_REUSEADDR} is enabled or
1069 * disabled after a socket is bound (See {@link #isBound()})
1070 * is not defined.
1071 *
1072 * @param on whether to enable or disable the
1073 * @exception SocketException if an error occurs enabling or
1074 * disabling the {@code SO_REUSEADDR} socket option,
1075 * or the socket is closed.
1076 * @since 1.4
1077 * @see #getReuseAddress()
1078 * @see #bind(SocketAddress)
1079 * @see #isBound()
1080 * @see #isClosed()
1081 */
1082 public synchronized void setReuseAddress(boolean on) throws SocketException {
1083 if (isClosed())
1084 throw new SocketException("Socket is closed");
1085 // Integer instead of Boolean for compatibility with older DatagramSocketImpl
1086 if (oldImpl)
1087 getImpl().setOption(SocketOptions.SO_REUSEADDR, on?-1:0);
1088 else
1089 getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
1090 }
1091
1092 /**
1093 * Tests if SO_REUSEADDR is enabled.
1094 *
1095 * @return a {@code boolean} indicating whether or not SO_REUSEADDR is enabled.
1096 * @exception SocketException if there is an error
1097 * in the underlying protocol, such as an UDP error.
1098 * @since 1.4
1099 * @see #setReuseAddress(boolean)
1100 */
1101 public synchronized boolean getReuseAddress() throws SocketException {
1102 if (isClosed())
1103 throw new SocketException("Socket is closed");
1104 Object o = getImpl().getOption(SocketOptions.SO_REUSEADDR);
1105 return ((Boolean)o).booleanValue();
1106 }
1107
1108 /**
1109 * Enable/disable SO_BROADCAST.
1110 *
1111 * <p> Some operating systems may require that the Java virtual machine be
1112 * started with implementation specific privileges to enable this option or
1113 * send broadcast datagrams.
1114 *
1115 * @param on
1116 * whether or not to have broadcast turned on.
1117 *
1118 * @throws SocketException
1119 * if there is an error in the underlying protocol, such as an UDP
1120 * error.
1121 *
1122 * @since 1.4
1123 * @see #getBroadcast()
1124 */
1125 public synchronized void setBroadcast(boolean on) throws SocketException {
1126 if (isClosed())
1127 throw new SocketException("Socket is closed");
1128 getImpl().setOption(SocketOptions.SO_BROADCAST, Boolean.valueOf(on));
1129 }
1130
1131 /**
1132 * Tests if SO_BROADCAST is enabled.
1133 * @return a {@code boolean} indicating whether or not SO_BROADCAST is enabled.
1134 * @exception SocketException if there is an error
1135 * in the underlying protocol, such as an UDP error.
1136 * @since 1.4
1137 * @see #setBroadcast(boolean)
1138 */
1139 public synchronized boolean getBroadcast() throws SocketException {
1140 if (isClosed())
1141 throw new SocketException("Socket is closed");
1142 return ((Boolean)(getImpl().getOption(SocketOptions.SO_BROADCAST))).booleanValue();
1143 }
1144
1145 /**
1146 * Sets traffic class or type-of-service octet in the IP
1147 * datagram header for datagrams sent from this DatagramSocket.
1148 * As the underlying network implementation may ignore this
1149 * value applications should consider it a hint.
1150 *
1151 * <P> The tc <B>must</B> be in the range {@code 0 <= tc <=
1152 * 255} or an IllegalArgumentException will be thrown.
1153 * <p>Notes:
1154 * <p>For Internet Protocol v4 the value consists of an
1273 */
1274 static DatagramSocketImplFactory factory;
1275
1276 /**
1277 * Sets the datagram socket implementation factory for the
1278 * application. The factory can be specified only once.
1279 * <p>
1280 * When an application creates a new datagram socket, the socket
1281 * implementation factory's {@code createDatagramSocketImpl} method is
1282 * called to create the actual datagram socket implementation.
1283 * <p>
1284 * Passing {@code null} to the method is a no-op unless the factory
1285 * was already set.
1286 *
1287 * <p>If there is a security manager, this method first calls
1288 * the security manager's {@code checkSetFactory} method
1289 * to ensure the operation is allowed.
1290 * This could result in a SecurityException.
1291 *
1292 * @param fac the desired factory.
1293 * @exception IOException if an I/O error occurs when setting the
1294 * datagram socket factory.
1295 * @exception SocketException if the factory is already defined.
1296 * @exception SecurityException if a security manager exists and its
1297 * {@code checkSetFactory} method doesn't allow the operation.
1298 * @see java.net.DatagramSocketImplFactory#createDatagramSocketImpl()
1299 * @see SecurityManager#checkSetFactory
1300 * @since 1.3
1301 */
1302 public static synchronized void
1303 setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
1304 throws IOException
1305 {
1306 if (factory != null) {
1307 throw new SocketException("factory already defined");
1308 }
1309 SecurityManager security = System.getSecurityManager();
1310 if (security != null) {
1311 security.checkSetFactory();
1312 }
1313 factory = fac;
1314 }
1315
1316 /**
|
171 connectState = ST_CONNECTED_NO_IMPL;
172 }
173 }
174
175 connectedAddress = address;
176 connectedPort = port;
177 }
178
179
180 /**
181 * Constructs a datagram socket and binds it to any available port
182 * on the local host machine. The socket will be bound to the
183 * {@link InetAddress#isAnyLocalAddress wildcard} address,
184 * an IP address chosen by the kernel.
185 *
186 * <p>If there is a security manager,
187 * its {@code checkListen} method is first called
188 * with 0 as its argument to ensure the operation is allowed.
189 * This could result in a SecurityException.
190 *
191 * @throws SocketException if the socket could not be opened,
192 * or the socket could not bind to the specified local port.
193 * @throws SecurityException if a security manager exists and its
194 * {@code checkListen} method doesn't allow the operation.
195 *
196 * @see SecurityManager#checkListen
197 */
198 public DatagramSocket() throws SocketException {
199 this(new InetSocketAddress(0));
200 }
201
202 /**
203 * Creates an unbound datagram socket with the specified
204 * DatagramSocketImpl.
205 *
206 * @param impl an instance of a <B>DatagramSocketImpl</B>
207 * the subclass wishes to use on the DatagramSocket.
208 * @since 1.4
209 */
210 protected DatagramSocket(DatagramSocketImpl impl) {
211 if (impl == null)
212 throw new NullPointerException();
213 this.impl = impl;
214 checkOldImpl();
215 }
216
217 /**
218 * Creates a datagram socket, bound to the specified local
219 * socket address.
220 * <p>
221 * If, if the address is {@code null}, creates an unbound socket.
222 *
223 * <p>If there is a security manager,
224 * its {@code checkListen} method is first called
225 * with the port from the socket address
226 * as its argument to ensure the operation is allowed.
227 * This could result in a SecurityException.
228 *
229 * @param bindaddr local socket address to bind, or {@code null}
230 * for an unbound socket.
231 *
232 * @throws SocketException if the socket could not be opened,
233 * or the socket could not bind to the specified local port.
234 * @throws SecurityException if a security manager exists and its
235 * {@code checkListen} method doesn't allow the operation.
236 *
237 * @see SecurityManager#checkListen
238 * @since 1.4
239 */
240 public DatagramSocket(SocketAddress bindaddr) throws SocketException {
241 // create a datagram socket.
242 createImpl();
243 if (bindaddr != null) {
244 try {
245 bind(bindaddr);
246 } finally {
247 if (!isBound())
248 close();
249 }
250 }
251 }
252
253 /**
254 * Constructs a datagram socket and binds it to the specified port
255 * on the local host machine. The socket will be bound to the
256 * {@link InetAddress#isAnyLocalAddress wildcard} address,
257 * an IP address chosen by the kernel.
258 *
259 * <p>If there is a security manager,
260 * its {@code checkListen} method is first called
261 * with the {@code port} argument
262 * as its argument to ensure the operation is allowed.
263 * This could result in a SecurityException.
264 *
265 * @param port port to use.
266 * @throws SocketException if the socket could not be opened,
267 * or the socket could not bind to the specified local port.
268 * @throws SecurityException if a security manager exists and its
269 * {@code checkListen} method doesn't allow the operation.
270 *
271 * @see SecurityManager#checkListen
272 */
273 public DatagramSocket(int port) throws SocketException {
274 this(port, null);
275 }
276
277 /**
278 * Creates a datagram socket, bound to the specified local
279 * address. The local port must be between 0 and 65535 inclusive.
280 * If the IP address is 0.0.0.0, the socket will be bound to the
281 * {@link InetAddress#isAnyLocalAddress wildcard} address,
282 * an IP address chosen by the kernel.
283 *
284 * <p>If there is a security manager,
285 * its {@code checkListen} method is first called
286 * with the {@code port} argument
287 * as its argument to ensure the operation is allowed.
288 * This could result in a SecurityException.
289 *
290 * @param port local port to use
291 * @param laddr local address to bind
292 *
293 * @throws SocketException if the socket could not be opened,
294 * or the socket could not bind to the specified local port.
295 * @throws SecurityException if a security manager exists and its
296 * {@code checkListen} method doesn't allow the operation.
297 *
298 * @see SecurityManager#checkListen
299 * @since 1.1
300 */
301 public DatagramSocket(int port, InetAddress laddr) throws SocketException {
302 this(new InetSocketAddress(laddr, port));
303 }
304
305 private void checkOldImpl() {
306 if (impl == null)
307 return;
308 // DatagramSocketImpl.peekData() is a protected method, therefore we need to use
309 // getDeclaredMethod, therefore we need permission to access the member
310 try {
311 AccessController.doPrivileged(
312 new PrivilegedExceptionAction<>() {
313 public Void run() throws NoSuchMethodException {
314 Class<?>[] cl = new Class<?>[1];
315 cl[0] = DatagramPacket.class;
618 * Sends a datagram packet from this socket. The
619 * {@code DatagramPacket} includes information indicating the
620 * data to be sent, its length, the IP address of the remote host,
621 * and the port number on the remote host.
622 *
623 * <p>If there is a security manager, and the socket is not currently
624 * connected to a remote address, this method first performs some
625 * security checks. First, if {@code p.getAddress().isMulticastAddress()}
626 * is true, this method calls the
627 * security manager's {@code checkMulticast} method
628 * with {@code p.getAddress()} as its argument.
629 * If the evaluation of that expression is false,
630 * this method instead calls the security manager's
631 * {@code checkConnect} method with arguments
632 * {@code p.getAddress().getHostAddress()} and
633 * {@code p.getPort()}. Each call to a security manager method
634 * could result in a SecurityException if the operation is not allowed.
635 *
636 * @param p the {@code DatagramPacket} to be sent.
637 *
638 * @throws IOException if an I/O error occurs.
639 * @throws SecurityException if a security manager exists and its
640 * {@code checkMulticast} or {@code checkConnect}
641 * method doesn't allow the send.
642 * @throws PortUnreachableException may be thrown if the socket is connected
643 * to a currently unreachable destination. Note, there is no
644 * guarantee that the exception will be thrown.
645 * @throws java.nio.channels.IllegalBlockingModeException
646 * if this socket has an associated channel,
647 * and the channel is in non-blocking mode.
648 * @throws IllegalArgumentException if the socket is connected,
649 * and connected address and packet address differ.
650 *
651 * @see java.net.DatagramPacket
652 * @see SecurityManager#checkMulticast(InetAddress)
653 * @see SecurityManager#checkConnect
654 * @revised 1.4
655 * @spec JSR-51
656 */
657 public void send(DatagramPacket p) throws IOException {
658 InetAddress packetAddress = null;
659 synchronized (p) {
660 if (isClosed())
661 throw new SocketException("Socket is closed");
662 checkAddress (p.getAddress(), "send");
663 if (connectState == ST_NOT_CONNECTED) {
664 // check the address is ok with the security manager on every send.
665 SecurityManager security = System.getSecurityManager();
666
667 // The reason you want to synchronize on datagram packet
668 // is because you don't want an applet to change the address
697 }
698 }
699
700 /**
701 * Receives a datagram packet from this socket. When this method
702 * returns, the {@code DatagramPacket}'s buffer is filled with
703 * the data received. The datagram packet also contains the sender's
704 * IP address, and the port number on the sender's machine.
705 * <p>
706 * This method blocks until a datagram is received. The
707 * {@code length} field of the datagram packet object contains
708 * the length of the received message. If the message is longer than
709 * the packet's length, the message is truncated.
710 * <p>
711 * If there is a security manager, a packet cannot be received if the
712 * security manager's {@code checkAccept} method
713 * does not allow it.
714 *
715 * @param p the {@code DatagramPacket} into which to place
716 * the incoming data.
717 * @throws IOException if an I/O error occurs.
718 * @throws SocketTimeoutException if setSoTimeout was previously called
719 * and the timeout has expired.
720 * @throws PortUnreachableException may be thrown if the socket is connected
721 * to a currently unreachable destination. Note, there is no guarantee that the
722 * exception will be thrown.
723 * @throws java.nio.channels.IllegalBlockingModeException
724 * if this socket has an associated channel,
725 * and the channel is in non-blocking mode.
726 * @see java.net.DatagramPacket
727 * @see java.net.DatagramSocket
728 * @revised 1.4
729 * @spec JSR-51
730 */
731 public synchronized void receive(DatagramPacket p) throws IOException {
732 synchronized (p) {
733 if (!isBound())
734 bind(new InetSocketAddress(0));
735 if (connectState == ST_NOT_CONNECTED) {
736 // check the address is ok with the security manager before every recv.
737 SecurityManager security = System.getSecurityManager();
738 if (security != null) {
739 while(true) {
740 String peekAd = null;
741 int peekPort = 0;
742 // peek at the packet to see who it is from.
743 if (!oldImpl) {
934 * network implementation as a hint to size the underlying
935 * network I/O buffers. The SO_SNDBUF setting may also be used
936 * by the network implementation to determine the maximum size
937 * of the packet that can be sent on this socket.
938 * <p>
939 * As SO_SNDBUF is a hint, applications that want to verify
940 * what size the buffer is should call {@link #getSendBufferSize()}.
941 * <p>
942 * Increasing the buffer size may allow multiple outgoing packets
943 * to be queued by the network implementation when the send rate
944 * is high.
945 * <p>
946 * Note: If {@link #send(DatagramPacket)} is used to send a
947 * {@code DatagramPacket} that is larger than the setting
948 * of SO_SNDBUF then it is implementation specific if the
949 * packet is sent or discarded.
950 *
951 * @param size the size to which to set the send buffer
952 * size. This value must be greater than 0.
953 *
954 * @throws SocketException if there is an error
955 * in the underlying protocol, such as an UDP error.
956 * @throws IllegalArgumentException if the value is 0 or is
957 * negative.
958 * @see #getSendBufferSize()
959 */
960 public synchronized void setSendBufferSize(int size)
961 throws SocketException{
962 if (!(size > 0)) {
963 throw new IllegalArgumentException("negative send size");
964 }
965 if (isClosed())
966 throw new SocketException("Socket is closed");
967 getImpl().setOption(SocketOptions.SO_SNDBUF, size);
968 }
969
970 /**
971 * Get value of the SO_SNDBUF option for this {@code DatagramSocket}, that is the
972 * buffer size used by the platform for output on this {@code DatagramSocket}.
973 *
974 * @return the value of the SO_SNDBUF option for this {@code DatagramSocket}
975 * @throws SocketException if there is an error in
976 * the underlying protocol, such as an UDP error.
977 * @see #setSendBufferSize
978 */
979 public synchronized int getSendBufferSize() throws SocketException {
980 if (isClosed())
981 throw new SocketException("Socket is closed");
982 int result = 0;
983 Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);
984 if (o instanceof Integer) {
985 result = ((Integer)o).intValue();
986 }
987 return result;
988 }
989
990 /**
991 * Sets the SO_RCVBUF option to the specified value for this
992 * {@code DatagramSocket}. The SO_RCVBUF option is used by
993 * the network implementation as a hint to size the underlying
994 * network I/O buffers. The SO_RCVBUF setting may also be used
995 * by the network implementation to determine the maximum size
996 * of the packet that can be received on this socket.
997 * <p>
998 * Because SO_RCVBUF is a hint, applications that want to
999 * verify what size the buffers were set to should call
1000 * {@link #getReceiveBufferSize()}.
1001 * <p>
1002 * Increasing SO_RCVBUF may allow the network implementation
1003 * to buffer multiple packets when packets arrive faster than
1004 * are being received using {@link #receive(DatagramPacket)}.
1005 * <p>
1006 * Note: It is implementation specific if a packet larger
1007 * than SO_RCVBUF can be received.
1008 *
1009 * @param size the size to which to set the receive buffer
1010 * size. This value must be greater than 0.
1011 *
1012 * @throws SocketException if there is an error in
1013 * the underlying protocol, such as an UDP error.
1014 * @throws IllegalArgumentException if the value is 0 or is
1015 * negative.
1016 * @see #getReceiveBufferSize()
1017 */
1018 public synchronized void setReceiveBufferSize(int size)
1019 throws SocketException{
1020 if (size <= 0) {
1021 throw new IllegalArgumentException("invalid receive size");
1022 }
1023 if (isClosed())
1024 throw new SocketException("Socket is closed");
1025 getImpl().setOption(SocketOptions.SO_RCVBUF, size);
1026 }
1027
1028 /**
1029 * Get value of the SO_RCVBUF option for this {@code DatagramSocket}, that is the
1030 * buffer size used by the platform for input on this {@code DatagramSocket}.
1031 *
1032 * @return the value of the SO_RCVBUF option for this {@code DatagramSocket}
1033 * @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
1034 * @see #setReceiveBufferSize(int)
1035 */
1036 public synchronized int getReceiveBufferSize()
1037 throws SocketException{
1038 if (isClosed())
1039 throw new SocketException("Socket is closed");
1040 int result = 0;
1041 Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
1042 if (o instanceof Integer) {
1043 result = ((Integer)o).intValue();
1044 }
1045 return result;
1046 }
1047
1048 /**
1049 * Enable/disable the SO_REUSEADDR socket option.
1050 * <p>
1051 * For UDP sockets it may be necessary to bind more than one
1052 * socket to the same socket address. This is typically for the
1053 * purpose of receiving multicast packets
1054 * (See {@link java.net.MulticastSocket}). The
1055 * {@code SO_REUSEADDR} socket option allows multiple
1056 * sockets to be bound to the same socket address if the
1057 * {@code SO_REUSEADDR} socket option is enabled prior
1058 * to binding the socket using {@link #bind(SocketAddress)}.
1059 * <p>
1060 * Note: This functionality is not supported by all existing platforms,
1061 * so it is implementation specific whether this option will be ignored
1062 * or not. However, if it is not supported then
1063 * {@link #getReuseAddress()} will always return {@code false}.
1064 * <p>
1065 * When a {@code DatagramSocket} is created the initial setting
1066 * of {@code SO_REUSEADDR} is disabled.
1067 * <p>
1068 * The behaviour when {@code SO_REUSEADDR} is enabled or
1069 * disabled after a socket is bound (See {@link #isBound()})
1070 * is not defined.
1071 *
1072 * @param on whether to enable or disable the
1073 * @throws SocketException if an error occurs enabling or
1074 * disabling the {@code SO_REUSEADDR} socket option,
1075 * or the socket is closed.
1076 * @since 1.4
1077 * @see #getReuseAddress()
1078 * @see #bind(SocketAddress)
1079 * @see #isBound()
1080 * @see #isClosed()
1081 */
1082 public synchronized void setReuseAddress(boolean on) throws SocketException {
1083 if (isClosed())
1084 throw new SocketException("Socket is closed");
1085 // Integer instead of Boolean for compatibility with older DatagramSocketImpl
1086 if (oldImpl)
1087 getImpl().setOption(SocketOptions.SO_REUSEADDR, on?-1:0);
1088 else
1089 getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
1090 }
1091
1092 /**
1093 * Tests if SO_REUSEADDR is enabled.
1094 *
1095 * @return a {@code boolean} indicating whether or not SO_REUSEADDR is enabled.
1096 * @throws SocketException if there is an error
1097 * in the underlying protocol, such as an UDP error.
1098 * @since 1.4
1099 * @see #setReuseAddress(boolean)
1100 */
1101 public synchronized boolean getReuseAddress() throws SocketException {
1102 if (isClosed())
1103 throw new SocketException("Socket is closed");
1104 Object o = getImpl().getOption(SocketOptions.SO_REUSEADDR);
1105 return ((Boolean)o).booleanValue();
1106 }
1107
1108 /**
1109 * Enable/disable SO_BROADCAST.
1110 *
1111 * <p> Some operating systems may require that the Java virtual machine be
1112 * started with implementation specific privileges to enable this option or
1113 * send broadcast datagrams.
1114 *
1115 * @param on
1116 * whether or not to have broadcast turned on.
1117 *
1118 * @throws SocketException
1119 * if there is an error in the underlying protocol, such as an UDP
1120 * error.
1121 *
1122 * @since 1.4
1123 * @see #getBroadcast()
1124 */
1125 public synchronized void setBroadcast(boolean on) throws SocketException {
1126 if (isClosed())
1127 throw new SocketException("Socket is closed");
1128 getImpl().setOption(SocketOptions.SO_BROADCAST, Boolean.valueOf(on));
1129 }
1130
1131 /**
1132 * Tests if SO_BROADCAST is enabled.
1133 * @return a {@code boolean} indicating whether or not SO_BROADCAST is enabled.
1134 * @throws SocketException if there is an error
1135 * in the underlying protocol, such as an UDP error.
1136 * @since 1.4
1137 * @see #setBroadcast(boolean)
1138 */
1139 public synchronized boolean getBroadcast() throws SocketException {
1140 if (isClosed())
1141 throw new SocketException("Socket is closed");
1142 return ((Boolean)(getImpl().getOption(SocketOptions.SO_BROADCAST))).booleanValue();
1143 }
1144
1145 /**
1146 * Sets traffic class or type-of-service octet in the IP
1147 * datagram header for datagrams sent from this DatagramSocket.
1148 * As the underlying network implementation may ignore this
1149 * value applications should consider it a hint.
1150 *
1151 * <P> The tc <B>must</B> be in the range {@code 0 <= tc <=
1152 * 255} or an IllegalArgumentException will be thrown.
1153 * <p>Notes:
1154 * <p>For Internet Protocol v4 the value consists of an
1273 */
1274 static DatagramSocketImplFactory factory;
1275
1276 /**
1277 * Sets the datagram socket implementation factory for the
1278 * application. The factory can be specified only once.
1279 * <p>
1280 * When an application creates a new datagram socket, the socket
1281 * implementation factory's {@code createDatagramSocketImpl} method is
1282 * called to create the actual datagram socket implementation.
1283 * <p>
1284 * Passing {@code null} to the method is a no-op unless the factory
1285 * was already set.
1286 *
1287 * <p>If there is a security manager, this method first calls
1288 * the security manager's {@code checkSetFactory} method
1289 * to ensure the operation is allowed.
1290 * This could result in a SecurityException.
1291 *
1292 * @param fac the desired factory.
1293 * @throws IOException if an I/O error occurs when setting the
1294 * datagram socket factory.
1295 * @throws SocketException if the factory is already defined.
1296 * @throws SecurityException if a security manager exists and its
1297 * {@code checkSetFactory} method doesn't allow the operation.
1298 * @see java.net.DatagramSocketImplFactory#createDatagramSocketImpl()
1299 * @see SecurityManager#checkSetFactory
1300 * @since 1.3
1301 */
1302 public static synchronized void
1303 setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
1304 throws IOException
1305 {
1306 if (factory != null) {
1307 throw new SocketException("factory already defined");
1308 }
1309 SecurityManager security = System.getSecurityManager();
1310 if (security != null) {
1311 security.checkSetFactory();
1312 }
1313 factory = fac;
1314 }
1315
1316 /**
|