1 /*
2 * Copyright (c) 1995, 2011, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package sun.net;
26
27 import java.io.*;
28 import java.net.Socket;
29 import java.net.ServerSocket;
30
31 /**
32 * This is the base class for network servers. To define a new type
33 * of server define a new subclass of NetworkServer with a serviceRequest
34 * method that services one request. Start the server by executing:
35 * <pre>
36 * new MyServerClass().startServer(port);
37 * </pre>
38 */
39 public class NetworkServer implements Runnable, Cloneable {
40 /** Socket for communicating with client. */
41 public Socket clientSocket = null;
42 private Thread serverInstance;
43 private ServerSocket serverSocket;
44
45 /** Stream for printing to the client. */
46 public PrintStream clientOutput;
47
48 /** Buffered stream for reading replies from client. */
49 public InputStream clientInput;
100 try {
101 close();
102 } catch(IOException e2) {}
103 }
104 }
105
106 /** Start a server on port <i>port</i>. It will call serviceRequest()
107 for each new connection. */
108 public final void startServer(int port) throws IOException {
109 serverSocket = new ServerSocket(port, 50);
110 serverInstance = new Thread(null, this, "NetworkServer", 0, false);
111 serverInstance.start();
112 }
113
114 /** Service one request. It is invoked with the clientInput and
115 clientOutput streams initialized. This method handles one client
116 connection. When it is done, it can simply exit. The default
117 server just echoes it's input. It is invoked in it's own private
118 thread. */
119 public void serviceRequest() throws IOException {
120 byte buf[] = new byte[300];
121 int n;
122 clientOutput.print("Echo server " + getClass().getName() + "\n");
123 clientOutput.flush();
124 while ((n = clientInput.read(buf, 0, buf.length)) >= 0) {
125 clientOutput.write(buf, 0, n);
126 }
127 }
128
129 public static void main(String argv[]) {
130 try {
131 new NetworkServer ().startServer(8888);
132 } catch (IOException e) {
133 System.out.print("Server failed: "+e+"\n");
134 }
135 }
136
137 /**
138 * Clone this object;
139 */
140 public Object clone() {
141 try {
142 return super.clone();
143 } catch (CloneNotSupportedException e) {
144 // this shouldn't happen, since we are Cloneable
145 throw new InternalError(e);
146 }
|
1 /*
2 * Copyright (c) 1995, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package sun.net;
26
27 import java.io.*;
28 import java.net.Socket;
29 import java.net.ServerSocket;
30 import jdk.internal.io.IOSupport;
31
32 /**
33 * This is the base class for network servers. To define a new type
34 * of server define a new subclass of NetworkServer with a serviceRequest
35 * method that services one request. Start the server by executing:
36 * <pre>
37 * new MyServerClass().startServer(port);
38 * </pre>
39 */
40 public class NetworkServer implements Runnable, Cloneable {
41 /** Socket for communicating with client. */
42 public Socket clientSocket = null;
43 private Thread serverInstance;
44 private ServerSocket serverSocket;
45
46 /** Stream for printing to the client. */
47 public PrintStream clientOutput;
48
49 /** Buffered stream for reading replies from client. */
50 public InputStream clientInput;
101 try {
102 close();
103 } catch(IOException e2) {}
104 }
105 }
106
107 /** Start a server on port <i>port</i>. It will call serviceRequest()
108 for each new connection. */
109 public final void startServer(int port) throws IOException {
110 serverSocket = new ServerSocket(port, 50);
111 serverInstance = new Thread(null, this, "NetworkServer", 0, false);
112 serverInstance.start();
113 }
114
115 /** Service one request. It is invoked with the clientInput and
116 clientOutput streams initialized. This method handles one client
117 connection. When it is done, it can simply exit. The default
118 server just echoes it's input. It is invoked in it's own private
119 thread. */
120 public void serviceRequest() throws IOException {
121 clientOutput.print("Echo server " + getClass().getName() + "\n");
122 clientOutput.flush();
123 IOSupport.copy(clientInput, clientOutput, 300);
124 }
125
126 public static void main(String argv[]) {
127 try {
128 new NetworkServer ().startServer(8888);
129 } catch (IOException e) {
130 System.out.print("Server failed: "+e+"\n");
131 }
132 }
133
134 /**
135 * Clone this object;
136 */
137 public Object clone() {
138 try {
139 return super.clone();
140 } catch (CloneNotSupportedException e) {
141 // this shouldn't happen, since we are Cloneable
142 throw new InternalError(e);
143 }
|