Package java.net
Class SocketImpl
java.lang.Object
java.net.SocketImpl
- All Implemented Interfaces:
SocketOptions
- Direct Known Subclasses:
PlainSocketImpl
public abstract class SocketImpl extends Object implements SocketOptions
This class is the base of all streaming socket implementation classes.
Streaming sockets are wrapped by two classes,
ServerSocket and
Socket at the server and client end of a connection. At the server,
there are two types of sockets engaged in communication, the
ServerSocket on a well known port (referred to as listener) used to
establish a connection and the resulting Socket (referred to as
host).-
Field Summary
Fields Modifier and Type Field Description protected InetAddressaddressThe remote address this socket is connected to.protected FileDescriptorfdThe file descriptor of this socket.protected intlocalportThe local port this socket is connected to.protected intportThe remote port this socket is connected to.Fields inherited from interface java.net.SocketOptions
IP_MULTICAST_IF, IP_MULTICAST_IF2, IP_MULTICAST_LOOP, IP_TOS, SO_BINDADDR, SO_BROADCAST, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_REUSEADDR, SO_SNDBUF, SO_TIMEOUT, TCP_NODELAY -
Constructor Summary
Constructors Constructor Description SocketImpl() -
Method Summary
Modifier and Type Method Description protected abstract voidaccept(SocketImpl newSocket)Waits for an incoming request and blocks until the connection is opened on the given socket.protected abstract intavailable()Returns the available number of bytes which are readable from this socket without blocking.protected abstract voidbind(InetAddress address, int port)Binds this socket to the specified local host address and port number.protected abstract voidclose()Closes this socket.protected abstract voidconnect(String host, int port)Connects this socket to the specified remote host and port number.protected abstract voidconnect(InetAddress address, int port)Connects this socket to the specified remote host address and port number.protected abstract voidconnect(SocketAddress remoteAddr, int timeout)Connects this socket to the remote host address and port number specified by theSocketAddressobject with the given timeout.protected abstract voidcreate(boolean isStreaming)Creates a new unconnected socket.FileDescriptorgetFD$()protected FileDescriptorgetFileDescriptor()Gets the file descriptor of this socket.protected InetAddressgetInetAddress()Gets the remote address this socket is connected to.protected abstract InputStreamgetInputStream()Gets the input stream of this socket.protected intgetLocalPort()Gets the local port number of this socket.protected abstract OutputStreamgetOutputStream()Gets the output stream of this socket.protected intgetPort()Gets the remote port number of this socket.protected abstract voidlisten(int backlog)Listens for connection requests on this streaming socket.protected abstract voidsendUrgentData(int value)Sends the single byte of urgent data on the socket.protected voidsetPerformancePreferences(int connectionTime, int latency, int bandwidth)Sets performance preference for connection time, latency and bandwidth.protected voidshutdownInput()Closes the input channel of this socket.protected voidshutdownOutput()Closes the output channel of this socket.protected booleansupportsUrgentData()Returns whether the socket supports urgent data or not.StringtoString()Returns a string containing a concise, human-readable description of the socket.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.net.SocketOptions
getOption, setOption
-
Field Details
-
address
The remote address this socket is connected to. -
port
protected int portThe remote port this socket is connected to. -
fd
The file descriptor of this socket. -
localport
protected int localportThe local port this socket is connected to.
-
-
Constructor Details
-
SocketImpl
public SocketImpl()
-
-
Method Details
-
accept
Waits for an incoming request and blocks until the connection is opened on the given socket.- Parameters:
newSocket- the socket to accept connections on.- Throws:
IOException- if an error occurs while accepting a new connection.
-
available
Returns the available number of bytes which are readable from this socket without blocking.- Returns:
- the number of bytes that may be read without blocking.
- Throws:
IOException- if an error occurs while reading the number of bytes.
-
bind
Binds this socket to the specified local host address and port number.- Parameters:
address- the local machine address to bind this socket to.port- the port on the local machine to bind this socket to.- Throws:
IOException- if an error occurs while binding this socket.
-
close
Closes this socket. This makes later access invalid.- Throws:
IOException- if an error occurs while closing this socket.
-
connect
Connects this socket to the specified remote host and port number.- Parameters:
host- the remote host this socket has to be connected to.port- the remote port on which this socket has to be connected.- Throws:
IOException- if an error occurs while connecting to the remote host.
-
connect
Connects this socket to the specified remote host address and port number.- Parameters:
address- the remote host address this socket has to be connected to.port- the remote port on which this socket has to be connected.- Throws:
IOException- if an error occurs while connecting to the remote host.
-
create
Creates a new unconnected socket. The argumentisStreamingdefines whether the new socket is a streaming or a datagram socket.- Parameters:
isStreaming- defines whether the type of the new socket is streaming or datagram.- Throws:
IOException- if an error occurs while creating the socket.
-
getFileDescriptor
Gets the file descriptor of this socket.- Returns:
- the file descriptor of this socket.
-
getFD$
-
getInetAddress
Gets the remote address this socket is connected to.- Returns:
- the remote address of this socket.
-
getInputStream
Gets the input stream of this socket.- Returns:
- the input stream of this socket.
- Throws:
IOException- if an error occurs while accessing the input stream.
-
getLocalPort
protected int getLocalPort()Gets the local port number of this socket. The field is initialized to-1and upon demand will go to the IP stack to get the bound value. See the class comment for the context of the local port.- Returns:
- the local port number this socket is bound to.
-
getOutputStream
Gets the output stream of this socket.- Returns:
- the output stream of this socket.
- Throws:
IOException- if an error occurs while accessing the output stream.
-
getPort
protected int getPort()Gets the remote port number of this socket. This value is not meaningful when this instance is wrapped by aServerSocket.- Returns:
- the remote port this socket is connected to.
-
listen
Listens for connection requests on this streaming socket. Incoming connection requests are queued up to the limit specified bybacklog. Additional requests are rejected. This method may only be invoked on stream sockets.- Parameters:
backlog- the maximum number of outstanding connection requests.- Throws:
IOException- if an error occurs while listening.
-
toString
Returns a string containing a concise, human-readable description of the socket. -
shutdownInput
Closes the input channel of this socket.This default implementation always throws an
IOExceptionto indicate that the subclass should have overridden this method.- Throws:
IOException- always because this method should be overridden.
-
shutdownOutput
Closes the output channel of this socket.This default implementation always throws an
IOExceptionto indicate that the subclass should have overridden this method.- Throws:
IOException- always because this method should be overridden.
-
connect
Connects this socket to the remote host address and port number specified by theSocketAddressobject with the given timeout. This method will block indefinitely if the timeout is set to zero.- Parameters:
remoteAddr- the remote host address and port number to connect to.timeout- the timeout value in milliseconds.- Throws:
IOException- if an error occurs while connecting.
-
supportsUrgentData
protected boolean supportsUrgentData()Returns whether the socket supports urgent data or not. Subclasses should override this method.- Returns:
falsebecause subclasses must override this method.
-
sendUrgentData
Sends the single byte of urgent data on the socket.- Parameters:
value- the byte of urgent data.- Throws:
IOException- if an error occurs sending urgent data.
-
setPerformancePreferences
protected void setPerformancePreferences(int connectionTime, int latency, int bandwidth)Sets performance preference for connection time, latency and bandwidth. Does nothing by default.- Parameters:
connectionTime- the importance of connect time.latency- the importance of latency.bandwidth- the importance of bandwidth.
-