DatagramSocket.java [plain text]
package java.net;
import java.io.IOException;
public class DatagramSocket
{
DatagramSocketImpl impl;
public DatagramSocket() throws SocketException
{
this(0, null);
}
public DatagramSocket(int port) throws SocketException
{
this(port, null);
}
public DatagramSocket(int port, InetAddress laddr) throws SocketException
{
if (port < 0 || port > 65535)
throw new IllegalArgumentException("Invalid port: " + port);
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkListen(port);
String propVal = System.getProperty("impl.prefix");
if (propVal == null || propVal.equals(""))
impl = new PlainDatagramSocketImpl();
else
try
{
impl = (DatagramSocketImpl) Class.forName("java.net." + propVal +
"DatagramSocketImpl").newInstance();
}
catch (Exception e)
{
System.err.println("Could not instantiate class: java.net." +
propVal + "DatagramSocketImpl");
impl = new PlainDatagramSocketImpl();
}
impl.create();
if (this instanceof MulticastSocket)
impl.setOption(SocketOptions.SO_REUSEADDR, new Boolean(true));
impl.bind(port, laddr == null ? InetAddress.ANY_IF : laddr);
}
public void close()
{
impl.close();
}
public InetAddress getLocalAddress()
{
SecurityManager s = System.getSecurityManager();
try
{
return (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
}
catch (SocketException ex)
{
}
try
{
return InetAddress.getLocalHost();
}
catch (UnknownHostException ex)
{
return null;
}
}
public int getLocalPort()
{
return impl.getLocalPort();
}
public synchronized int getSoTimeout() throws SocketException
{
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
if (timeout instanceof Integer)
return ((Integer)timeout).intValue();
else
return 0;
}
public synchronized void receive(DatagramPacket p) throws IOException
{
SecurityManager s = System.getSecurityManager();
if (s != null)
s.checkAccept(p.getAddress().getHostAddress(), p.getPort());
impl.receive(p);
}
public void send(DatagramPacket p) throws IOException
{
SecurityManager s = System.getSecurityManager();
if (s != null)
{
InetAddress addr = p.getAddress();
if (addr.isMulticastAddress())
s.checkMulticast(addr);
else
s.checkConnect(addr.getHostAddress(), p.getPort());
}
impl.send(p);
}
public synchronized void setSoTimeout(int timeout) throws SocketException
{
if (timeout < 0)
throw new IllegalArgumentException("Invalid timeout: " + timeout);
impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
}
public int getReceiveBufferSize() throws SocketException
{
Object obj = impl.getOption(SocketOptions.SO_RCVBUF);
if (obj instanceof Integer)
return(((Integer)obj).intValue());
else
throw new SocketException("Unexpected type");
}
public int getSendBufferSize() throws SocketException
{
Object obj = impl.getOption(SocketOptions.SO_SNDBUF);
if (obj instanceof Integer)
return(((Integer)obj).intValue());
else
throw new SocketException("Unexpected type");
}
public void setReceiveBufferSize(int size) throws SocketException
{
if (size < 0)
throw new IllegalArgumentException("Buffer size is less than 0");
impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
}
public void setSendBufferSize(int size) throws SocketException
{
if (size < 0)
throw new IllegalArgumentException("Buffer size is less than 0");
impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
}
}