LocalSocket

public class LocalSocket
extends Object implements Closeable

java.lang.Object
   ↳ android.net.LocalSocket


Creates a (non-server) socket in the UNIX-domain namespace. The interface here is not entirely unlike that of java.net.Socket. This class and the streams returned from it may be used from multiple threads.

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()
boolean isBound()
boolean isClosed()
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 representation of the object.

Inherited methods

Constants

SOCKET_DGRAM

Added in API level 19
public static final int SOCKET_DGRAM

Datagram socket type

Constant Value: 1 (0x00000001)

SOCKET_SEQPACKET

Added in API level 19
public static final int SOCKET_SEQPACKET

Sequential packet socket type

Constant Value: 3 (0x00000003)

SOCKET_STREAM

Added in API level 19
public static final int SOCKET_STREAM

Stream socket type

Constant Value: 2 (0x00000002)

Public constructors

LocalSocket

Added in API level 1
public LocalSocket ()

Creates a AF_LOCAL/UNIX domain stream socket.

LocalSocket

Added in API level 1
public LocalSocket (int sockType)

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

Parameters
sockType int: either SOCKET_DGRAM, SOCKET_STREAM or SOCKET_SEQPACKET

Public methods

bind

Added in API level 1
public void bind (LocalSocketAddress bindpoint)

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

Parameters
bindpoint LocalSocketAddress: endpoint address

Throws
java.io.IOException
IOException

close

Added in API level 1
public void close ()

Closes the socket.

Throws
java.io.IOException
IOException

connect

Added in API level 1
public void connect (LocalSocketAddress endpoint, 
                int timeout)

Parameters
endpoint LocalSocketAddress

timeout int

Throws
IOException

connect

Added in API level 1
public void connect (LocalSocketAddress endpoint)

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

Parameters
endpoint LocalSocketAddress: endpoint address

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

getAncillaryFileDescriptors

Added in API level 1
public FileDescriptor[] getAncillaryFileDescriptors ()

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
FileDescriptor[] null or file descriptor array

Throws
java.io.IOException
IOException

getFileDescriptor

Added in API level 1
public FileDescriptor getFileDescriptor ()

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

Returns
FileDescriptor fd or null

getInputStream

Added in API level 1
public InputStream getInputStream ()

Retrieves the input stream for this instance. Closing this stream is equivalent to closing the entire socket and its associated streams using close().

Returns
InputStream input stream

Throws
IOException if socket has been closed or cannot be created.

getLocalSocketAddress

Added in API level 1
public LocalSocketAddress getLocalSocketAddress ()

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

Returns
LocalSocketAddress Local address or null if anonymous

getOutputStream

Added in API level 1
public OutputStream getOutputStream ()

Retrieves the output stream for this instance. Closing this stream is equivalent to closing the entire socket and its associated streams using close().

Returns
OutputStream output stream

Throws
IOException if socket has been closed or cannot be created.

getPeerCredentials

Added in API level 1
public Credentials getPeerCredentials ()

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

Returns
Credentials non-null; peer credentials

Throws
java.io.IOException
IOException

getReceiveBufferSize

Added in API level 1
public int getReceiveBufferSize ()

Returns
int

Throws
IOException

getRemoteSocketAddress

Added in API level 1
public LocalSocketAddress getRemoteSocketAddress ()

Returns
LocalSocketAddress

getSendBufferSize

Added in API level 1
public int getSendBufferSize ()

Returns
int

Throws
IOException

getSoTimeout

Added in API level 1
public int getSoTimeout ()

Returns
int

Throws
IOException

isBound

Added in API level 1
public boolean isBound ()

Returns
boolean

isClosed

Added in API level 1
public boolean isClosed ()

Returns
boolean

isConnected

Added in API level 1
public boolean isConnected ()

Returns
boolean

isInputShutdown

Added in API level 1
public boolean isInputShutdown ()

Returns
boolean

isOutputShutdown

Added in API level 1
public boolean isOutputShutdown ()

Returns
boolean

setFileDescriptorsForSend

Added in API level 1
public void setFileDescriptorsForSend (FileDescriptor[] fds)

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 FileDescriptor: non-null; file descriptors to send.

setReceiveBufferSize

Added in API level 1
public void setReceiveBufferSize (int size)

Parameters
size int

Throws
IOException

setSendBufferSize

Added in API level 1
public void setSendBufferSize (int n)

Parameters
n int

Throws
IOException

setSoTimeout

Added in API level 1
public void setSoTimeout (int n)

Parameters
n int

Throws
IOException

shutdownInput

Added in API level 1
public void shutdownInput ()

Shuts down the input side of the socket.

Throws
java.io.IOException
IOException

shutdownOutput

Added in API level 1
public void shutdownOutput ()

Shuts down the output side of the socket.

Throws
java.io.IOException
IOException

toString

Added in API level 1
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.