1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/*************************************************************************
/* Socket.java -- Client socket implementation
/*
/* Copyright (c) 1998 by Aaron M. Renn (arenn@urbanophile.com)
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU Library General Public License as published
/* by the Free Software Foundation, version 2. (see COPYING.LIB)
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software Foundation
/* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
/*************************************************************************/
package java.net;
import java.io.*;
import java.security.*;
/**
* This class models a client site socket. A socket is a TCP/IP endpoint
* for network communications conceptually similar to a file handle.
* <p>
* This class does not actually do any work. Instead, it redirects all of
* its calls to a socket implementation object which implements the
* SocketImpl interface. The implementation class is instantiated by
* factory class that implements the SocketImplFactory interface. A default
* factory is provided, however the factory may be set by a call to
* the setSocketImplFactory method. Note that this may only be done once
* per virtual machine. If a subsequent attempt is made to set the
* factory, a SocketException will be thrown.
*
* @version
*
* @author Aaron M. Renn (arenn@urbanophile.com)
*/
public class Socket
{
/*************************************************************************/
/*
* Class Variables
*/
/**
* This is the user SocketImplFactory for this class. If this variable is
* null, a default factory is used.
*/
protected static SocketImplFactory factory;
/*************************************************************************/
/*
* Instance Variables
*/
/**
* The implementation object to which calls are redirected
*/
protected SocketImpl impl;
/**
* The local address to which we are connected
*/
InetAddress local_addr;
/*************************************************************************/
/*
* Class Methods
*/
/**
* Sets the SocketImplFactory. This may be done only once per virtual
* machine. Subsequent attempts will generate a SocketException. Note that
* a SecurityManager check is made prior to setting the factory. If
* insufficient privileges exist to set the factory, then an IOException
* will be thrown.
*
* @exception SocketException If the SocketImplFactory is already defined
* @exception IOException If any other error occurs
*/
public static synchronized void
setSocketImplFactory(SocketImplFactory factory) throws IOException
{
// See if already set
if (Socket.factory != null)
throw new SocketException("SocketImplFactory already defined");
// Check permissions
SecurityManager sm = System.getSecurityManager();
if (sm != null)
{
try
{
sm.checkSetFactory();
}
// catch (AccessControlException e)
catch (SecurityException e)
{
throw new IOException(e.toString());
}
}
Socket.factory = factory;
}
/*************************************************************************/
/*
* Constructors
*/
/**
* This creates a Socket object without connecting to a remote host. This
* useful for subclasses of socket that might want this behavior.
*/
protected
Socket()
{
if (factory != null)
impl = factory.createSocketImpl();
else
impl = new PlainSocketImpl();
}
/*************************************************************************/
/**
* This creates a Socket object without connecting to a remote host. This
* is useful for subclasses of socket that might want this behavior.
* Additionally, this socket will be created using the supplied implementation
* class instead the default class or one returned by a factory. This
* value can be null, but if it is, all instance methods in Socket should
* be overridden because most of them rely on this value being populated.
*
* @param impl The SocketImpl to use for this Socket
*
* @exception SocketException If an error occurs
*/
protected
Socket(SocketImpl impl) throws SocketException
{
this.impl = impl;
}
/*************************************************************************/
/**
* Creates a Socket and connects to the address and port number specified
* as arguments.
*
* @param add The address to connect to
* @param port The port number to connect to
*
* @exception IOException If an error occurs
*/
public
Socket(InetAddress addr, int port) throws IOException
{
this(addr, port, InetAddress.getInaddrAny(), 0, true);
}
/*************************************************************************/
/**
* Creates a Socket and connects to the address and port number specified
* as arguments. If the stream param is true, a stream socket will be
* created, otherwise a datagram socket is created.
* <p>
* <b>This method is deprecated. Use the DatagramSocket class for creating
* datagram sockets.</b>
*
* @param add The address to connect to
* @param port The port number to connect to
* @param stream True to create a stream socket, false to create a datagram socket
*
* @exception IOException If an error occurs
*
* @deprecated
*/
public
Socket(InetAddress addr, int port, boolean stream) throws IOException
{
this(addr, port, InetAddress.getInaddrAny(), 0, stream);
}
/*************************************************************************/
/**
* Creates a Socket and connects to the address and port number specified
* as arguments, plus binds to the specified local address and port.
*
* @param raddr The remote address to connect to
* @param rport The remote port to connect to
* @param laddr The local address to connect to
* @param lport The local port to connect to
*
* @exception IOException If an error occurs
*/
public
Socket(InetAddress raddr, int rport, InetAddress laddr,
int lport) throws IOException
{
this(raddr, rport, laddr, lport, true);
}
/*************************************************************************/
/**
* Creates a Socket and connects to the hostname and port specified as
* arguments.
*
* @param hostname The name of the host to connect to
* @param port The port number to connect to
*
* @exception UnknownHostException If the hostname cannot be resolved to an address
* @exception IOException If an error occurs
*/
public
Socket(String hostname, int port) throws IOException
{
this(InetAddress.getByName(hostname), port, InetAddress.getInaddrAny(),
0, true);
}
/*************************************************************************/
/**
* Creates a Socket and connects to the hostname and port specified as
* arguments. If the stream argument is set to true, then a stream socket
* is created. If it is false, a datagram socket is created.
* <p>
* <b>This method is deprecated. Use the DatagramSocket class to create
* datagram oriented sockets.</b>
*
* @param hostname The name of the host to connect to
* @param port The port to connect to
* @param stream true for a stream socket, false for a datagram socket
*
* @exception IOException If an error occurs
*/
public
Socket(String hostname, int port, boolean stream) throws IOException
{
this(InetAddress.getByName(hostname), port, InetAddress.getInaddrAny(),
0, stream);
}
/*************************************************************************/
/**
* This constructor is where the real work takes place. Connect to the
* specified address and port. Use default local values if not specified,
* otherwise use the local host and port passed in. Create as stream or
* datagram based on "stream" argument.
* <p>
* ******* Check security *************
*
* @param raddr The remote address to connect to
* @param rport The remote port to connect to
* @param laddr The local address to connect to
* @param lport The local port to connect to
* @param stream true for a stream socket, false for a datagram socket
*
* @exception IOException If an error occurs
*/
private
Socket(InetAddress raddr, int rport, InetAddress laddr, int lport,
boolean stream) throws IOException
{
this();
if (impl == null)
throw new IOException("Cannot initialize Socket implementation");
impl.create(stream);
if (laddr != null)
impl.bind(laddr, lport);
local_addr = laddr;
if (raddr != null)
impl.connect(raddr, rport);
}
/*************************************************************************/
/*
* Instance Methods
*/
/**
* Closes the socket.
*
* @exception IOException If an error occurs
*/
public synchronized void
close() throws IOException
{
if (impl != null)
impl.close();
}
/*************************************************************************/
/**
* Returns the address of the remote end of the socket. If this socket
* is not connected, then ???
*
* @return The remote address this socket is connected to
*/
public InetAddress
getInetAddress()
{
if (impl != null)
return(impl.getInetAddress());
return(null);
}
/*************************************************************************/
/**
* Returns the port number of the remote end of the socket connection. If
* this socket is not connected, then ???
*
* @return The remote port this socket is connected to
*/
public int
getPort()
{
if (impl != null)
return(impl.getPort());
return(-1);
}
/*************************************************************************/
/**
* Returns the local address to which this socket is bound. If this socket
* is not connected, then ???
*
* @return The local address
*/
public InetAddress
getLocalAddress()
{
return(local_addr);
}
/*************************************************************************/
/**
* Returns the local port number to which this socket is bound. If this
* socket is not connected, then ???
*
* @return The local port
*/
public int
getLocalPort()
{
if (impl != null)
return(impl.getLocalPort());
return(-1);
}
/*************************************************************************/
/**
* Returns an InputStream for reading from this socket.
*
* @return The InputStream object
*
* @exception IOException If an error occurs
*/
public synchronized InputStream
getInputStream() throws IOException
{
if (impl != null)
return(impl.getInputStream());
throw new IOException("Not connected");
}
/*************************************************************************/
/**
* Returns an OutputStream for writing to this socket.
*
* @return The OutputStream object
*
* @exception IOException If an error occurs
*/
public synchronized OutputStream
getOutputStream() throws IOException
{
if (impl != null)
return(impl.getOutputStream());
throw new IOException("Not connected");
}
/*************************************************************************/
/**
* Returns the value of the SO_LINGER option on the socket. If the
* SO_LINGER option is set on a socket and there is still data waiting to
* be sent when the socket is closed, then the close operation will block
* until either that data is delivered or until the timeout period
* expires. This method either returns the timeouts (in hundredths of
* of a second (****????****)) if SO_LINGER is set, or -1 if SO_LINGER is
* not set.
*
* @return The SO_LINGER timeout in hundreths of a second or -1 if SO_LINGER not set
*
* @exception SocketException If an error occurs
*/
public synchronized int
getSoLinger() throws SocketException
{
if (impl == null)
throw new SocketException("Not connected");
Object obj = impl.getOption(SocketOptions.SO_LINGER);
if (obj instanceof Boolean)
return(-1); // Boolean is only returned in unset case
if (obj instanceof Integer)
return(((Integer)obj).intValue());
else
throw new SocketException("Internal Error");
}
/*************************************************************************/
/**
* Sets the value of the SO_LINGER option on the socket. If the
* SO_LINGER option is set on a socket and there is still data waiting to
* be sent when the socket is closed, then the close operation will block
* until either that data is delivered or until the timeout period
* expires. The linger interval is specified in hundreths of a second
* *********???????********
*
* @param state true to enable SO_LINGER, false to disable
* @param timeout The SO_LINGER timeout in hundreths of a second or -1 if SO_LINGER not set
*
* @exception SocketException If an error occurs
*/
public synchronized void
setSoLinger(boolean state, int timeout) throws SocketException
{
if (impl == null)
throw new SocketException("No socket created");
if (state == true)
impl.setOption(SocketOptions.SO_LINGER, new Integer(timeout));
else
impl.setOption(SocketOptions.SO_LINGER, new Boolean(false));
return;
}
/*************************************************************************/
/**
* Returns the value of the SO_TIMEOUT option on the socket. If this value
* is set, and an read/write is performed that does not complete within
* the timeout period, a short count is returned (or an EWOULDBLOCK signal
* would be sent in Unix if no data had been read). A value of 0 for
* this option implies that there is no timeout (ie, operations will
* block forever). On systems that have separate read and write timeout
* values, this method returns the read timeout. This
* value is in thousandths of a second. (*****Is it *******);
*
* @return The length of the timeout in thousandth's of a second or 0 if not set
*
* @exception SocketException If an error occurs
*/
public synchronized int
getSoTimeout() throws SocketException
{
if (impl == null)
throw new SocketException("Not connected");
Object obj = impl.getOption(SocketOptions.SO_TIMEOUT);
if (obj instanceof Integer)
return(((Integer)obj).intValue());
else
throw new SocketException("Internal Error");
}
/*************************************************************************/
/**
* Sets the value of the SO_TIMEOUT option on the socket. If this value
* is set, and an read/write is performed that does not complete within
* the timeout period, a short count is returned (or an EWOULDBLOCK signal
* would be sent in Unix if no data had been read). A value of 0 for
* this option implies that there is no timeout (ie, operations will
* block forever). On systems that have separate read and write timeout
* values, this method returns the read timeout. This
* value is in thousandths of a second (****????*****)
*
* @param timeout The length of the timeout in thousandth's of a second or 0 if not set
*
* @exception SocketException If an error occurs
*/
public synchronized void
setSoTimeout(int timeout) throws SocketException
{
if (impl == null)
throw new SocketException("Not connected");
impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
return;
}
/*************************************************************************/
/**
* Tests whether or not the TCP_NODELAY option is set on the socket.
* Returns true if enabled, false if disabled. *** Need good explanation
* of this parameter.
*
* @return Whether or not TCP_NODELAY is set
*
* @exception SocketException If an error occurs
*/
public synchronized boolean
getTcpNoDelay() throws SocketException
{
if (impl == null)
throw new SocketException("Not connected");
Object obj = impl.getOption(SocketOptions.TCP_NODELAY);
if (obj instanceof Boolean)
return(((Boolean)obj).booleanValue());
else
throw new SocketException("Internal Error");
}
/*************************************************************************/
/**
* Sets the TCP_NODELAY option is set on the socket.
* Returns true if enabled, false if disabled. *** Need good explanation
* of this parameter.
*
* @param state true to enable, false to disable
*
* @exception SocketException If an error occurs
*/
public synchronized void
setTcpNoDelay(boolean state) throws SocketException
{
if (impl == null)
throw new SocketException("Not connected");
impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(state));
return;
}
/*************************************************************************/
/**
* Converts this Socket to a String. Overrides Object.toString()
*
* @return The String representation of this Socket
*/
public String
toString()
{
return(getInetAddress().getHostName() + ":" + getPort());
}
} // class Socket
|