* All files: Updated copyright to reflect Cygnus purchase.
[official-gcc.git] / libjava / java / net / DatagramSocket.java
blob33d666b7b6dc7afb1cf06fd23eec4c72015c8f33
1 // DatagramSocket.java
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
9 details. */
11 package java.net;
12 import java.io.IOException;
14 /**
15 * @author Warren Levy <warrenl@cygnus.com>
16 * @date May 3, 1999.
19 /**
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();
45 if (s != null)
46 s.checkListen(port);
48 String propVal = System.getProperty("impl.prefix");
49 if (propVal == null || propVal.equals(""))
50 impl = new PlainDatagramSocketImpl();
51 else
52 try
54 impl = (DatagramSocketImpl) Class.forName("java.net." + propVal +
55 "DatagramSocketImpl").newInstance();
57 catch (Exception e)
59 System.err.println("Could not instantiate class: java.net." +
60 propVal + "DatagramSocketImpl");
61 impl = new PlainDatagramSocketImpl();
63 impl.create();
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);
72 public void close()
74 impl.close();
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.
94 // if (s != null)
95 // s.checkConnect("localhost", -1);
96 try
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?
111 return null;
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();
125 else
126 return 0;
129 public synchronized void receive(DatagramPacket p) throws IOException
131 SecurityManager s = System.getSecurityManager();
132 if (s != null)
133 s.checkAccept(p.getAddress().getHostAddress(), p.getPort());
135 impl.receive(p);
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();
142 if (s != null)
144 InetAddress addr = p.getAddress();
145 if (addr.isMulticastAddress())
146 s.checkMulticast(addr);
147 else
148 s.checkConnect(addr.getHostAddress(), p.getPort());
151 // FIXME: if this is a subclass of MulticastSocket, use getTTL for TTL val.
152 impl.send(p);
155 public synchronized void setSoTimeout(int timeout) throws SocketException
157 if (timeout < 0)
158 throw new IllegalArgumentException("Invalid timeout: " + timeout);
160 impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
163 // JDK1.2
164 // public void connect(InetAddress address, int port)
165 // {
166 // }
168 // JDK1.2
169 // public void disconnect()
170 // {
171 // }
173 // JDK1.2
174 // public InetAddress getInetAddress()
175 // {
176 // }
178 // JDK1.2
179 // public int getPort()
180 // {
181 // }
183 // JDK1.2
184 // public int getReceiveBufferSize() throws SocketException
185 // {
186 // }
188 // JDK1.2
189 // public int getSendBufferSize() throws SocketException
190 // {
191 // }
193 // JDK1.2
194 // public void setReceiveBufferSize(int size) throws SocketException
195 // {
196 // }
198 // JDK1.2
199 // public void setSendBufferSize(int size) throws SocketException
200 // {
201 // }