亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? 一個socket服務器程序.java

?? java寫的一個socket服務器程序
?? JAVA
?? 第 1 頁 / 共 2 頁
字號:
/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * This class is a generic framework for a flexible, multi-threaded server.
 * It listens on any number of specified ports, and, when it receives a 
 * connection on a port, passes input and output streams to a specified Service
 * object which provides the actual service.  It can limit the number of
 * concurrent connections, and logs activity to a specified stream.
 **/
public class Server {
    /**
     * A main() method for running the server as a standalone program.  The
     * command-line arguments to the program should be pairs of servicenames
     * and port numbers.  For each pair, the program will dynamically load the
     * named Service class, instantiate it, and tell the server to provide
     * that Service on the specified port.  The special -control argument
     * should be followed by a password and port, and will start special
     * server control service running on the specified port, protected by the
     * specified password.
     **/
    public static void main(String[] args) {
        try {
            if (args.length < 2)  // Check number of arguments
                throw new IllegalArgumentException("Must specify a service");
            
            // Create a Server object that uses standard out as its log and
            // has a limit of ten concurrent connections at once.
            Server s = new Server(System.out, 10);

            // Parse the argument list
            int i = 0;
            while(i < args.length) {
                if (args[i].equals("-control")) {  // Handle the -control arg
                    i++;
                    String password = args[i++];
                    int port = Integer.parseInt(args[i++]);
      // add control service
                    s.addService(new Control(s, password), port);
                } 
                else {
                    // Otherwise start a named service on the specified port.
                    // Dynamically load and instantiate a Service class
                    String serviceName = args[i++];
                    Class serviceClass = Class.forName(serviceName); 
                    Service service = (Service)serviceClass.newInstance();
                    int port = Integer.parseInt(args[i++]);
                    s.addService(service, port);
                }
            }
        }
        catch (Exception e) { // Display a message if anything goes wrong
            System.err.println("Server: " + e);
            System.err.println("Usage: java Server " +
          "[-control <password> <port>] " +
          "[<servicename> <port> ... ]");
            System.exit(1);
        }
    }

    // This is the state for the server
    Map services;                   // Hashtable mapping ports to Listeners
    Set connections;                // The set of current connections
    int maxConnections;             // The concurrent connection limit
    ThreadGroup threadGroup;        // The threadgroup for all our threads
    PrintWriter logStream;          // Where we send our logging output to

    /**
     * This is the Server() constructor.  It must be passed a stream 
     * to send log output to (may be null), and the limit on the number of
     * concurrent connections.  
     **/
    public Server(OutputStream logStream, int maxConnections) { 
        setLogStream(logStream);
        log("Starting server");
        threadGroup = new ThreadGroup(Server.class.getName());
 this.maxConnections = maxConnections;
        services = new HashMap();
 connections = new HashSet(maxConnections);
    }
    
    /** 
     * A public method to set the current logging stream.  Pass null
     * to turn logging off
     **/
    public synchronized void setLogStream(OutputStream out) {
        if (out != null) logStream = new PrintWriter(out);
        else logStream = null;
    }

    /** Write the specified string to the log */
    protected synchronized void log(String s) { 
        if (logStream != null) {
            logStream.println("[" + new Date() + "] " + s);
            logStream.flush();
        }
    }
    /** Write the specified object to the log */
    protected void log(Object o) { log(o.toString()); }
    
    /**
     * This method makes the server start providing a new service.
     * It runs the specified Service object on the specified port.
     **/
    public synchronized void addService(Service service, int port)
 throws IOException
    {
        Integer key = new Integer(port);  // the hashtable key
        // Check whether a service is already on that port
        if (services.get(key) != null) 
            throw new IllegalArgumentException("Port " + port +
            " already in use.");
        // Create a Listener object to listen for connections on the port
        Listener listener = new Listener(threadGroup, port, service);
        // Store it in the hashtable
        services.put(key, listener);
        // Log it
        log("Starting service " + service.getClass().getName() + 
     " on port " + port);
        // Start the listener running.
        listener.start();
    }
    
    /**
     * This method makes the server stop providing a service on a port.
     * It does not terminate any pending connections to that service, merely
     * causes the server to stop accepting new connections
     **/
    public synchronized void removeService(int port) {
        Integer key = new Integer(port);  // hashtable key
        // Look up the Listener object for the port in the hashtable
        final Listener listener = (Listener) services.get(key);
        if (listener == null) return;
        // Ask the listener to stop
        listener.pleaseStop();
        // Remove it from the hashtable
        services.remove(key);
        // And log it.
        log("Stopping service " + listener.service.getClass().getName() + 
     " on port " + port);
    }
    
    /** 
     * This nested Thread subclass is a "listener".  It listens for
     * connections on a specified port (using a ServerSocket) and when it gets
     * a connection request, it calls the servers addConnection() method to
     * accept (or reject) the connection.  There is one Listener for each
     * Service being provided by the Server.
     **/
    public class Listener extends Thread {
        ServerSocket listen_socket;    // The socket to listen for connections
        int port;                      // The port we're listening on
        Service service;               // The service to provide on that port
        volatile boolean stop = false; // Whether we've been asked to stop

        /**
  * The Listener constructor creates a thread for itself in the
  * threadgroup.  It creates a ServerSocket to listen for connections
  * on the specified port.  It arranges for the ServerSocket to be
  * interruptible, so that services can be removed from the server.
  **/
        public Listener(ThreadGroup group, int port, Service service) 
     throws IOException
 {
            super(group, "Listener:" + port);      
            listen_socket = new ServerSocket(port);
            // give it a non-zero timeout so accept() can be interrupted
            listen_socket.setSoTimeout(600000);
            this.port = port;
            this.service = service;
        }

        /** 
  * This is the polite way to get a Listener to stop accepting
  * connections
  ***/
        public void pleaseStop() {
            this.stop = true;              // Set the stop flag
            this.interrupt();              // Stop blocking in accept()
     try { listen_socket.close(); } // Stop listening.
     catch(IOException e) {}
        }
        
        /**
  * A Listener is a Thread, and this is its body.
  * Wait for connection requests, accept them, and pass the socket on
  * to the addConnection method of the server.
  **/
        public void run() {
            while(!stop) {      // loop until we're asked to stop.
                try {
                    Socket client = listen_socket.accept();
                    addConnection(client, service);
                } 
                catch (InterruptedIOException e) {} 
                catch (IOException e) {log(e);}
            }
        }
    }
 
    /**
     * This is the method that Listener objects call when they accept a
     * connection from a client.  It either creates a Connection object 
     * for the connection and adds it to the list of current connections,
     * or, if the limit on connections has been reached, it closes the 
     * connection. 
     **/
    protected synchronized void addConnection(Socket s, Service service) {
 // If the connection limit has been reached
 if (connections.size() >= maxConnections) {
     try {
  // Then tell the client it is being rejected.
  PrintWriter out = new PrintWriter(s.getOutputStream());
  out.print("Connection refused; " +
     "the server is busy; please try again later.\n");
  out.flush();
  // And close the connection to the rejected client.
  s.close();
  // And log it, of course
  log("Connection refused to " +
      s.getInetAddress().getHostAddress() +
      ":" + s.getPort() + ": max connections reached.");
     } catch (IOException e) {log(e);}
 }
 else {  // Otherwise, if the limit has not been reached
     // Create a Connection thread to handle this connection
     Connection c = new Connection(s, service);
     // Add it to the list of current connections
     connections.add(c);
     // Log this new connection
     log("Connected to " + s.getInetAddress().getHostAddress() +
  ":" + s.getPort() + " on port " + s.getLocalPort() +
  " for service " + service.getClass().getName());
     // And start the Connection thread to provide the service
     c.start();
 }
    }

    /**
     * A Connection thread calls this method just before it exits.  It removes
     * the specified Connection from the set of connections.
     **/
    protected synchronized void endConnection(Connection c) {
 connections.remove(c);
 log("Connection to " + c.client.getInetAddress().getHostAddress() +
     ":" + c.client.getPort() + " closed.");
    }

    /** Change the current connection limit */
    public synchronized void setMaxConnections(int max) {
 maxConnections = max;
    }

    /**
     * This method displays status information about the server on the
     * specified stream.  It can be used for debugging, and is used by the
     * Control service later in this example.
     **/
    public synchronized void displayStatus(PrintWriter out) {
 // Display a list of all Services that are being provided
 Iterator keys = services.keySet().iterator();
 while(keys.hasNext()) {
     Integer port = (Integer) keys.next();
     Listener listener = (Listener) services.get(port);
     out.print("SERVICE " + listener.service.getClass().getName()
        + " ON PORT " + port + "\n");
 }
 
 // Display the current connection limit
 out.print("MAX CONNECTIONS: " + maxConnections + "\n");

 // Display a list of all current connections
 Iterator conns = connections.iterator();
 while(conns.hasNext()) {
     Connection c = (Connection)conns.next();
     out.print("CONNECTED TO " +
        c.client.getInetAddress().getHostAddress() +
        ":" + c.client.getPort() + " ON PORT " +
        c.client.getLocalPort() + " FOR SERVICE " +
        c.service.getClass().getName() + "\n");
 }
    }

    /**
     * This class is a subclass of Thread that handles an individual
     * connection between a client and a Service provided by this server.
     * Because each such connection has a thread of its own, each Service can
     * have multiple connections pending at once.  Despite all the other
     * threads in use, this is the key feature that makes this a
     * multi-threaded server implementation.
     **/
    public class Connection extends Thread {

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美久久一二区| 国产亚洲婷婷免费| 国产揄拍国内精品对白| 日韩不卡免费视频| 三级不卡在线观看| 香蕉乱码成人久久天堂爱免费| 国产精品进线69影院| 欧美激情一区二区在线| 久久久99久久| 亚洲永久精品国产| 亚洲成在人线在线播放| 国产精品资源在线观看| 国产91在线观看丝袜| 成人涩涩免费视频| 91在线视频播放地址| 色婷婷亚洲一区二区三区| 亚洲精品一区二区三区福利| 555www色欧美视频| 精品国产免费人成在线观看| 亚洲黄色av一区| 婷婷一区二区三区| 久久国产精品72免费观看| 美女一区二区在线观看| 久色婷婷小香蕉久久| 欧美亚一区二区| 欧美白人最猛性xxxxx69交| 精品99一区二区| 肉色丝袜一区二区| 欧美性色黄大片手机版| 亚洲三级理论片| 日韩电影一区二区三区四区| 一本大道av一区二区在线播放 | 国精产品一区一区三区mba桃花| 免费成人美女在线观看| 国产精品一二三四| 久久久青草青青国产亚洲免观| 亚洲天堂网中文字| 91亚洲资源网| 欧美r级电影在线观看| 日本女优在线视频一区二区| 欧美日韩中文字幕精品| 欧美极品xxx| 欧美日韩五月天| 亚洲无人区一区| 国产激情视频一区二区三区欧美| 91蜜桃网址入口| 一区二区在线电影| 欧美综合亚洲图片综合区| 久久综合久久综合久久综合| 亚洲国产日韩一级| 678五月天丁香亚洲综合网| 日韩福利视频网| 日韩免费电影一区| 亚洲国产精品麻豆| 日韩一区二区免费在线电影| 亚洲激情在线激情| 欧美精品九九99久久| 日韩激情在线观看| 久久综合色一综合色88| 成人av电影免费在线播放| 久久免费的精品国产v∧| 国产成人午夜视频| 亚洲婷婷在线视频| 欧美精品久久99| 在线观看视频一区二区| 三级久久三级久久| 国产嫩草影院久久久久| 精品一区二区在线播放| 欧美精品三级在线观看| 久久精品久久精品| 国产精品久久久久一区二区三区| 色av一区二区| 亚洲一区二区在线播放相泽| 欧美一区二区在线观看| 秋霞午夜鲁丝一区二区老狼| 久久久天堂av| 欧美日韩国产美女| 日韩国产成人精品| 国产精品少妇自拍| 不卡一区二区在线| 日本伊人色综合网| 中文字幕免费在线观看视频一区| 欧美性色综合网| 国产成人精品免费视频网站| 亚洲成人你懂的| 国产精品欧美久久久久一区二区 | 色婷婷综合久久久久中文一区二区 | 欧美精品一区二区三区四区| jiyouzz国产精品久久| 国产精品久久久久影院老司| 7777女厕盗摄久久久| 成人av免费在线| 精品一区二区在线免费观看| 一区二区三区高清不卡| 欧美日韩在线播放三区| 国产91清纯白嫩初高中在线观看| 亚洲图片欧美视频| 国产精品久久久久久久久快鸭| 欧美精品三级在线观看| 一本大道av伊人久久综合| 国产精品69毛片高清亚洲| kk眼镜猥琐国模调教系列一区二区 | 日本精品视频一区二区| 国产成人激情av| 美国av一区二区| 日日夜夜精品视频天天综合网| 亚洲人成精品久久久久久| 欧美色精品在线视频| 99视频一区二区| 成人午夜碰碰视频| 国产精品1区二区.| 精品在线一区二区三区| 日韩国产欧美一区二区三区| 亚洲精选视频在线| 国产精品国产精品国产专区不片| 精品国产乱码久久久久久图片| 欧美精品在线观看一区二区| 色成年激情久久综合| 99国产精品久久| av中文一区二区三区| 99在线热播精品免费| 成人avav影音| 精品国产乱码久久久久久夜甘婷婷| 欧美高清性hdvideosex| 91麻豆精品91久久久久同性| 欧美日韩国产综合视频在线观看 | 欧美日韩国产影片| 欧美日韩亚洲丝袜制服| 精品视频在线免费| 欧美日韩另类一区| 91麻豆精品91久久久久同性| 91精品国产91热久久久做人人| 欧美日韩日日骚| 在线91免费看| 精品国内二区三区| 国产婷婷精品av在线| 国产日韩精品一区二区三区| 中文乱码免费一区二区| 国产精品国产自产拍高清av王其 | 精品国产乱码久久| 欧美国产日韩一二三区| 中文字幕视频一区| 久久久久99精品国产片| 国产精品乱码一区二三区小蝌蚪| 日韩精品一二三四| 精品亚洲成a人| 成人精品在线视频观看| 日本久久精品电影| 91精品国产欧美日韩| 久久久久国产成人精品亚洲午夜| 国产精品美女久久久久aⅴ国产馆 国产精品美女久久久久av爽李琼 国产精品美女久久久久高潮 | 欧美挠脚心视频网站| 日韩女优电影在线观看| 中文字幕精品一区 | 久久久久久久久岛国免费| 国产精品久久久久一区| 亚洲成人免费影院| 国产一区二区三区精品欧美日韩一区二区三区| 国产一区二区久久| 欧美在线播放高清精品| 精品人在线二区三区| 亚洲青青青在线视频| 另类调教123区| 91美女片黄在线| 精品国产乱码久久久久久夜甘婷婷 | 国产成人在线视频网址| 欧美影院一区二区| 久久嫩草精品久久久久| 国产成人av一区| 在线观看亚洲精品| 国产人伦精品一区二区| 亚洲成人一区二区| jizz一区二区| 2024国产精品视频| 亚洲丶国产丶欧美一区二区三区| 国产精品亚洲综合一区在线观看| 欧美三级日韩三级国产三级| 国产日本欧美一区二区| 男女男精品视频| 在线看不卡av| 国产精品女主播在线观看| 老司机免费视频一区二区| 欧美中文一区二区三区| 国产精品久久毛片| 国产曰批免费观看久久久| 91精品久久久久久久99蜜桃 | 成人亚洲精品久久久久软件| 欧美二区三区91| 亚洲激情av在线| 波多野结衣欧美| 久久精品视频在线免费观看| 三级欧美在线一区| 欧美少妇bbb| 亚洲激情图片小说视频| 成人不卡免费av| 国产精品色在线观看| 在线不卡a资源高清| 亚洲成人手机在线| 欧美亚洲日本国产| 又紧又大又爽精品一区二区|