serversocketremoteserver.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 169 行

SVN-BASE
169
字号
/* * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program 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 version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package sample.distributed.server;import java.io.IOException;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketException;import com.sun.javatest.Status;import com.sun.tck.j2me.services.messagingService.J2SEDistributedTest;/** * Sample test that shows how to obtain configuration values provided * in the interview. */public class ServerSocketRemoteServer extends J2SEDistributedTest {    private SocketServerThread server;    /**     * Creates an instance of this remote component.     */    public ServerSocketRemoteServer() {        super("ServerSocketRemoteServer");    }    public void handle_serverUp(String from, String[] args, byte[] bytes) {        try {            log.println("FROM: " + from);            for (int i = 0; i < args.length; i++) {                log.println("args[" + i + "] = " + args[i]);            }            log.println("Starting up the server...");            try {                server = new SocketServerThread();                server.start();            } catch (IOException ioe) {                send(from, new String[] {"serverUp", "ERROR"});                return;            }            log.println("Server started on port " + server.getPort());            send(from, new String[] {"serverUp", server.getPort()});        } catch (IOException e) {            log.println(ERROR_SERVER_UP);            e.printStackTrace(log);            setMsgStatus(Status.error(ERROR_SERVER_UP));        }    }    public void handle_serverDown(String from, String[] args, byte[] bytes) {       log.println("Shutting down the server...");       if (server != null) {           server.stop();       }       try {           send(from, new String[] {"serverDown", "OK"});       } catch (IOException e) {           log.println(ERROR_SERVER_DOWN);           e.printStackTrace(log);           setMsgStatus(Status.error(ERROR_SERVER_DOWN));       }    }    private final static String ERROR_SERVER_UP        = "Unexpected IOException while sending serverUp response";        private final static String ERROR_SERVER_DOWN        = "Unexpected IOException while sending serverDown response";}class SocketServerThread implements Runnable {    private boolean started = false;    private ServerSocket serverSocket;    public SocketServerThread() throws IOException {        serverSocket = new ServerSocket(0);    }    public synchronized String getPort() {        return "" + serverSocket.getLocalPort();    }    public void run() {        started = true;        while (isStarted()) {            Socket socket = null;            OutputStream os = null;            try {                System.out.println("Waiting for connection");                socket = serverSocket.accept();                System.out.println("Connection accepted");                os = socket.getOutputStream();                os.write('t');                os.write('e');                os.write('s');                os.write('t');                os.write('\n');                os.flush();            } catch (SocketException se) {                // normal case when we close the server socket            } catch (IOException e) {                System.out.println("Server exception " + e);            } finally {                try {                    if (socket != null) {                        socket.close();                    }                } catch (IOException e1) {                    // ignore, can't do much                }                if (os != null) {                    try {                        os.close();                    } catch (IOException e) {                        // ignore, can't do much                    }                }            }        }    }    public synchronized boolean isStarted() {        return started;    }    public synchronized void start() {        Thread runner = new Thread(this);        runner.start();    }    public synchronized void stop() {        started = false;        if (serverSocket != null) {            try {                serverSocket.close();            } catch (IOException ignore) {                // ignore, we're shutting down            }        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?