Package cx.ath.matthew.unix
Class UnixSocket
- java.lang.Object
-
- cx.ath.matthew.unix.UnixSocket
-
public class UnixSocket extends java.lang.Object
Represents a UnixSocket.
-
-
Constructor Summary
Constructors Constructor Description UnixSocket()
Create an unconnected socket.UnixSocket(UnixSocketAddress address)
Create a socket connected to the given address.UnixSocket(java.lang.String address)
Create a socket connected to the given address.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the connection.void
connect(UnixSocketAddress address)
Connect the socket to this address.void
connect(java.lang.String address)
Connect the socket to this address.void
finalize()
UnixSocketAddress
getAddress()
Returns the address this socket is connected to.boolean
getBlocking()
Get the blocking mode.java.io.InputStream
getInputStream()
Returns an InputStream for reading from the socket.java.io.OutputStream
getOutputStream()
Returns an OutputStream for writing to the socket.boolean
getPassCred()
Get the credential passing status.int
getPeerGID()
Return the gid of the remote process.int
getPeerPID()
Return the pid of the remote process.int
getPeerUID()
Return the uid of the remote process.boolean
isClosed()
Check the socket status.boolean
isConnected()
Check the socket status.boolean
isInputShutdown()
Check the socket status.boolean
isOutputShutdown()
Check the socket status.byte
recvCredentialByte()
Receive a single byte of data, with credentials.void
sendCredentialByte(byte data)
Send a single byte of data with credentials.void
setBlocking(boolean enable)
Set the blocking mode.void
setPassCred(boolean enable)
Set the credential passing status.void
setSoTimeout(int timeout)
Set timeout of read requests.void
shutdownInput()
Shuts down the input stream.void
shutdownOutput()
Shuts down the output stream.
-
-
-
Constructor Detail
-
UnixSocket
public UnixSocket()
Create an unconnected socket.
-
UnixSocket
public UnixSocket(UnixSocketAddress address) throws java.io.IOException
Create a socket connected to the given address.- Parameters:
address
- The Unix Socket address to connect to- Throws:
java.io.IOException
-
UnixSocket
public UnixSocket(java.lang.String address) throws java.io.IOException
Create a socket connected to the given address.- Parameters:
address
- The Unix Socket address to connect to- Throws:
java.io.IOException
-
-
Method Detail
-
connect
public void connect(UnixSocketAddress address) throws java.io.IOException
Connect the socket to this address.- Parameters:
address
- The Unix Socket address to connect to- Throws:
java.io.IOException
-
connect
public void connect(java.lang.String address) throws java.io.IOException
Connect the socket to this address.- Parameters:
address
- The Unix Socket address to connect to- Throws:
java.io.IOException
-
finalize
public void finalize()
- Overrides:
finalize
in classjava.lang.Object
-
close
public void close() throws java.io.IOException
Closes the connection.- Throws:
java.io.IOException
-
getInputStream
public java.io.InputStream getInputStream()
Returns an InputStream for reading from the socket.- Returns:
- An InputStream connected to this socket.
-
getOutputStream
public java.io.OutputStream getOutputStream()
Returns an OutputStream for writing to the socket.- Returns:
- An OutputStream connected to this socket.
-
getAddress
public UnixSocketAddress getAddress()
Returns the address this socket is connected to. Returns null if the socket is unconnected.- Returns:
- The UnixSocketAddress the socket is connected to
-
sendCredentialByte
public void sendCredentialByte(byte data) throws java.io.IOException
Send a single byte of data with credentials. (Works on BSDs)- Parameters:
data
- The byte of data to send.- Throws:
java.io.IOException
-
recvCredentialByte
public byte recvCredentialByte() throws java.io.IOException
Receive a single byte of data, with credentials. (Works on BSDs)- Parameters:
data
- The byte of data to send.- Throws:
java.io.IOException
- See Also:
getPeerUID()
,getPeerPID()
,getPeerGID()
-
getPassCred
public boolean getPassCred()
Get the credential passing status. (only effective on linux)- Returns:
- The current status of credential passing.
- See Also:
setPassCred(boolean)
-
getPeerUID
public int getPeerUID()
Return the uid of the remote process. Some data must have been received on the socket to do this. Either setPassCred must be called on Linux first, or recvCredentialByte on BSD.- Returns:
- the UID or -1 if it is not available
-
getPeerGID
public int getPeerGID()
Return the gid of the remote process. Some data must have been received on the socket to do this. Either setPassCred must be called on Linux first, or recvCredentialByte on BSD.- Returns:
- the GID or -1 if it is not available
-
getPeerPID
public int getPeerPID()
Return the pid of the remote process. Some data must have been received on the socket to do this. Either setPassCred must be called on Linux first, or recvCredentialByte on BSD.- Returns:
- the PID or -1 if it is not available
-
setPassCred
public void setPassCred(boolean enable) throws java.io.IOException
Set the credential passing status. (Only does anything on linux, for other OS, you need to use send/recv credentials)- Parameters:
enable
- Set to true for credentials to be passed.- Throws:
java.io.IOException
-
getBlocking
public boolean getBlocking()
Get the blocking mode.- Returns:
- true if reads are blocking.
- See Also:
setBlocking(boolean)
-
setBlocking
public void setBlocking(boolean enable)
Set the blocking mode.- Parameters:
enable
- Set to false for non-blocking reads.
-
isClosed
public boolean isClosed()
Check the socket status.- Returns:
- true if closed.
-
isConnected
public boolean isConnected()
Check the socket status.- Returns:
- true if connected.
-
isInputShutdown
public boolean isInputShutdown()
Check the socket status.- Returns:
- true if the input stream has been shutdown
-
isOutputShutdown
public boolean isOutputShutdown()
Check the socket status.- Returns:
- true if the output stream has been shutdown
-
shutdownInput
public void shutdownInput()
Shuts down the input stream. Subsequent reads on the associated InputStream will fail.
-
shutdownOutput
public void shutdownOutput()
Shuts down the output stream. Subsequent writes to the associated OutputStream will fail.
-
setSoTimeout
public void setSoTimeout(int timeout)
Set timeout of read requests.
-
-