OSDN Git Service

e82681a7f61dc27379835625c1c03d85eced8dce
[pf3gnuchains/gcc-fork.git] / libjava / java / net / DatagramSocket.java
1 // DatagramSocket.java
2
3 /* Copyright (C) 1999  Cygnus Solutions
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   // FIXME: Shouldn't this be determined by getsockname() instead?
29   InetAddress laddr;
30
31   public DatagramSocket() throws SocketException
32   {
33     this(0, null);
34   }
35
36   public DatagramSocket(int port) throws SocketException
37   {
38     this(port, null);
39   }
40
41   public DatagramSocket(int port, InetAddress laddr) throws SocketException
42   {
43     if (port < 0 || port > 65535)
44       throw new IllegalArgumentException("Invalid port: " + port);
45
46     SecurityManager s = System.getSecurityManager();
47     if (s != null)
48       s.checkListen(port);
49
50     String propVal = System.getProperty("impl.prefix");
51     if (propVal == null || propVal.equals(""))
52       propVal = "Plain";
53     impl = (DatagramSocketImpl) Class.forName("java.net." + propVal +
54                                         "DatagramSocketImpl").newInstance();
55     impl.create();
56     // TBD: if this is right then the same should be done in Socket().
57     try
58     {
59       impl.bind(port, laddr == null ? InetAddress.getLocalHost() : laddr);
60     }
61     catch (UnknownHostException e)
62     {
63       throw new BindException(e.getMessage());
64     }
65     this.laddr = laddr;
66   }
67
68   public void close()
69   {
70     impl.close();
71   }
72
73   public InetAddress getLocalAddress()
74   {
75     return laddr;
76   }
77
78   public int getLocalPort()
79   {
80     return impl.getLocalPort();
81   }
82
83   public synchronized int getSoTimeout() throws SocketException
84   {
85     // FIXME: TODO - DatagramSocket.getSoTimeout
86      throw new SocketException("DatagramSocket.getSoTimeout - not yet implemented");
87   }
88
89   public synchronized void receive(DatagramPacket p) throws IOException
90   {
91     SecurityManager s = System.getSecurityManager();
92     if (s != null)
93       s.checkAccept(p.getAddress().getHostAddress(), p.getPort());
94
95     impl.receive(p);
96   }
97
98   public void send(DatagramPacket p) throws IOException
99   {
100     // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api.
101     SecurityManager s = System.getSecurityManager();
102     if (s != null)
103       {
104         InetAddress addr = p.getAddress();
105         if (addr.isMulticastAddress())
106           s.checkMulticast(addr);
107         else
108           s.checkConnect(addr.getHostAddress(), p.getPort());
109       }
110
111     // FIXME: if this is a subclass of MulticastSocket, use getTTL for TTL val.
112     impl.send(p);
113   }
114
115   public synchronized void setSoTimeout(int timeout) throws SocketException
116   {
117     // FIXME: TODO - DatagramSocket.setSoTimeout
118     throw new SocketException("DatagramSocket.setSoTimeout - not yet implemented");
119   }
120
121   // JDK1.2
122   // public void connect(InetAddress address, int port)
123   // {
124   // }
125
126   // JDK1.2
127   // public void disconnect()
128   // {
129   // }
130
131   // JDK1.2
132   // public InetAddress getInetAddress()
133   // {
134   // }
135
136   // JDK1.2
137   // public int getPort()
138   // {
139   // }
140
141   // JDK1.2
142   // public int getReceiveBufferSize() throws SocketException
143   // {
144   // }
145
146   // JDK1.2
147   // public int getSendBufferSize() throws SocketException
148   // {
149   // }
150
151   // JDK1.2
152   // public void setReceiveBufferSize(int size) throws SocketException
153   // {
154   // }
155
156   // JDK1.2
157   // public void setSendBufferSize(int size) throws SocketException
158   // {
159   // }
160 }