⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericclient.java

📁 java网络编程方面的源码,其中有一个整合的聊天室,比较不错,建议大家下载练习,配合java网络编程技术内幕看
💻 JAVA
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
import java.net.*;
import java.util.*;

public class GenericClient extends Thread {
  static private int clientNumber;
  static private synchronized int nextClientNum () { return clientNumber ++; }
  
  protected Queue queue;
  protected Hashtable registry;
  protected MessageOutput messageOut;

  public GenericClient (String host, int port, String name)
    throws IOException {
    super ("GenericClient-" + nextClientNum ());
    connect (host, port);
    logon (name);
    registry = new Hashtable ();
    queue = new Queue ();
    QueueOutputStream queueOut = new QueueOutputStream (queue);
    messageOut = new RoutingOutputStream (queueOut);
  }

  protected InputStream in;
  protected OutputStream out;
  
  protected void connect (String host, int port) throws IOException {
    Socket socket = new Socket (host, port);
    in = socket.getInputStream ();
    out = socket.getOutputStream ();
  }

  protected void logon (String name) throws IOException {
    try {
      DataOutputStream dataOut = new DataOutputStream (out);
      dataOut.writeUTF (name);
      dataOut.flush ();
      DataInputStream dataIn = new DataInputStream (in);
      boolean registered = dataIn.readBoolean ();
      if (!registered)
        throw new IOException ("Name in use");
    } catch (IOException ex) {
      try {
        out.close ();
      } catch (IOException ignored) {
      }
      throw ex;
    }
  }

  protected boolean finished;
  
  public void shutdown () throws IOException {
    finished = true;
    interrupt ();
    out.close ();
  }

  public void run () {
    QueueInputStream queueIn = new QueueInputStream (queue);
    MessageOutputStream messageOut = new MessageOutputStream (out);
    GenericMessageCopier copier =
      new GenericMessageCopier (this, queueIn, messageOut);
    try {
      copier.start ();
      demux ();
    } catch (IOException ex) {
      if (!finished)
        ex.printStackTrace ();
    } finally {
      copier.finish ();
      closedown ();
    }
  }

  protected void demux () throws IOException {
    MessageInputStream messageIn = new MessageInputStream (in);
    MultiplexInputStream multiplexIn = new MultiplexInputStream (messageIn);
    while (!Thread.interrupted ()) {
      multiplexIn.receive ();
      Client client = (Client) registry.get (multiplexIn.getLabel ());
      if (client != null) {
        try {
          client.receive (new DataInputStream (multiplexIn));
        } catch (RuntimeException ex) {
          ex.printStackTrace ();
        }
      }
    }
  }

  protected void closedown () {
    try {
      out.close ();
    } catch (IOException ex) {
      ex.printStackTrace ();
    }
    synchronized (registry) {
      Enumeration clients = registry.keys ();
      while (clients.hasMoreElements ()) {
        deregister ((String) clients.nextElement ());
      }
    }
  }

  public void register (String label, Client client) {
    synchronized (registry) {
      deregister (label);
      registry.put (label, client);
      client.setMessageOutput
        (new MultiplexOutputStream (messageOut, label));
    }
  }

  public void deregister (String label) {
    synchronized (registry) {
      Client client = (Client) registry.remove (label);
      if (client != null)
        client.disconnected ();
    }
  }
}

⌨️ 快捷键说明

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