KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > SocketImpl


1 /*
2  * @(#)SocketImpl.java 1.42 04/03/25
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.OutputStream JavaDoc;
13 import java.io.FileDescriptor JavaDoc;
14
15 /**
16  * The abstract class <code>SocketImpl</code> is a common superclass
17  * of all classes that actually implement sockets. It is used to
18  * create both client and server sockets.
19  * <p>
20  * A "plain" socket implements these methods exactly as
21  * described, without attempting to go through a firewall or proxy.
22  *
23  * @author unascribed
24  * @version 1.42, 03/25/04
25  * @since JDK1.0
26  */

27 public abstract class SocketImpl implements SocketOptions JavaDoc {
28     /**
29      * The actual Socket object.
30      */

31     Socket JavaDoc socket = null;
32     ServerSocket JavaDoc serverSocket = null;
33
34     /**
35      * The file descriptor object for this socket.
36      */

37     protected FileDescriptor JavaDoc fd;
38     
39     /**
40      * The IP address of the remote end of this socket.
41      */

42     protected InetAddress JavaDoc address;
43    
44     /**
45      * The port number on the remote host to which this socket is connected.
46      */

47     protected int port;
48
49     /**
50      * The local port number to which this socket is connected.
51      */

52     protected int localport;
53
54     /**
55      * Creates either a stream or a datagram socket.
56      *
57      * @param stream if <code>true</code>, create a stream socket;
58      * otherwise, create a datagram socket.
59      * @exception IOException if an I/O error occurs while creating the
60      * socket.
61      */

62     protected abstract void create(boolean stream) throws IOException JavaDoc;
63
64     /**
65      * Connects this socket to the specified port on the named host.
66      *
67      * @param host the name of the remote host.
68      * @param port the port number.
69      * @exception IOException if an I/O error occurs when connecting to the
70      * remote host.
71      */

72     protected abstract void connect(String JavaDoc host, int port) throws IOException JavaDoc;
73
74     /**
75      * Connects this socket to the specified port number on the specified host.
76      *
77      * @param address the IP address of the remote host.
78      * @param port the port number.
79      * @exception IOException if an I/O error occurs when attempting a
80      * connection.
81      */

82     protected abstract void connect(InetAddress JavaDoc address, int port) throws IOException JavaDoc;
83
84     /**
85      * Connects this socket to the specified port number on the specified host.
86      * A timeout of zero is interpreted as an infinite timeout. The connection
87      * will then block until established or an error occurs.
88      *
89      * @param address the Socket address of the remote host.
90      * @param timeout the timeout value, in milliseconds, or zero for no timeout.
91      * @exception IOException if an I/O error occurs when attempting a
92      * connection.
93      * @since 1.4
94      */

95     protected abstract void connect(SocketAddress JavaDoc address, int timeout) throws IOException JavaDoc;
96
97     /**
98      * Binds this socket to the specified local IP address and port number.
99      *
100      * @param host an IP address that belongs to a local interface.
101      * @param port the port number.
102      * @exception IOException if an I/O error occurs when binding this socket.
103      */

104     protected abstract void bind(InetAddress JavaDoc host, int port) throws IOException JavaDoc;
105
106     /**
107      * Sets the maximum queue length for incoming connection indications
108      * (a request to connect) to the <code>count</code> argument. If a
109      * connection indication arrives when the queue is full, the
110      * connection is refused.
111      *
112      * @param backlog the maximum length of the queue.
113      * @exception IOException if an I/O error occurs when creating the queue.
114      */

115     protected abstract void listen(int backlog) throws IOException JavaDoc;
116
117     /**
118      * Accepts a connection.
119      *
120      * @param s the accepted connection.
121      * @exception IOException if an I/O error occurs when accepting the
122      * connection.
123      */

124     protected abstract void accept(SocketImpl JavaDoc s) throws IOException JavaDoc;
125
126     /**
127      * Returns an input stream for this socket.
128      *
129      * @return a stream for reading from this socket.
130      * @exception IOException if an I/O error occurs when creating the
131      * input stream.
132     */

133     protected abstract InputStream JavaDoc getInputStream() throws IOException JavaDoc;
134
135     /**
136      * Returns an output stream for this socket.
137      *
138      * @return an output stream for writing to this socket.
139      * @exception IOException if an I/O error occurs when creating the
140      * output stream.
141      */

142     protected abstract OutputStream JavaDoc getOutputStream() throws IOException JavaDoc;
143
144     /**
145      * Returns the number of bytes that can be read from this socket
146      * without blocking.
147      *
148      * @return the number of bytes that can be read from this socket
149      * without blocking.
150      * @exception IOException if an I/O error occurs when determining the
151      * number of bytes available.
152      */

153     protected abstract int available() throws IOException JavaDoc;
154
155     /**
156      * Closes this socket.
157      *
158      * @exception IOException if an I/O error occurs when closing this socket.
159      */

160     protected abstract void close() throws IOException JavaDoc;
161
162     /**
163      * Places the input stream for this socket at "end of stream".
164      * Any data sent to this socket is acknowledged and then
165      * silently discarded.
166      *
167      * If you read from a socket input stream after invoking
168      * shutdownInput() on the socket, the stream will return EOF.
169      *
170      * @exception IOException if an I/O error occurs when shutting down this
171      * socket.
172      * @see java.net.Socket#shutdownOutput()
173      * @see java.net.Socket#close()
174      * @see java.net.Socket#setSoLinger(boolean, int)
175      */

176     protected void shutdownInput() throws IOException JavaDoc {
177       throw new IOException JavaDoc("Method not implemented!");
178     }
179     
180     /**
181      * Disables the output stream for this socket.
182      * For a TCP socket, any previously written data will be sent
183      * followed by TCP's normal connection termination sequence.
184      *
185      * If you write to a socket output stream after invoking
186      * shutdownOutput() on the socket, the stream will throw
187      * an IOException.
188      *
189      * @exception IOException if an I/O error occurs when shutting down this
190      * socket.
191      * @see java.net.Socket#shutdownInput()
192      * @see java.net.Socket#close()
193      * @see java.net.Socket#setSoLinger(boolean, int)
194      */

195     protected void shutdownOutput() throws IOException JavaDoc {
196       throw new IOException JavaDoc("Method not implemented!");
197     }
198
199     /**
200      * Returns the value of this socket's <code>fd</code> field.
201      *
202      * @return the value of this socket's <code>fd</code> field.
203      * @see java.net.SocketImpl#fd
204      */

205     protected FileDescriptor JavaDoc getFileDescriptor() {
206     return fd;
207     }
208
209     /**
210      * Returns the value of this socket's <code>address</code> field.
211      *
212      * @return the value of this socket's <code>address</code> field.
213      * @see java.net.SocketImpl#address
214      */

215     protected InetAddress JavaDoc getInetAddress() {
216     return address;
217     }
218
219     /**
220      * Returns the value of this socket's <code>port</code> field.
221      *
222      * @return the value of this socket's <code>port</code> field.
223      * @see java.net.SocketImpl#port
224      */

225     protected int getPort() {
226     return port;
227     }
228
229     /**
230      * Returns whether or not this SocketImpl supports sending
231      * urgent data. By default, false is returned
232      * unless the method is overridden in a sub-class
233      *
234      * @return true if urgent data supported
235      * @see java.net.SocketImpl#address
236      * @since 1.4
237      */

238     protected boolean supportsUrgentData () {
239         return false; // must be overridden in sub-class
240
}
241
242     /**
243      * Send one byte of urgent data on the socket.
244      * The byte to be sent is the low eight bits of the parameter
245      * @param data The byte of data to send
246      * @exception IOException if there is an error
247      * sending the data.
248      * @since 1.4
249      */

250     protected abstract void sendUrgentData (int data) throws IOException JavaDoc;
251
252     /**
253      * Returns the value of this socket's <code>localport</code> field.
254      *
255      * @return the value of this socket's <code>localport</code> field.
256      * @see java.net.SocketImpl#localport
257      */

258     protected int getLocalPort() {
259     return localport;
260     }
261     
262     void setSocket(Socket JavaDoc soc) {
263     this.socket = soc;
264     }
265
266     Socket JavaDoc getSocket() {
267     return socket;
268     }
269
270     void setServerSocket(ServerSocket JavaDoc soc) {
271     this.serverSocket = soc;
272     }
273
274     ServerSocket JavaDoc getServerSocket() {
275     return serverSocket;
276     }
277
278     /**
279      * Returns the address and port of this socket as a <code>String</code>.
280      *
281      * @return a string representation of this socket.
282      */

283     public String JavaDoc toString() {
284     return "Socket[addr=" + getInetAddress() +
285         ",port=" + getPort() + ",localport=" + getLocalPort() + "]";
286     }
287
288     void reset() throws IOException JavaDoc {
289     address = null;
290         port = 0;
291         localport = 0;
292         close();
293     }
294
295     /**
296      * Sets performance preferences for this socket.
297      *
298      * <p> Sockets use the TCP/IP protocol by default. Some implementations
299      * may offer alternative protocols which have different performance
300      * characteristics than TCP/IP. This method allows the application to
301      * express its own preferences as to how these tradeoffs should be made
302      * when the implementation chooses from the available protocols.
303      *
304      * <p> Performance preferences are described by three integers
305      * whose values indicate the relative importance of short connection time,
306      * low latency, and high bandwidth. The absolute values of the integers
307      * are irrelevant; in order to choose a protocol the values are simply
308      * compared, with larger values indicating stronger preferences. Negative
309      * values represent a lower priority than positive values. If the
310      * application prefers short connection time over both low latency and high
311      * bandwidth, for example, then it could invoke this method with the values
312      * <tt>(1, 0, 0)</tt>. If the application prefers high bandwidth above low
313      * latency, and low latency above short connection time, then it could
314      * invoke this method with the values <tt>(0, 1, 2)</tt>.
315      *
316      * By default, this method does nothing, unless it is overridden in a
317      * a sub-class.
318      *
319      * @param connectionTime
320      * An <tt>int</tt> expressing the relative importance of a short
321      * connection time
322      *
323      * @param latency
324      * An <tt>int</tt> expressing the relative importance of low
325      * latency
326      *
327      * @param bandwidth
328      * An <tt>int</tt> expressing the relative importance of high
329      * bandwidth
330      *
331      * @since 1.5
332      */

333     protected void setPerformancePreferences(int connectionTime,
334                                           int latency,
335                                           int bandwidth)
336     {
337     /* Not implemented yet */
338     }
339 }
340
Popular Tags