File: DatagramSocket.java

package info (click to toggle)
orp-classpath 1%3A0.02.1-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 15,212 kB
  • ctags: 16,077
  • sloc: java: 82,255; ansic: 12,779; sh: 6,321; makefile: 1,478; perl: 962; exp: 122; lisp: 115
file content (461 lines) | stat: -rw-r--r-- 12,675 bytes parent folder | download
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
/* DatagramSocket.java -- A class to model UDP sockets
   Copyright (C) 1998,2000 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath 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 GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */

package java.net;

import java.io.IOException;

/**
  * This class models a connectionless datagram socket that sends 
  * individual packets of data across the network.  In the TCP/IP world,
  * this means UDP.  Datagram packets do not have guaranteed delivery,
  * or any guarantee about the order the data will be received on the
  * remote host.
  * <p>
  * 
  * @author Aaron M. Renn (arenn@urbanophile.com)
  * @author Warren Levy <warrenl@cygnus.com>
  */
public class DatagramSocket
{

/*************************************************************************/

/**
  * Instance Variables
  */

/**
  * This is the implementation object used by this socket.
  */
DatagramSocketImpl impl;

/**
  * This is the address we are "connected" to
  */
private InetAddress remote_addr;

/**
  * This is the port we are "connected" to
  */
private int remote_port = -1;

/**
  * Is this a "connected" datagram socket?
  */
private boolean connected = false;

/*************************************************************************/

/**
  * Constructors
  */

/**
  * Initializes a new instance of <code>DatagramSocket</code> that binds to 
  * a random port and every address on the local machine.
  *
  * @exception SocketException If an error occurs.
  */
public
DatagramSocket() throws SocketException
{
  this(0, null);
}

/*************************************************************************/

/**
  * Initializes a new instance of <code>DatagramSocket</code> that binds to 
  * the specified port and every address on the local machine.
  *
  * @param port The local port number to bind to
  *
  * @exception SocketException If an error occurs.
  */
public
DatagramSocket(int port) throws SocketException
{
  this(port, null);
}

/*************************************************************************/

/**
  * Initializes a new instance of <code>DatagramSocket</code> that binds to 
  * the specified local port and address.
  *
  * @param port The local port number to bind to
  * @param addr The local address to bind to
  *
  * @exception SocketException If an error occurs
  */
public
DatagramSocket(int port, InetAddress addr) throws SocketException
{
  if ((port < 0) || (port > 65535))
    throw new IllegalArgumentException("Bad port value: " + port);

  SecurityManager sm = System.getSecurityManager();
  if (sm != null)
    sm.checkListen(port);
  
  // Why is there no factory for this?
  impl = new PlainDatagramSocketImpl();

  impl.create();

  try
    {
      if (addr == null)
         addr = InetAddress.getInaddrAny();

      impl.localPort = port;
      impl.bind(port, addr);

    }
  catch (UnknownHostException e)
    {
      throw new SocketException(e.toString());
    }
}

/*************************************************************************/

/**
  * Closes this socket. 
  */
public synchronized void
close()
{
  impl.close();
}
  
/*************************************************************************/

/**
  * This method returns the remote address to which this socket is 
  * connected.  If this socket is not connected, then this method will
  * return <code>null</code>.
  *
  * @return The remote address.
  */
public InetAddress
getInetAddress()
{
  return(remote_addr);
}

/*************************************************************************/

/**
  * This method returns the remote port to which this socket is
  * connected.  If this socket is not connected, then this method will
  * return -1.
  *
  * @return The remote port
  */
public int
getPort()
{
  return(remote_port);
}

/*************************************************************************/

/**
  * Returns the local address this socket is bound to.
  */
public InetAddress
getLocalAddress()
{
  if (impl == null)
    return(null);

  InetAddress addr = null;
  try
    {
      addr = (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR);
    }
  catch(SocketException e)
    {
      return(null);
    }

  // FIXME: According to libgcj, checkConnect() is supposed to be called
  // before performing this operation.  Problems: 1) We don't have the
  // addr until after we do it, so we do a post check.  2). The docs I
  // see don't require this in the Socket case, only DatagramSocket, but
  // we'll assume they mean both.
  SecurityManager sm = System.getSecurityManager();
  if (sm != null)
    sm.checkConnect(addr.getHostName(), getLocalPort());

  return(addr);
}

/*************************************************************************/

/**
  * Returns the local port this socket is bound to
  */
public int
getLocalPort()
{
  return(impl.getLocalPort());
}

/*************************************************************************/

/**
  * Returns the value of the socket's SO_TIMEOUT setting.  If this method
  * returns 0 then SO_TIMEOUT is disabled.
  *
  * @exception SocketException If an error occurs
  */
public synchronized int
getSoTimeout() throws SocketException
{
  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 socket's SO_TIMEOUT value.  A value of 0 will
  * disable SO_TIMEOUT.  Any other value is the number of milliseconds
  * a socket read/write will block before timing out.
  *
  * @param timeout The new SO_TIMEOUT value
  *
  * @exception SocketException If an error occurs
  */
public synchronized void
setSoTimeout(int timeout) throws SocketException
{
  if (timeout < 0)
    throw new IllegalArgumentException("Timeout value is less than 0");

  impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
}

/*************************************************************************/

/**
  * This method returns the value of the system level socket option
  * SO_SNDBUF, which is used by the operating system to tune buffer
  * sizes for data transfers.
  *
  * @return The send buffer size.
  *
  * @exception SocketException If an error occurs.
  */
public synchronized int
getSendBufferSize() throws SocketException
{
  Object obj = impl.getOption(SocketOptions.SO_SNDBUF);

  if (obj instanceof Integer)
    return(((Integer)obj).intValue());
  else
    throw new SocketException("Unexpected type");
}

/*************************************************************************/

/**
  * This method sets the value for the system level socket option
  * SO_SNDBUF to the specified value.  Note that valid values for this
  * option are specific to a given operating system.
  *
  * @param size The new send buffer size.
  *
  * @exception SocketException If an error occurs.
  */
public synchronized void
setSendBufferSize(int size) throws SocketException
{
  if (size < 0)
    throw new IllegalArgumentException("Buffer size is less than 0");

  impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
}

/*************************************************************************/

/**
  * This method returns the value of the system level socket option
  * SO_RCVBUF, which is used by the operating system to tune buffer
  * sizes for data transfers.
  *
  * @return The receive buffer size.
  *
  * @exception SocketException If an error occurs.
  */
public synchronized int
getReceiveBufferSize() throws SocketException
{
  Object obj = impl.getOption(SocketOptions.SO_RCVBUF);

  if (obj instanceof Integer)
    return(((Integer)obj).intValue());
  else
    throw new SocketException("Unexpected type");
}

/*************************************************************************/

/**
  * This method sets the value for the system level socket option
  * SO_RCVBUF to the specified value.  Note that valid values for this
  * option are specific to a given operating system.
  *
  * @param size The new receive buffer size.
  *
  * @exception SocketException If an error occurs.
  */
public synchronized void
setReceiveBufferSize(int size) throws SocketException
{
  if (size < 0)
    throw new IllegalArgumentException("Buffer size is less than 0");

  impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
}

/*************************************************************************/

/**
  * This method connects this socket to the specified address and port.
  * When a datagram socket is connected, it will only send or receive
  * packate to and from the host to which it is connected.  A multicast
  * socket that is connected may only send and not receive packets.
  *
  * @param addr The address to connect this socket to.
  * @param port The port to connect this socket to.
  *
  * @exception SecurityException If connections to this addr/port are not allowed.
  * @exception IllegalArgumentException If the addr or port are invalid.
  */
public void
connect(InetAddress addr, int port) throws SecurityException, 
                                           IllegalArgumentException
{
  if (addr == null)
    throw new IllegalArgumentException("Connect address is null");

  if ((port < 1) || (port > 65535))
    throw new IllegalArgumentException("Bad port number: " + port);

  SecurityManager sm = System.getSecurityManager();
  if (sm != null)
    sm.checkConnect(addr.getHostName(), port);

  this.remote_addr = addr;
  this.remote_port = port;

  /* FIXME: Shit, we can't do this even though the OS supports it since this 
     method isn't in DatagramSocketImpl. */
//  impl.connect(addr, port);

  connected = true;
} 

/*************************************************************************/

/**
  * This method disconnects this socket from the addr/port it was 
  * connected to.  If the socket was not connected in the first place,
  * this method does nothing.
  */
public void
disconnect()
{
  // FIXME: See my comments on connect()
  this.remote_addr = null;
  this.remote_port = -1;
  connected = false;
}

/*************************************************************************/

/**
  * Reads a datagram packet from the socket.  Note that this method
  * will block until a packet is received from the network.  On return,
  * the passed in <code>DatagramPacket</code> is populated with the data 
  * received and all the other information about the packet.
  *
  * @param packet A <code>DatagramPacket</code> for storing the data
  *
  * @exception IOException If an error occurs
  */
public synchronized void
receive(DatagramPacket packet) throws IOException
{
  SecurityManager sm = System.getSecurityManager();
  if (sm != null)
    sm.checkAccept(packet.getAddress().getHostAddress(), packet.getPort());

  impl.receive(packet);
}

/*************************************************************************/

/**
  * Sends the specified packet.  The host and port to which the packet
  * are to be sent should be set inside the packet.
  *
  * @param packet The packet of data to send
  *
  * @exception IOException If an error occurs
  */
public synchronized void
send(DatagramPacket packet) throws IOException
{
  if (!connected)
    {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null)
        {
           InetAddress addr = packet.getAddress();
           if (addr.isMulticastAddress())
             sm.checkMulticast(addr);
           else
             sm.checkConnect(addr.getHostAddress(), packet.getPort());
        }
    }

  // FIXME: if this is a subclass of MulticastSocket, use getTTL for TTL val.
  impl.send(packet);
}

} // class DatagramSocket