3 /* Copyright (C) 1999 Cygnus Solutions
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;
28 // FIXME: Shouldn't this be determined by getsockname() instead?
31 public DatagramSocket() throws SocketException
36 public DatagramSocket(int port) throws SocketException
41 public DatagramSocket(int port, InetAddress laddr) throws SocketException
43 if (port < 0 || port > 65535)
44 throw new IllegalArgumentException("Invalid port: " + port);
46 SecurityManager s = System.getSecurityManager();
50 String propVal = System.getProperty("impl.prefix");
51 if (propVal == null || propVal.equals(""))
53 impl = (DatagramSocketImpl) Class.forName("java.net." + propVal +
54 "DatagramSocketImpl").newInstance();
56 // TBD: if this is right then the same should be done in Socket().
59 impl.bind(port, laddr == null ? InetAddress.getLocalHost() : laddr);
61 catch (UnknownHostException e)
63 throw new BindException(e.getMessage());
73 public InetAddress getLocalAddress()
78 public int getLocalPort()
80 return impl.getLocalPort();
83 public synchronized int getSoTimeout() throws SocketException
85 // FIXME: TODO - DatagramSocket.getSoTimeout
86 throw new SocketException("DatagramSocket.getSoTimeout - not yet implemented");
89 public synchronized void receive(DatagramPacket p) throws IOException
91 SecurityManager s = System.getSecurityManager();
93 s.checkAccept(p.getAddress().getHostAddress(), p.getPort());
98 public void send(DatagramPacket p) throws IOException
100 // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api.
101 SecurityManager s = System.getSecurityManager();
104 InetAddress addr = p.getAddress();
105 if (addr.isMulticastAddress())
106 s.checkMulticast(addr);
108 s.checkConnect(addr.getHostAddress(), p.getPort());
111 // FIXME: if this is a subclass of MulticastSocket, use getTTL for TTL val.
115 public synchronized void setSoTimeout(int timeout) throws SocketException
117 // FIXME: TODO - DatagramSocket.setSoTimeout
118 throw new SocketException("DatagramSocket.setSoTimeout - not yet implemented");
122 // public void connect(InetAddress address, int port)
127 // public void disconnect()
132 // public InetAddress getInetAddress()
137 // public int getPort()
142 // public int getReceiveBufferSize() throws SocketException
147 // public int getSendBufferSize() throws SocketException
152 // public void setReceiveBufferSize(int size) throws SocketException
157 // public void setSendBufferSize(int size) throws SocketException