001 /*
002 * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.net;
027
028 import java.io.IOException;
029 import java.io.InputStream;
030 import java.io.OutputStream;
031 import java.io.FileDescriptor;
032
033 /**
034 * The abstract class <code>SocketImpl</code> is a common superclass
035 * of all classes that actually implement sockets. It is used to
036 * create both client and server sockets.
037 * <p>
038 * A "plain" socket implements these methods exactly as
039 * described, without attempting to go through a firewall or proxy.
040 *
041 * @author unascribed
042 * @version 1.51, 05/05/07
043 * @since JDK1.0
044 */
045 public abstract class SocketImpl implements SocketOptions {
046 /**
047 * The actual Socket object.
048 */
049 Socket socket = null;
050 ServerSocket serverSocket = null;
051
052 /**
053 * The file descriptor object for this socket.
054 */
055 protected FileDescriptor fd;
056
057 /**
058 * The IP address of the remote end of this socket.
059 */
060 protected InetAddress address;
061
062 /**
063 * The port number on the remote host to which this socket is connected.
064 */
065 protected int port;
066
067 /**
068 * The local port number to which this socket is connected.
069 */
070 protected int localport;
071
072 /**
073 * Creates either a stream or a datagram socket.
074 *
075 * @param stream if <code>true</code>, create a stream socket;
076 * otherwise, create a datagram socket.
077 * @exception IOException if an I/O error occurs while creating the
078 * socket.
079 */
080 protected abstract void create(boolean stream) throws IOException;
081
082 /**
083 * Connects this socket to the specified port on the named host.
084 *
085 * @param host the name of the remote host.
086 * @param port the port number.
087 * @exception IOException if an I/O error occurs when connecting to the
088 * remote host.
089 */
090 protected abstract void connect(String host, int port)
091 throws IOException;
092
093 /**
094 * Connects this socket to the specified port number on the specified host.
095 *
096 * @param address the IP address of the remote host.
097 * @param port the port number.
098 * @exception IOException if an I/O error occurs when attempting a
099 * connection.
100 */
101 protected abstract void connect(InetAddress address, int port)
102 throws IOException;
103
104 /**
105 * Connects this socket to the specified port number on the specified host.
106 * A timeout of zero is interpreted as an infinite timeout. The connection
107 * will then block until established or an error occurs.
108 *
109 * @param address the Socket address of the remote host.
110 * @param timeout the timeout value, in milliseconds, or zero for no timeout.
111 * @exception IOException if an I/O error occurs when attempting a
112 * connection.
113 * @since 1.4
114 */
115 protected abstract void connect(SocketAddress address, int timeout)
116 throws IOException;
117
118 /**
119 * Binds this socket to the specified local IP address and port number.
120 *
121 * @param host an IP address that belongs to a local interface.
122 * @param port the port number.
123 * @exception IOException if an I/O error occurs when binding this socket.
124 */
125 protected abstract void bind(InetAddress host, int port)
126 throws IOException;
127
128 /**
129 * Sets the maximum queue length for incoming connection indications
130 * (a request to connect) to the <code>count</code> argument. If a
131 * connection indication arrives when the queue is full, the
132 * connection is refused.
133 *
134 * @param backlog the maximum length of the queue.
135 * @exception IOException if an I/O error occurs when creating the queue.
136 */
137 protected abstract void listen(int backlog) throws IOException;
138
139 /**
140 * Accepts a connection.
141 *
142 * @param s the accepted connection.
143 * @exception IOException if an I/O error occurs when accepting the
144 * connection.
145 */
146 protected abstract void accept(SocketImpl s) throws IOException;
147
148 /**
149 * Returns an input stream for this socket.
150 *
151 * @return a stream for reading from this socket.
152 * @exception IOException if an I/O error occurs when creating the
153 * input stream.
154 */
155 protected abstract InputStream getInputStream() throws IOException;
156
157 /**
158 * Returns an output stream for this socket.
159 *
160 * @return an output stream for writing to this socket.
161 * @exception IOException if an I/O error occurs when creating the
162 * output stream.
163 */
164 protected abstract OutputStream getOutputStream()
165 throws IOException;
166
167 /**
168 * Returns the number of bytes that can be read from this socket
169 * without blocking.
170 *
171 * @return the number of bytes that can be read from this socket
172 * without blocking.
173 * @exception IOException if an I/O error occurs when determining the
174 * number of bytes available.
175 */
176 protected abstract int available() throws IOException;
177
178 /**
179 * Closes this socket.
180 *
181 * @exception IOException if an I/O error occurs when closing this socket.
182 */
183 protected abstract void close() throws IOException;
184
185 /**
186 * Places the input stream for this socket at "end of stream".
187 * Any data sent to this socket is acknowledged and then
188 * silently discarded.
189 *
190 * If you read from a socket input stream after invoking
191 * shutdownInput() on the socket, the stream will return EOF.
192 *
193 * @exception IOException if an I/O error occurs when shutting down this
194 * socket.
195 * @see java.net.Socket#shutdownOutput()
196 * @see java.net.Socket#close()
197 * @see java.net.Socket#setSoLinger(boolean, int)
198 * @since 1.3
199 */
200 protected void shutdownInput() throws IOException {
201 throw new IOException("Method not implemented!");
202 }
203
204 /**
205 * Disables the output stream for this socket.
206 * For a TCP socket, any previously written data will be sent
207 * followed by TCP's normal connection termination sequence.
208 *
209 * If you write to a socket output stream after invoking
210 * shutdownOutput() on the socket, the stream will throw
211 * an IOException.
212 *
213 * @exception IOException if an I/O error occurs when shutting down this
214 * socket.
215 * @see java.net.Socket#shutdownInput()
216 * @see java.net.Socket#close()
217 * @see java.net.Socket#setSoLinger(boolean, int)
218 * @since 1.3
219 */
220 protected void shutdownOutput() throws IOException {
221 throw new IOException("Method not implemented!");
222 }
223
224 /**
225 * Returns the value of this socket's <code>fd</code> field.
226 *
227 * @return the value of this socket's <code>fd</code> field.
228 * @see java.net.SocketImpl#fd
229 */
230 protected FileDescriptor getFileDescriptor() {
231 return fd;
232 }
233
234 /**
235 * Returns the value of this socket's <code>address</code> field.
236 *
237 * @return the value of this socket's <code>address</code> field.
238 * @see java.net.SocketImpl#address
239 */
240 protected InetAddress getInetAddress() {
241 return address;
242 }
243
244 /**
245 * Returns the value of this socket's <code>port</code> field.
246 *
247 * @return the value of this socket's <code>port</code> field.
248 * @see java.net.SocketImpl#port
249 */
250 protected int getPort() {
251 return port;
252 }
253
254 /**
255 * Returns whether or not this SocketImpl supports sending
256 * urgent data. By default, false is returned
257 * unless the method is overridden in a sub-class
258 *
259 * @return true if urgent data supported
260 * @see java.net.SocketImpl#address
261 * @since 1.4
262 */
263 protected boolean supportsUrgentData() {
264 return false; // must be overridden in sub-class
265 }
266
267 /**
268 * Send one byte of urgent data on the socket.
269 * The byte to be sent is the low eight bits of the parameter
270 * @param data The byte of data to send
271 * @exception IOException if there is an error
272 * sending the data.
273 * @since 1.4
274 */
275 protected abstract void sendUrgentData(int data) throws IOException;
276
277 /**
278 * Returns the value of this socket's <code>localport</code> field.
279 *
280 * @return the value of this socket's <code>localport</code> field.
281 * @see java.net.SocketImpl#localport
282 */
283 protected int getLocalPort() {
284 return localport;
285 }
286
287 void setSocket(Socket soc) {
288 this .socket = soc;
289 }
290
291 Socket getSocket() {
292 return socket;
293 }
294
295 void setServerSocket(ServerSocket soc) {
296 this .serverSocket = soc;
297 }
298
299 ServerSocket getServerSocket() {
300 return serverSocket;
301 }
302
303 /**
304 * Returns the address and port of this socket as a <code>String</code>.
305 *
306 * @return a string representation of this socket.
307 */
308 public String toString() {
309 return "Socket[addr=" + getInetAddress() + ",port=" + getPort()
310 + ",localport=" + getLocalPort() + "]";
311 }
312
313 void reset() throws IOException {
314 address = null;
315 port = 0;
316 localport = 0;
317 }
318
319 /**
320 * Sets performance preferences for this socket.
321 *
322 * <p> Sockets use the TCP/IP protocol by default. Some implementations
323 * may offer alternative protocols which have different performance
324 * characteristics than TCP/IP. This method allows the application to
325 * express its own preferences as to how these tradeoffs should be made
326 * when the implementation chooses from the available protocols.
327 *
328 * <p> Performance preferences are described by three integers
329 * whose values indicate the relative importance of short connection time,
330 * low latency, and high bandwidth. The absolute values of the integers
331 * are irrelevant; in order to choose a protocol the values are simply
332 * compared, with larger values indicating stronger preferences. Negative
333 * values represent a lower priority than positive values. If the
334 * application prefers short connection time over both low latency and high
335 * bandwidth, for example, then it could invoke this method with the values
336 * <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low
337 * latency, and low latency above short connection time, then it could
338 * invoke this method with the values <tt>(0, 1, 2)</tt>.
339 *
340 * By default, this method does nothing, unless it is overridden in a
341 * a sub-class.
342 *
343 * @param connectionTime
344 * An <tt>int</tt> expressing the relative importance of a short
345 * connection time
346 *
347 * @param latency
348 * An <tt>int</tt> expressing the relative importance of low
349 * latency
350 *
351 * @param bandwidth
352 * An <tt>int</tt> expressing the relative importance of high
353 * bandwidth
354 *
355 * @since 1.5
356 */
357 protected void setPerformancePreferences(int connectionTime,
358 int latency, int bandwidth) {
359 /* Not implemented yet */
360 }
361 }
|