datagramsocket.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 742 行 · 第 1/2 页

JAVA
742
字号
	 * packate to and from the host to which it is connected.  A multicast
	 * socket that is connected may only send and not receive packets.
	 *
	 * @param addr The address to connect this socket to.
	 * @param port The port to connect this socket to.
	 *
	 * @exception SecurityException If connections to this addr/port are not
	 * allowed.
	 * @exception IllegalArgumentException If the addr or port are invalid.
	 *
	 * @since 1.2
	 */
	public void connect(InetAddress addr, int port)
		throws SecurityException, IllegalArgumentException {
		if (addr == null)
			throw new IllegalArgumentException("Connect address is null");

		if ((port < 1) || (port > 65535))
			throw new IllegalArgumentException("Bad port number: " + port);

		SecurityManager sm = System.getSecurityManager();
		if (sm != null)
			sm.checkConnect(addr.getHostName(), port);

		this.remoteAddress = addr;
		this.remotePort = port;

		/* FIXME: Shit, we can't do this even though the OS supports it since this 
		   method isn't in DatagramSocketImpl. */
		//  impl.connect(addr, port);

		connected = true;
	}

	/**
	 * This method disconnects this socket from the addr/port it was 
	 * connected to.  If the socket was not connected in the first place,
	 * this method does nothing.
	 *
	 * @since 1.2
	 */
	public void disconnect() {
		// FIXME: See my comments on connect()
		this.remoteAddress = null;
		this.remotePort = -1;
		connected = false;
	}

	/**
	 * Reads a datagram packet from the socket.  Note that this method
	 * will block until a packet is received from the network.  On return,
	 * the passed in <code>DatagramPacket</code> is populated with the data 
	 * received and all the other information about the packet.
	 *
	 * @param p A <code>DatagramPacket</code> for storing the data
	 *
	 * @exception IOException If an error occurs
	 */
	public synchronized void receive(DatagramPacket p) throws IOException {
		impl.receive(p);

		SecurityManager s = System.getSecurityManager();
		if (s != null && isConnected())
			s.checkAccept(p.getAddress().getHostName(), p.getPort());
	}

	/**
	 * Sends the specified packet.  The host and port to which the packet
	 * are to be sent should be set inside the packet.
	 *
	 * @param p The datagram packet to send.
	 *
	 * @exception IOException If an error occurs.
	 * @exception SecurityException If a security manager exists and its
	 * checkMulticast or checkConnect method doesn't allow the send.
	 * @exception PortUnreachableException If the socket is connected to a
	 * currently unreachable destination. Note, there is no guarantee that the
	 * exception will be thrown.
	 * @exception IllegalBlockingModeException If this socket has an associated
	 * channel, and the channel is in non-blocking mode.
	 */
	public void send(DatagramPacket p) throws IOException {
		if (!connected) {
			SecurityManager s = System.getSecurityManager();
			if (s != null) {
				InetAddress addr = p.getAddress();
				if (addr.isMulticastAddress())
					s.checkMulticast(addr);
				else
					s.checkConnect(addr.getHostAddress(), p.getPort());
			}
		} else {
			if (!p.getAddress().equals(remoteAddress)) {
				throw new SecurityException("Can only send to the connected remote address");
			}
			if (p.getPort() != remotePort) {
				throw new SecurityException("Can only send to the connected remote port");
			}
		}

		// FIXME: if this is a subclass of MulticastSocket, use getTTL for TTL val.
		impl.send(p);
	}

	/**
	 * Binds the socket to the given socket address.
	 *
	 * @param address The socket address to bind to.
	 *
	 * @exception SocketException If an error occurs.
	 * @exception SecurityException If a security manager exists and
	 * its checkListen method doesn't allow the operation.
	 * @exception IllegalArgumentException If address type is not supported.
	 *
	 * @since 1.4
	 */
	public void bind(SocketAddress address) throws SocketException {
		if (!(address instanceof InetSocketAddress))
			throw new IllegalArgumentException();

		InetSocketAddress tmp = (InetSocketAddress)address;

		SecurityManager s = System.getSecurityManager();
		if (s != null)
			s.checkListen(tmp.getPort());

		impl.bind(tmp.getPort(), tmp.getAddress());
	}

	/**
	 * Returns the datagram channel assoziated with this datagram socket.
	 * 
	 * @since 1.4
	 */
	public DatagramChannel getChannel() {
		return ch;
	}

	/**
	 * Connects the datagram socket to a specified socket address.
	 *
	 * @param address The socket address to connect to.
	 *
	 * @exception SocketException If an error occurs.
	 * @exception IllegalArgumentException If address type is not supported.
	 *
	 * @since 1.4
	 */
	public void connect(SocketAddress address) throws SocketException {
		if (!(address instanceof InetSocketAddress))
			throw new IllegalArgumentException("SocketAddress is not InetSocketAddress");

		InetSocketAddress tmp = (InetSocketAddress)address;
		connect(tmp.getAddress(), tmp.getPort());
	}

	/**
	 * Returns the binding state of the socket.
	 * 
	 * @since 1.4
	 */
	public boolean isBound() {
		try {
			Object bindaddr = impl.getOption(SocketOptions.SO_BINDADDR);
		} catch (SocketException e) {
			return false;
		}

		return true;
	}

	/**
	 * Returns the connection state of the socket.
	 * 
	 * @since 1.4
	 */
	public boolean isConnected() {
		return remoteAddress != null;
	}

	/**
	 * Returns the SocketAddress of the host this socket is conneted to
	 * or null if this socket is not connected.
	 * 
	 * @since 1.4
	 */
	public SocketAddress getRemoteSocketAddress() {
		if (!isConnected())
			return null;

		return new InetSocketAddress(remoteAddress, remotePort);
	}

	/**
	 * Returns the local SocketAddress this socket is bound to
	 * or null if it is not bound.
	 * 
	 * @since 1.4
	 */
	public SocketAddress getLocalSocketAddress() {
		InetAddress addr;

		try {
			addr = (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
		} catch (SocketException e) {
			return null;
		}

		return new InetSocketAddress(local_addr, impl.localPort);
	}

	/**
	 * Enables/Disables SO_REUSEADDR.
	 * 
	 * @param on Whether or not to have SO_REUSEADDR turned on.
	 *
	 * @exception SocketException If an error occurs.
	 *
	 * @since 1.4
	 */
	public void setReuseAddress(boolean on) throws SocketException {
		if (impl == null)
			throw new SocketException("Cannot initialize Socket implementation");

		impl.setOption(SocketOptions.SO_REUSEADDR, new Boolean(on));
	}

	/**
	 * Checks if SO_REUSEADDR is enabled.
	 *
	 * @exception SocketException If an error occurs.
	 * 
	 * @since 1.4
	 */
	public boolean getReuseAddress() throws SocketException {
		if (impl == null)
			throw new SocketException("Cannot initialize Socket implementation");

		Object obj = impl.getOption(SocketOptions.SO_REUSEADDR);

		if (obj instanceof Boolean)
			return (((Boolean)obj).booleanValue());
		else
			throw new SocketException("Unexpected type");
	}

	/**
	 * Enables/Disables SO_BROADCAST
	 * 
	 * @param on Whether or not to have SO_BROADCAST turned on
	 *
	 * @exception SocketException If an error occurs
	 *
	 * @since 1.4
	 */
	public void setBroadcast(boolean on) throws SocketException {
		if (impl == null)
			throw new SocketException("Cannot initialize Socket implementation");

		impl.setOption(SocketOptions.SO_BROADCAST, new Boolean(on));
	}

	/**
	 * Checks if SO_BROADCAST is enabled
	 * 
	 * @exception SocketException If an error occurs
	 * 
	 * @since 1.4
	 */
	public boolean getBroadcast() throws SocketException {
		if (impl == null)
			throw new SocketException("Cannot initialize Socket implementation");

		Object obj = impl.getOption(SocketOptions.SO_BROADCAST);

		if (obj instanceof Boolean)
			return ((Boolean)obj).booleanValue();
		else
			throw new SocketException("Unexpected type");
	}

	/**
	 * Sets the traffic class value
	 *
	 * @param tc The traffic class
	 *
	 * @exception SocketException If an error occurs
	 * @exception IllegalArgumentException If tc value is illegal
	 *
	 * @see DatagramSocket:getTrafficClass
	 * 
	 * @since 1.4
	 */
	public void setTrafficClass(int tc) throws SocketException {
		if (impl == null)
			throw new SocketException("Cannot initialize Socket implementation");

		if (tc < 0 || tc > 255)
			throw new IllegalArgumentException();

		impl.setOption(SocketOptions.IP_TOS, new Integer(tc));
	}

	/**
	 * Returns the current traffic class
	 * 
	 * @see DatagramSocket:setTrafficClass
	 *
	 * @exception SocketException If an error occurs
	 * 
	 * @since 1.4
	 */
	public int getTrafficClass() throws SocketException {
		if (impl == null)
			throw new SocketException("Cannot initialize Socket implementation");

		Object obj = impl.getOption(SocketOptions.IP_TOS);

		if (obj instanceof Integer)
			return ((Integer)obj).intValue();
		else
			throw new SocketException("Unexpected type");
	}

	/**
	 * Sets the datagram socket implementation factory for the application
	 *
	 * @param fac The factory to set
	 *
	 * @exception IOException If an error occurs
	 * @exception SocketException If the factory is already defined
	 * @exception SecurityException If a security manager exists and its
	 * checkSetFactory method doesn't allow the operation
	 */
	public static void setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
		throws IOException {
		if (factory != null)
			throw new SocketException("DatagramSocketImplFactory already defined");

		SecurityManager sm = System.getSecurityManager();
		if (sm != null)
			sm.checkSetFactory();

		factory = fac;
	}

	/**
	 * Sets the datagram socket implementation factory for the application
	 *
	 * @param fac The factory to set
	 *
	 * @exception IOException If an error occurs
	 * @exception SocketException If the factory is already defined
	 * @exception SecurityException If a security manager exists and its
	 * checkSetFactory method doesn't allow the operation
	 */
	public static void setSystemDatagramSocketImplFactory(DatagramSocketImplFactory fac)
	throws IOException {
		if (systemFactory != null) {
			throw new SocketException("System wide DatagramSocketImplFactory already defined");
		}

		final SecurityManager sm = System.getSecurityManager();
		if (sm != null) {
			sm.checkSetFactory();
		}

		systemFactory = fac;
	}
} // class DatagramSocket

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?