jdk/src/share/classes/sun/net/NetworkServer.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2000, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This is the base class for network servers.  To define a new type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of server define a new subclass of NetworkServer with a serviceRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * method that services one request.  Start the server by executing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *      new MyServerClass().startServer(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class NetworkServer implements Runnable, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /** Socket for communicating with client. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public Socket clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private Thread serverInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private ServerSocket serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /** Stream for printing to the client. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public PrintStream clientOutput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** Buffered stream for reading replies from client. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public InputStream clientInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /** Close an open connection to the client. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        clientSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        clientInput = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        clientOutput = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Return client connection status */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public boolean clientIsOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return clientSocket != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    final public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (serverSocket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            // System.out.print("Server starts " + serverSocket + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    Socket ns = serverSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
//                  System.out.print("New connection " + ns + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    NetworkServer n = (NetworkServer)clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    n.serverSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    n.clientSocket = ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    new Thread(n).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    System.out.print("Server failure\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        serverSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    } catch(IOException e2) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    System.out.print("cs="+serverSocket+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
//          close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                clientOutput = new PrintStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        new BufferedOutputStream(clientSocket.getOutputStream()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                               false, "ISO8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                clientInput = new BufferedInputStream(clientSocket.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                serviceRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                // System.out.print("Service handler exits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                // "+clientSocket+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                // System.out.print("Service handler failure\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                // e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            } catch(IOException e2) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /** Start a server on port <i>port</i>.  It will call serviceRequest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        for each new connection. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    final public void startServer(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        serverSocket = new ServerSocket(port, 50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        serverInstance = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        serverInstance.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /** Service one request.  It is invoked with the clientInput and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        clientOutput streams initialized.  This method handles one client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        connection. When it is done, it can simply exit. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        server just echoes it's input. It is invoked in it's own private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        thread. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void serviceRequest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        byte buf[] = new byte[300];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        clientOutput.print("Echo server " + getClass().getName() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        clientOutput.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        while ((n = clientInput.read(buf, 0, buf.length)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            clientOutput.write(buf, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static void main(String argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            new NetworkServer ().startServer(8888);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.out.print("Server failed: "+e+"\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Clone this object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // this shouldn't happen, since we are Cloneable
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   145
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public NetworkServer () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}