1 /*
2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
38 * VM to run the EchoService each time that a client connects to
39 * the TCP port. The EchoService simply echos any messages it
40 * receives from the client and shuts if the client closes the
41 * connection.
42 *
43 * If configured as a "tcp wait" service then inetd will launch a VM
44 * to run the EchoService when a client connects to the port. When
45 * launched the EchoService takes over the listener socket. It
46 * terminates when all clients have disconnected and the service
47 * is idle for a few seconds.
48 *
49 * If configured as a "udp wait" service then a VM will be launched for
50 * each UDP packet to the configured port. System.inheritedChannel()
51 * will return a DatagramChannel. The echo service here will terminate after
52 * echoing the UDP packet back to the client.
53 *
54 * The service closes the inherited network channel when complete. To
55 * facilate testing that the channel is closed the "tcp nowait" service
56 * can close the connection after a given number of bytes.
57 */
58 import java.nio.*;
59 import java.nio.channels.*;
60 import java.io.IOException;
61 import java.net.*;
62
63 public class EchoService {
64
65 private static void doIt(SocketChannel sc, int closeAfter, int delay) throws IOException {
66 ByteBuffer bb = ByteBuffer.allocate(1024);
67 int total = 0;
68 for (;;) {
69 bb.clear();
70 int n = sc.read(bb);
71 if (n < 0) {
72 break;
73 }
74 total += n;
75
76 // echo
77 bb.flip();
78 sc.write(bb);
79
80 // close after X bytes?
81 if (closeAfter > 0 && total >= closeAfter) {
|
1 /*
2 * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
38 * VM to run the EchoService each time that a client connects to
39 * the TCP port. The EchoService simply echos any messages it
40 * receives from the client and shuts if the client closes the
41 * connection.
42 *
43 * If configured as a "tcp wait" service then inetd will launch a VM
44 * to run the EchoService when a client connects to the port. When
45 * launched the EchoService takes over the listener socket. It
46 * terminates when all clients have disconnected and the service
47 * is idle for a few seconds.
48 *
49 * If configured as a "udp wait" service then a VM will be launched for
50 * each UDP packet to the configured port. System.inheritedChannel()
51 * will return a DatagramChannel. The echo service here will terminate after
52 * echoing the UDP packet back to the client.
53 *
54 * The service closes the inherited network channel when complete. To
55 * facilate testing that the channel is closed the "tcp nowait" service
56 * can close the connection after a given number of bytes.
57 */
58 import java.io.IOException;
59 import java.net.SocketAddress;
60 import java.nio.ByteBuffer;
61 import java.nio.channels.Channel;
62 import java.nio.channels.DatagramChannel;
63 import java.nio.channels.SelectionKey;
64 import java.nio.channels.Selector;
65 import java.nio.channels.ServerSocketChannel;
66 import java.nio.channels.SocketChannel;
67
68 public class EchoService {
69
70 private static void doIt(SocketChannel sc, int closeAfter, int delay) throws IOException {
71 ByteBuffer bb = ByteBuffer.allocate(1024);
72 int total = 0;
73 for (;;) {
74 bb.clear();
75 int n = sc.read(bb);
76 if (n < 0) {
77 break;
78 }
79 total += n;
80
81 // echo
82 bb.flip();
83 sc.write(bb);
84
85 // close after X bytes?
86 if (closeAfter > 0 && total >= closeAfter) {
|