Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/NetworkServer.java
38829 views
/*1* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.net;2526import java.io.*;27import java.net.Socket;28import java.net.ServerSocket;2930/**31* This is the base class for network servers. To define a new type32* of server define a new subclass of NetworkServer with a serviceRequest33* method that services one request. Start the server by executing:34* <pre>35* new MyServerClass().startServer(port);36* </pre>37*/38public class NetworkServer implements Runnable, Cloneable {39/** Socket for communicating with client. */40public Socket clientSocket = null;41private Thread serverInstance;42private ServerSocket serverSocket;4344/** Stream for printing to the client. */45public PrintStream clientOutput;4647/** Buffered stream for reading replies from client. */48public InputStream clientInput;4950/** Close an open connection to the client. */51public void close() throws IOException {52clientSocket.close();53clientSocket = null;54clientInput = null;55clientOutput = null;56}5758/** Return client connection status */59public boolean clientIsOpen() {60return clientSocket != null;61}6263final public void run() {64if (serverSocket != null) {65Thread.currentThread().setPriority(Thread.MAX_PRIORITY);66// System.out.print("Server starts " + serverSocket + "\n");67while (true) {68try {69Socket ns = serverSocket.accept();70// System.out.print("New connection " + ns + "\n");71NetworkServer n = (NetworkServer)clone();72n.serverSocket = null;73n.clientSocket = ns;74new Thread(n).start();75} catch(Exception e) {76System.out.print("Server failure\n");77e.printStackTrace();78try {79serverSocket.close();80} catch(IOException e2) {}81System.out.print("cs="+serverSocket+"\n");82break;83}84}85// close();86} else {87try {88clientOutput = new PrintStream(89new BufferedOutputStream(clientSocket.getOutputStream()),90false, "ISO8859_1");91clientInput = new BufferedInputStream(clientSocket.getInputStream());92serviceRequest();93// System.out.print("Service handler exits94// "+clientSocket+"\n");95} catch(Exception e) {96// System.out.print("Service handler failure\n");97// e.printStackTrace();98}99try {100close();101} catch(IOException e2) {}102}103}104105/** Start a server on port <i>port</i>. It will call serviceRequest()106for each new connection. */107final public void startServer(int port) throws IOException {108serverSocket = new ServerSocket(port, 50);109serverInstance = new Thread(this);110serverInstance.start();111}112113/** Service one request. It is invoked with the clientInput and114clientOutput streams initialized. This method handles one client115connection. When it is done, it can simply exit. The default116server just echoes it's input. It is invoked in it's own private117thread. */118public void serviceRequest() throws IOException {119byte buf[] = new byte[300];120int n;121clientOutput.print("Echo server " + getClass().getName() + "\n");122clientOutput.flush();123while ((n = clientInput.read(buf, 0, buf.length)) >= 0) {124clientOutput.write(buf, 0, n);125}126}127128public static void main(String argv[]) {129try {130new NetworkServer ().startServer(8888);131} catch (IOException e) {132System.out.print("Server failed: "+e+"\n");133}134}135136/**137* Clone this object;138*/139public Object clone() {140try {141return super.clone();142} catch (CloneNotSupportedException e) {143// this shouldn't happen, since we are Cloneable144throw new InternalError(e);145}146}147148public NetworkServer () {149}150}151152153