OSDN Git Service

* All files: Updated copyright information.
[pf3gnuchains/gcc-fork.git] / libjava / java / net / DatagramSocket.java
1 // DatagramSocket.java
2
3 /* Copyright (C) 1999, 2000  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package java.net;
12 import java.io.IOException;
13
14 /**
15  * @author Warren Levy <warrenl@cygnus.com>
16  * @date May 3, 1999.
17  */
18
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.
23  */
24
25 public class DatagramSocket
26 {
27   DatagramSocketImpl impl;
28
29   public DatagramSocket() throws SocketException
30   {
31     this(0, ServerSocket.ANY_IF);
32   }
33
34   public DatagramSocket(int port) throws SocketException
35   {
36     this(port, ServerSocket.ANY_IF);
37   }
38
39   public DatagramSocket(int port, InetAddress laddr) throws SocketException
40   {
41     if (port < 0 || port > 65535)
42       throw new IllegalArgumentException("Invalid port: " + port);
43
44     SecurityManager s = System.getSecurityManager();
45     if (s != null)
46       s.checkListen(port);
47
48     String propVal = System.getProperty("impl.prefix");
49     if (propVal == null || propVal.equals(""))
50       impl = new PlainDatagramSocketImpl();
51     else
52       try
53         {
54           impl = (DatagramSocketImpl) Class.forName("java.net." + propVal +
55                                         "DatagramSocketImpl").newInstance();
56         }
57       catch (Exception e)
58         {
59           System.err.println("Could not instantiate class: java.net." +
60             propVal + "DatagramSocketImpl");
61           impl = new PlainDatagramSocketImpl();
62         }
63     impl.create();
64
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));
68
69     impl.bind(port, laddr == null ? ServerSocket.ANY_IF : laddr);
70   }
71
72   public void close()
73   {
74     impl.close();
75   }
76
77   public InetAddress getLocalAddress()
78   {
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.
83     //
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.
87     //
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.
93     //
94     // if (s != null)
95     //   s.checkConnect("localhost", -1);
96     try
97       {
98         return (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
99       }
100     catch (SocketException ex)
101       {
102       }
103
104     try
105       {
106         return InetAddress.getLocalHost();
107       }
108     catch (UnknownHostException ex)
109       {
110         // FIXME: This should never happen, so how can we avoid this construct?
111         return null;
112       }
113   }
114
115   public int getLocalPort()
116   {
117     return impl.getLocalPort();
118   }
119
120   public synchronized int getSoTimeout() throws SocketException
121   {
122     Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
123     if (timeout instanceof Integer) 
124       return ((Integer)timeout).intValue();
125     else
126       return 0;
127   }
128
129   public synchronized void receive(DatagramPacket p) throws IOException
130   {
131     SecurityManager s = System.getSecurityManager();
132     if (s != null)
133       s.checkAccept(p.getAddress().getHostAddress(), p.getPort());
134
135     impl.receive(p);
136   }
137
138   public void send(DatagramPacket p) throws IOException
139   {
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)
143       {
144         InetAddress addr = p.getAddress();
145         if (addr.isMulticastAddress())
146           s.checkMulticast(addr);
147         else
148           s.checkConnect(addr.getHostAddress(), p.getPort());
149       }
150
151     // FIXME: if this is a subclass of MulticastSocket, use getTTL for TTL val.
152     impl.send(p);
153   }
154
155   public synchronized void setSoTimeout(int timeout) throws SocketException
156   {
157     if (timeout < 0)
158       throw new IllegalArgumentException("Invalid timeout: " + timeout);
159
160     impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
161   }
162
163   // JDK1.2
164   // public void connect(InetAddress address, int port)
165   // {
166   // }
167
168   // JDK1.2
169   // public void disconnect()
170   // {
171   // }
172
173   // JDK1.2
174   // public InetAddress getInetAddress()
175   // {
176   // }
177
178   // JDK1.2
179   // public int getPort()
180   // {
181   // }
182
183   // JDK1.2
184   // public int getReceiveBufferSize() throws SocketException
185   // {
186   // }
187
188   // JDK1.2
189   // public int getSendBufferSize() throws SocketException
190   // {
191   // }
192
193   // JDK1.2
194   // public void setReceiveBufferSize(int size) throws SocketException
195   // {
196   // }
197
198   // JDK1.2
199   // public void setSendBufferSize(int size) throws SocketException
200   // {
201   // }
202 }