3 /* Copyright (C) 1999 Red Hat, Inc.
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 import java
.io
.IOException
;
15 * @author Warren Levy <warrenl@cygnus.com>
20 * Written using on-line Java Platform 1.2 API Specification, as well
21 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
22 * Status: Believed complete and correct.
25 public class DatagramSocket
27 DatagramSocketImpl impl
;
29 public DatagramSocket() throws SocketException
31 this(0, ServerSocket
.ANY_IF
);
34 public DatagramSocket(int port
) throws SocketException
36 this(port
, ServerSocket
.ANY_IF
);
39 public DatagramSocket(int port
, InetAddress laddr
) throws SocketException
41 if (port
< 0 || port
> 65535)
42 throw new IllegalArgumentException("Invalid port: " + port
);
44 SecurityManager s
= System
.getSecurityManager();
48 String propVal
= System
.getProperty("impl.prefix");
49 if (propVal
== null || propVal
.equals(""))
50 impl
= new PlainDatagramSocketImpl();
54 impl
= (DatagramSocketImpl
) Class
.forName("java.net." + propVal
+
55 "DatagramSocketImpl").newInstance();
59 System
.err
.println("Could not instantiate class: java.net." +
60 propVal
+ "DatagramSocketImpl");
61 impl
= new PlainDatagramSocketImpl();
65 // For multicasting, set the socket to be reused (Stevens pp. 195-6).
66 if (this instanceof MulticastSocket
)
67 impl
.setOption(SocketOptions
.SO_REUSEADDR
, new Boolean(true));
69 impl
.bind(port
, laddr
);
77 public InetAddress
getLocalAddress()
79 SecurityManager s
= System
.getSecurityManager();
80 // FIXME: JCL p. 510 says this should call checkConnect. But what
81 // string should be used as the hostname? Maybe this is just a side
82 // effect of calling InetAddress.getLocalHost.
84 // And is getOption with SO_BINDADDR the right way to get the address?
85 // Doesn't seem to be since this method doesn't throw a SocketException
86 // and SO_BINADDR can throw one.
88 // Also see RETURNS section in JCL p. 510 about returning any local
89 // addr "if the current execution context is not allowed to connect to
90 // the network interface that is actually bound to this datagram socket."
91 // How is that done? via InetAddress.getLocalHost? But that throws
92 // an UnknownHostException and this method doesn't.
95 // s.checkConnect("localhost", -1);
98 return (InetAddress
)impl
.getOption(SocketOptions
.SO_BINDADDR
);
100 catch (SocketException ex
)
106 return InetAddress
.getLocalHost();
108 catch (UnknownHostException ex
)
110 // FIXME: This should never happen, so how can we avoid this construct?
115 public int getLocalPort()
117 return impl
.getLocalPort();
120 public synchronized int getSoTimeout() throws SocketException
122 Object timeout
= impl
.getOption(SocketOptions
.SO_TIMEOUT
);
123 if (timeout
instanceof Integer
)
124 return ((Integer
)timeout
).intValue();
129 public synchronized void receive(DatagramPacket p
) throws IOException
131 SecurityManager s
= System
.getSecurityManager();
133 s
.checkAccept(p
.getAddress().getHostAddress(), p
.getPort());
138 public void send(DatagramPacket p
) throws IOException
140 // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api.
141 SecurityManager s
= System
.getSecurityManager();
144 InetAddress addr
= p
.getAddress();
145 if (addr
.isMulticastAddress())
146 s
.checkMulticast(addr
);
148 s
.checkConnect(addr
.getHostAddress(), p
.getPort());
151 // FIXME: if this is a subclass of MulticastSocket, use getTTL for TTL val.
155 public synchronized void setSoTimeout(int timeout
) throws SocketException
158 throw new IllegalArgumentException("Invalid timeout: " + timeout
);
160 impl
.setOption(SocketOptions
.SO_TIMEOUT
, new Integer(timeout
));
164 // public void connect(InetAddress address, int port)
169 // public void disconnect()
174 // public InetAddress getInetAddress()
179 // public int getPort()
184 // public int getReceiveBufferSize() throws SocketException
189 // public int getSendBufferSize() throws SocketException
194 // public void setReceiveBufferSize(int size) throws SocketException
199 // public void setSendBufferSize(int size) throws SocketException