Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.net

Class DatagramSocket

java.lang.Object
|
+--java.net.DatagramSocket


public class DatagramSocket

extends Object

This class models a connectionless datagram socket that sends individual packets of data across the network. In the TCP/IP world, this means UDP. Datagram packets do not have guaranteed delivery, or any guarantee about the order the data will be received on the remote host.

Authors:

Constructor Summary

DatagramSocket()

Initializes a new instance of DatagramSocket that binds to a random port and every address on the local machine.
DatagramSocket(int port)

Initializes a new instance of DatagramSocket that binds to the specified port and every address on the local machine.
DatagramSocket(int port, java.net.InetAddress laddr)

Initializes a new instance of DatagramSocket that binds to the specified local port and address.
DatagramSocket(java.net.SocketAddress address)

Initializes a new instance of DatagramSocket that binds to the specified local port and address.

Method Summary

voidbind(java.net.SocketAddress address)

Binds the socket to the given socket address.
voidclose()

Closes this datagram socket.
voidconnect(java.net.InetAddress addr, int port)

This method connects this socket to the specified address and port.
voidconnect(java.net.SocketAddress address)

Connects the datagram socket to a specified socket address.
voiddisconnect()

This method disconnects this socket from the addr/port it was connected to.
booleangetBroadcast()

Checks if SO_BROADCAST is enabled
java.nio.channels.DatagramChannelgetChannel()

Returns the datagram channel assoziated with this datagram socket.
java.net.InetAddressgetInetAddress()

This method returns the remote address to which this socket is connected.
java.net.InetAddressgetLocalAddress()

Returns the local address this datagram socket is bound to.
intgetLocalPort()

Returns the local port this socket is bound to.
java.net.SocketAddressgetLocalSocketAddress()

Returns the local SocketAddress this socket is bound to or null if it is not bound.
intgetPort()

This method returns the remote port to which this socket is connected.
intgetReceiveBufferSize()

This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.
java.net.SocketAddressgetRemoteSocketAddress()

Returns the SocketAddress of the host this socket is conneted to or null if this socket is not connected.
booleangetReuseAddress()

Checks if SO_REUSEADDR is enabled.
intgetSendBufferSize()

This method returns the value of the system level socket option SO_SNDBUF, which is used by the operating system to tune buffer sizes for data transfers.
synchronized intgetSoTimeout()

Returns the value of the socket's SO_TIMEOUT setting.
intgetTrafficClass()

Returns the current traffic class
booleanisBound()

Returns the binding state of the socket.
booleanisConnected()

Returns the connection state of the socket.
synchronized voidreceive(java.net.DatagramPacket p)

Reads a datagram packet from the socket.
voidsend(java.net.DatagramPacket p)

Sends the specified packet.
voidsetBroadcast(boolean on)

Enables/Disables SO_BROADCAST
static voidsetDatagramSocketImplFactory(java.net.DatagramSocketImplFactory fac)

Sets the datagram socket implementation factory for the application
voidsetReceiveBufferSize(int size)

This method sets the value for the system level socket option SO_RCVBUF to the specified value.
voidsetReuseAddress(boolean on)

Enables/Disables SO_REUSEADDR.
voidsetSendBufferSize(int size)

This method sets the value for the system level socket option SO_SNDBUF to the specified value.
voidsetSoTimeout(int timeout)

Sets the value of the socket's SO_TIMEOUT value.
voidsetTrafficClass(int tc)

Sets the traffic class value

Constructor Details

DatagramSocket

public DatagramSocket()

Initializes a new instance of DatagramSocket that binds to a random port and every address on the local machine.

Throws:


DatagramSocket

public DatagramSocket(int port)

Initializes a new instance of DatagramSocket that binds to the specified port and every address on the local machine.

Parameters:

Throws:


DatagramSocket

public DatagramSocket(int port, java.net.InetAddress laddr)

Initializes a new instance of DatagramSocket that binds to the specified local port and address.

Parameters:

Throws:


DatagramSocket

public DatagramSocket(java.net.SocketAddress address)

Initializes a new instance of DatagramSocket that binds to the specified local port and address.

Since:Parameters:

Throws:


Method Details

bind

public void bind(java.net.SocketAddress address)

Binds the socket to the given socket address.

Since:Parameters:

Throws:


close

public void close()

Closes this datagram socket.


connect

public void connect(java.net.InetAddress addr, int port)

This method connects this socket to the specified address and port. When a datagram socket is connected, it will only send or receive packate to and from the host to which it is connected. A multicast socket that is connected may only send and not receive packets.

Since:Parameters:

Throws:


connect

public void connect(java.net.SocketAddress address)

Connects the datagram socket to a specified socket address.

Since:Parameters:

Throws:


disconnect

public void disconnect()

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:

getBroadcast

public boolean getBroadcast()

Checks if SO_BROADCAST is enabled

Since:Throws:


getChannel

public DatagramChannel getChannel()

Returns the datagram channel assoziated with this datagram socket.

Since:

getInetAddress

public InetAddress getInetAddress()

This method returns the remote address to which this socket is connected. If this socket is not connected, then this method will return null.

Since:Returns:


getLocalAddress

public InetAddress getLocalAddress()

Returns the local address this datagram socket is bound to.

Since:

getLocalPort

public int getLocalPort()

Returns the local port this socket is bound to.

Returns:


getLocalSocketAddress

public SocketAddress getLocalSocketAddress()

Returns the local SocketAddress this socket is bound to or null if it is not bound.

Since:

getPort

public int getPort()

This method returns the remote port to which this socket is connected. If this socket is not connected, then this method will return -1.

Since:Returns:


getReceiveBufferSize

public int getReceiveBufferSize()

This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.

Since:Returns:

Throws:


getRemoteSocketAddress

public SocketAddress getRemoteSocketAddress()

Returns the SocketAddress of the host this socket is conneted to or null if this socket is not connected.

Since:

getReuseAddress

public boolean getReuseAddress()

Checks if SO_REUSEADDR is enabled.

Since:Throws:


getSendBufferSize

public int getSendBufferSize()

This method returns the value of the system level socket option SO_SNDBUF, which is used by the operating system to tune buffer sizes for data transfers.

Since:Returns:

Throws:


getSoTimeout

public synchronized int getSoTimeout()

Returns the value of the socket's SO_TIMEOUT setting. If this method returns 0 then SO_TIMEOUT is disabled.

Since:Returns:

Throws:


getTrafficClass

public int getTrafficClass()

Returns the current traffic class

Since:Throws:

See Also:


isBound

public boolean isBound()

Returns the binding state of the socket.

Since:

isConnected

public boolean isConnected()

Returns the connection state of the socket.

Since:

receive

public synchronized void receive(java.net.DatagramPacket p)

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 DatagramPacket is populated with the data received and all the other information about the packet.

Parameters:

Throws:


send

public void send(java.net.DatagramPacket p)

Sends the specified packet. The host and port to which the packet are to be sent should be set inside the packet.

Parameters:

Throws:


setBroadcast

public void setBroadcast(boolean on)

Enables/Disables SO_BROADCAST

Since:Parameters:

Throws:


setDatagramSocketImplFactory

public static void setDatagramSocketImplFactory(java.net.DatagramSocketImplFactory fac)

Sets the datagram socket implementation factory for the application

Parameters:

Throws:


setReceiveBufferSize

public void setReceiveBufferSize(int size)

This method sets the value for the system level socket option SO_RCVBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Since:Parameters:

Throws:


setReuseAddress

public void setReuseAddress(boolean on)

Enables/Disables SO_REUSEADDR.

Since:Parameters:

Throws:


setSendBufferSize

public void setSendBufferSize(int size)

This method sets the value for the system level socket option SO_SNDBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Since:Parameters:

Throws:


setSoTimeout

public void setSoTimeout(int timeout)

Sets the value of the socket's SO_TIMEOUT value. A value of 0 will disable SO_TIMEOUT. Any other value is the number of milliseconds a socket read/write will block before timing out.

Since:Parameters:

Throws:


setTrafficClass

public void setTrafficClass(int tc)

Sets the traffic class value

Since:Parameters:

Throws:

See Also: