001: /*
002: * @(#)SocketImpl.java 1.37 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.net;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.OutputStream;
033: import java.io.FileDescriptor;
034:
035: /**
036: * The abstract class <code>SocketImpl</code> is a common superclass
037: * of all classes that actually implement sockets. It is used to
038: * create both client and server sockets.
039: * <p>
040: * A "plain" socket implements these methods exactly as
041: * described, without attempting to go through a firewall or proxy.
042: *
043: * @author unascribed
044: * @version 1.31 10/17/00
045: * @since JDK1.0
046: */
047: public abstract class SocketImpl implements SocketOptions {
048: /**
049: * The actual Socket object.
050: */
051: Socket socket = null;
052: ServerSocket serverSocket = null;
053:
054: /**
055: * The file descriptor object for this socket.
056: */
057: protected FileDescriptor fd;
058:
059: /**
060: * The IP address of the remote end of this socket.
061: */
062: protected InetAddress address;
063:
064: /**
065: * The port number on the remote host to which this socket is connected.
066: */
067: protected int port;
068:
069: /**
070: * The local port number to which this socket is connected.
071: */
072: protected int localport;
073:
074: /**
075: * Creates either a stream or a datagram socket.
076: *
077: * @param stream if <code>true</code>, create a stream socket;
078: * otherwise, create a datagram socket.
079: * @exception IOException if an I/O error occurs while creating the
080: * socket.
081: */
082: protected abstract void create(boolean stream) throws IOException;
083:
084: /**
085: * Connects this socket to the specified port on the named host.
086: *
087: * @param host the name of the remote host.
088: * @param port the port number.
089: * @exception IOException if an I/O error occurs when connecting to the
090: * remote host.
091: */
092: protected abstract void connect(String host, int port)
093: throws IOException;
094:
095: /**
096: * Connects this socket to the specified port number on the specified host.
097: *
098: * @param address the IP address of the remote host.
099: * @param port the port number.
100: * @exception IOException if an I/O error occurs when attempting a
101: * connection.
102: */
103: protected abstract void connect(InetAddress address, int port)
104: throws IOException;
105:
106: /**
107: * Connects this socket to the specified port number on the specified host.
108: * A timeout of zero is interpreted as an infinite timeout. The connection
109: * will then block until established or an error occurs.
110: *
111: * @param address the Socket address of the remote host.
112: * @param timeout the timeout value, in milliseconds, or zero for no timeout.
113: * @exception IOException if an I/O error occurs when attempting a
114: * connection.
115: * @since 1.4
116: */
117: protected abstract void connect(SocketAddress address, int timeout)
118: throws IOException;
119:
120: /**
121: * Binds this socket to the specified port number on the specified host.
122: *
123: * @param host the IP address of the remote host.
124: * @param port the port number.
125: * @exception IOException if an I/O error occurs when binding this socket.
126: */
127: protected abstract void bind(InetAddress host, int port)
128: throws IOException;
129:
130: /**
131: * Sets the maximum queue length for incoming connection indications
132: * (a request to connect) to the <code>count</code> argument. If a
133: * connection indication arrives when the queue is full, the
134: * connection is refused.
135: *
136: * @param backlog the maximum length of the queue.
137: * @exception IOException if an I/O error occurs when creating the queue.
138: */
139: protected abstract void listen(int backlog) throws IOException;
140:
141: /**
142: * Accepts a connection.
143: *
144: * @param s the accepted connection.
145: * @exception IOException if an I/O error occurs when accepting the
146: * connection.
147: */
148: protected abstract void accept(SocketImpl s) throws IOException;
149:
150: /**
151: * Returns an input stream for this socket.
152: *
153: * @return a stream for reading from this socket.
154: * @exception IOException if an I/O error occurs when creating the
155: * input stream.
156: */
157: protected abstract InputStream getInputStream() throws IOException;
158:
159: /**
160: * Returns an output stream for this socket.
161: *
162: * @return an output stream for writing to this socket.
163: * @exception IOException if an I/O error occurs when creating the
164: * output stream.
165: */
166: protected abstract OutputStream getOutputStream()
167: throws IOException;
168:
169: /**
170: * Returns the number of bytes that can be read from this socket
171: * without blocking.
172: *
173: * @return the number of bytes that can be read from this socket
174: * without blocking.
175: * @exception IOException if an I/O error occurs when determining the
176: * number of bytes available.
177: */
178: protected abstract int available() throws IOException;
179:
180: /**
181: * Closes this socket.
182: *
183: * @exception IOException if an I/O error occurs when closing this socket.
184: */
185: protected abstract void close() throws IOException;
186:
187: /**
188: * Places the input stream for this socket at "end of stream".
189: * Any data sent to this socket is acknowledged and then
190: * silently discarded.
191: *
192: * If you read from a socket input stream after invoking
193: * shutdownInput() on the socket, the stream will return EOF.
194: *
195: * @exception IOException if an I/O error occurs when shutting down this
196: * socket.
197: * @see java.net.Socket#shutdownOutput()
198: * @see java.net.Socket#close()
199: * @see java.net.Socket#setSoLinger(boolean, int)
200: */
201: protected void shutdownInput() throws IOException {
202: throw new IOException("Method not implemented!");
203: }
204:
205: /**
206: * Disables the output stream for this socket.
207: * For a TCP socket, any previously written data will be sent
208: * followed by TCP's normal connection termination sequence.
209: *
210: * If you write to a socket output stream after invoking
211: * shutdownOutput() on the socket, the stream will throw
212: * an IOException.
213: *
214: * @exception IOException if an I/O error occurs when shutting down this
215: * socket.
216: * @see java.net.Socket#shutdownInput()
217: * @see java.net.Socket#close()
218: * @see java.net.Socket#setSoLinger(boolean, int)
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: close();
318: }
319: }
|