Source code: com/act365/net/GeneralSocketImpl.java
1 /*
2 * JSocket Wrench
3 *
4 * Copyright (C) act365.com October 2003
5 *
6 * Web site: http://www.act365.com/wrench
7 * E-mail: developers@act365.com
8 *
9 * The JSocket Wrench library adds support for low-level Internet protocols
10 * to the Java programming language.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 * Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 package com.act365.net;
28
29 import java.io.*;
30 import java.net.*;
31
32 /**
33 * GeneralDatagramSocketImpl extends <code>java.net.SocketImpl</code>
34 * and provides native implementations of all of its abstract methods. The
35 * class remains abstract because it leaves its <code>create()</code> method
36 * undefined - it is up to subclasses to specify the parameters that will
37 * be used to create the underlying socket. The native code calls into the
38 * local Berkeley sockets implementation.
39 */
40
41 public abstract class GeneralSocketImpl extends SocketImpl {
42
43 /**
44 Creates a new unconnected TCP socket.
45 @see com.act365.net.SocketConstants
46 @param socketType the socket type as defined in <code>SocketConstants</code> e.g. SOCK_RAW
47 @param protocol the protocol as defined in <code>SocketConstants</code> e.g. IPPROTO_UDP
48 */
49
50 public void create ( int socketType , int protocol ) throws IOException {
51
52 int sd = _socket( SocketConstants.AF_INET , socketType , protocol );
53
54 fd = new FileDescriptor();
55
56 setSocketDescriptor( fd , sd );
57 }
58
59 native static int _socket( int addressFamily , int socketType , int protocol );
60
61 /**
62 Binds this socket to the local port.
63 @param inetAddress local IP address
64 @param port number of the local port
65 */
66
67 public void bind( InetAddress inetAddress , int port ) throws IOException {
68
69 _bind( getSocketDescriptor( fd ) , inetAddress.getAddress() , port );
70
71 address = createInetAddress( SocketConstants.AF_INET , inetAddress.getAddress() );
72 localport = port ;
73 }
74
75 native static int _bind( int sd , byte[] ipAddress , int port );
76
77 /**
78 Connects this socket to a named host.
79 @param hostName name of the remote host
80 @param port port number on the remote host
81 */
82
83 public void connect( String hostName , int port ) throws IOException {
84
85 connect( InetAddress.getByName( hostName ) , port );
86 }
87
88 /**
89 Connects this socket to a given destination.
90 @param dst address of the remote host
91 @param remotePort port number on the remote host
92 */
93
94 public void connect( InetAddress dst , int remotePort ) throws IOException {
95
96 int ret = _connect( getSocketDescriptor( fd ) ,
97 dst.getAddress() ,
98 remotePort );
99
100 port = remotePort ;
101 address = dst ;
102 }
103
104 /**
105 * Connects to the specified host.
106 */
107
108 public void connect( SocketAddress address , int timeout ) throws IOException {
109 throw new IOException("SocketAddress is not supported");
110 }
111
112 native static int _connect( int sd , byte[] ipAddress , int port );
113
114 /**
115 Listens for connection requests on this socket.
116 @param backlog maxmimum number of pending requests to be serviced simultaneously
117 */
118
119 public void listen( int backlog ) throws IOException {
120
121 _listen( getSocketDescriptor( fd ) , backlog );
122 }
123
124 native static int _listen( int sd , int backlog );
125
126 /**
127 Accepts a connection request for this socket.
128 @param newSocket socket to be populated with the details of the remote client
129 */
130
131 public void accept( SocketImpl newSocket ) throws IOException {
132
133 _accept( getSocketDescriptor( fd ) , newSocket );
134 }
135
136 native static int _accept( int sd , SocketImpl newSocket );
137
138 /**
139 Closes this socket.
140 */
141
142 public void close() throws IOException {
143
144 _close( getSocketDescriptor( fd ) );
145 }
146
147 native static int _close( int sd );
148
149 /**
150 Gets the socket descriptor from a <code>java.io.FileDescriptor</code> object.
151 NB Java provides no public access to the value of the descriptor so
152 it has to be extracted using native code.
153 */
154
155 public static int getSocketDescriptor( FileDescriptor fd ){
156 return _getSocketDescriptor( fd );
157 }
158
159 native static int _getSocketDescriptor( FileDescriptor fd );
160
161 /**
162 Sets the socket descriptor for a <code>java.io.FileDescriptor</code> object.
163 NB Java provides no public access to the descriptor so its value has to
164 be set using native code.
165 */
166
167 public static void setSocketDescriptor( FileDescriptor fd , int sd ){
168 _setSocketDescriptor( fd , sd );
169 }
170
171 native static void _setSocketDescriptor( FileDescriptor fd , int sd );
172
173 /**
174 Creates a <code>java.net.InetAddress</code> object with a given IP address.
175 @param family the socket family - normally SocketConstants.AF_INET
176 @param ipAddress the IP address e.g. new byte[] { 127, 0, 0, 1 }
177 */
178
179 public static InetAddress createInetAddress( int family , byte[] ipAddress ){
180 return _createInetAddress( family , ipAddress );
181 }
182
183 native static InetAddress _createInetAddress( int family , byte[] ipAddress );
184
185 /**
186 Sets the value of a socket option.The value has to be an
187 <code>Integer</code> object.
188 @param optID option as defined in <code>java.net.SocketConstants</code>
189 @param value an <code>Integer</code> object that wraps the new value
190
191 */
192
193 public void setOption( int optID , Object value ) throws SocketException {
194
195 if( _setOption( getSocketDescriptor( fd ) , optID , value ) < 0 ){
196 throw new SocketException();
197 }
198 }
199
200 static native int _setOption( int socketDescriptor , int optionName , Object newValue );
201
202 /**
203 Gets the value of a socket option. The returned value will be an
204 <code>Integer</code> object.
205 @param optID option as defined in <code>java.net.SocketConstants</code>
206 */
207
208 public Object getOption( int optID ) throws SocketException {
209
210 return _getOption( getSocketDescriptor( fd ) , optID );
211 }
212
213 static native Object _getOption( int socketDescriptor , int optionName ) throws SocketException ;
214
215 /**
216 Gets the output stream.
217 */
218
219 public OutputStream getOutputStream() throws IOException {
220 return new GeneralSocketOutputStream( getSocketDescriptor( fd ) );
221 }
222
223 /**
224 * Gets the input stream.
225 */
226
227 public InputStream getInputStream() throws IOException {
228 return new GeneralSocketInputStream( getSocketDescriptor( fd ) );
229 }
230
231 /**
232 * <code>available()</code> isn't supported.
233 */
234
235 public int available() throws IOException {
236 throw new IOException("available() not supported");
237 }
238
239 /**
240 * Urgent data isn't supported.
241 */
242
243 public void sendUrgentData( int data ) throws IOException {
244 throw new IOException("Urgent data not supported");
245 }
246 };