Android APIs
public class

LocalSocket

extends Object
implements Closeable
java.lang.Object
   ↳ android.net.LocalSocket

Class Overview

Creates a (non-server) socket in the UNIX-domain namespace. The interface here is not entirely unlike that of java.net.Socket

Summary

Constants
int SOCKET_DGRAM Datagram socket type
int SOCKET_SEQPACKET Sequential packet socket type
int SOCKET_STREAM Stream socket type
Public Constructors
LocalSocket()
Creates a AF_LOCAL/UNIX domain stream socket.
LocalSocket(int sockType)
Creates a AF_LOCAL/UNIX domain stream socket with given socket type
Public Methods
void bind(LocalSocketAddress bindpoint)
Binds this socket to an endpoint name.
void close()
Closes the socket.
void connect(LocalSocketAddress endpoint, int timeout)
void connect(LocalSocketAddress endpoint)
Connects this socket to an endpoint.
FileDescriptor[] getAncillaryFileDescriptors()
Retrieves a set of file descriptors that a peer has sent through an ancillary message.
FileDescriptor getFileDescriptor()
Returns file descriptor or null if not yet open/already closed
InputStream getInputStream()
Retrieves the input stream for this instance.
LocalSocketAddress getLocalSocketAddress()
Retrieves the name that this socket is bound to, if any.
OutputStream getOutputStream()
Retrieves the output stream for this instance.
Credentials getPeerCredentials()
Retrieves the credentials of this socket's peer.
int getReceiveBufferSize()
LocalSocketAddress getRemoteSocketAddress()
int getSendBufferSize()
int getSoTimeout()
synchronized boolean isBound()
boolean isClosed()
synchronized boolean isConnected()
boolean isInputShutdown()
boolean isOutputShutdown()
void setFileDescriptorsForSend(FileDescriptor[] fds)
Enqueues a set of file descriptors to send to the peer.
void setReceiveBufferSize(int size)
void setSendBufferSize(int n)
void setSoTimeout(int n)
void shutdownInput()
Shuts down the input side of the socket.
void shutdownOutput()
Shuts down the output side of the socket.
String toString()
Returns a string containing a concise, human-readable description of this object.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Constants

public static final int SOCKET_DGRAM

Added in API level 19

Datagram socket type

Constant Value: 1 (0x00000001)

public static final int SOCKET_SEQPACKET

Added in API level 19

Sequential packet socket type

Constant Value: 3 (0x00000003)

public static final int SOCKET_STREAM

Added in API level 19

Stream socket type

Constant Value: 2 (0x00000002)

Public Constructors

public LocalSocket ()

Added in API level 1

Creates a AF_LOCAL/UNIX domain stream socket.

public LocalSocket (int sockType)

Added in API level 19

Creates a AF_LOCAL/UNIX domain stream socket with given socket type

Parameters
sockType either SOCKET_DGRAM, SOCKET_STREAM or SOCKET_SEQPACKET

Public Methods

public void bind (LocalSocketAddress bindpoint)

Added in API level 1

Binds this socket to an endpoint name. May only be called on an instance that has not yet been bound.

Parameters
bindpoint endpoint address
Throws
IOException

public void close ()

Added in API level 1

Closes the socket.

Throws
IOException

public void connect (LocalSocketAddress endpoint, int timeout)

Added in API level 1

Throws
IOException

public void connect (LocalSocketAddress endpoint)

Added in API level 1

Connects this socket to an endpoint. May only be called on an instance that has not yet been connected.

Parameters
endpoint endpoint address
Throws
IOException if socket is in invalid state or the address does not exist.

public FileDescriptor[] getAncillaryFileDescriptors ()

Added in API level 1

Retrieves a set of file descriptors that a peer has sent through an ancillary message. This method retrieves the most recent set sent, and then returns null until a new set arrives. File descriptors may only be passed along with regular data, so this method can only return a non-null after a read operation.

Returns
  • null or file descriptor array
Throws
IOException

public FileDescriptor getFileDescriptor ()

Added in API level 1

Returns file descriptor or null if not yet open/already closed

Returns
  • fd or null

public InputStream getInputStream ()

Added in API level 1

Retrieves the input stream for this instance.

Returns
  • input stream
Throws
IOException if socket has been closed or cannot be created.

public LocalSocketAddress getLocalSocketAddress ()

Added in API level 1

Retrieves the name that this socket is bound to, if any.

Returns
  • Local address or null if anonymous

public OutputStream getOutputStream ()

Added in API level 1

Retrieves the output stream for this instance.

Returns
  • output stream
Throws
IOException if socket has been closed or cannot be created.

public Credentials getPeerCredentials ()

Added in API level 1

Retrieves the credentials of this socket's peer. Only valid on connected sockets.

Returns
  • non-null; peer credentials
Throws
IOException

public int getReceiveBufferSize ()

Added in API level 1

Throws
IOException

public LocalSocketAddress getRemoteSocketAddress ()

Added in API level 1

public int getSendBufferSize ()

Added in API level 1

Throws
IOException

public int getSoTimeout ()

Added in API level 1

Throws
IOException

public synchronized boolean isBound ()

Added in API level 1

public boolean isClosed ()

Added in API level 1

public synchronized boolean isConnected ()

Added in API level 1

public boolean isInputShutdown ()

Added in API level 1

public boolean isOutputShutdown ()

Added in API level 1

public void setFileDescriptorsForSend (FileDescriptor[] fds)

Added in API level 1

Enqueues a set of file descriptors to send to the peer. The queue is one deep. The file descriptors will be sent with the next write of normal data, and will be delivered in a single ancillary message. See "man 7 unix" SCM_RIGHTS on a desktop Linux machine.

Parameters
fds non-null; file descriptors to send.

public void setReceiveBufferSize (int size)

Added in API level 1

Throws
IOException

public void setSendBufferSize (int n)

Added in API level 1

Throws
IOException

public void setSoTimeout (int n)

Added in API level 1

Throws
IOException

public void shutdownInput ()

Added in API level 1

Shuts down the input side of the socket.

Throws
IOException

public void shutdownOutput ()

Added in API level 1

Shuts down the output side of the socket.

Throws
IOException

public String toString ()

Added in API level 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.