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

📄 socketlistserver.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 SocketListServer implements Runnable {
  protected Vector handlers;
  protected IDList idList;
  protected Socket socket;
  
  public SocketListServer (Socket socket, Vector handlers, IDList idList) {
    this.socket = socket;
    this.handlers = handlers;
    this.idList = idList;
  }

  protected Thread listener;
  
  public synchronized void start () {
    if (listener == null) {
      listener = new Thread (this);
      listener.start ();
    }
  }

  public synchronized void stop () {
    if (listener != null) {
      if (listener != Thread.currentThread ())
        listener.interrupt ();
      listener = null;
      try {
        socket.close ();
      } catch (IOException ignored) {
      }
    }
  }

  public void run () {
    try {
      init ();
      try {
        execute ();
      } finally {
        handlers.removeElement (this);
      }
    } catch (IOException ex) {
      if (listener == Thread.currentThread ())
        ex.printStackTrace ();
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace ();
    }
    stop ();
  }

  protected ObjectInputStream objectIn;
  protected ObjectOutputStream objectOut;
  
  protected void init () throws IOException {
    objectOut = new ObjectOutputStream (
      new BufferedOutputStream (socket.getOutputStream (), 2048));
    objectOut.flush ();
    objectIn = new ObjectInputStream (
      new BufferedInputStream (socket.getInputStream ()));
    synchronized (handlers) {
      handlers.addElement (this);
      transmit (new InitMsg (idList));
    }
    flush ();
  }

  protected void transmit (IDListMsg msg) {
    if (listener != null) {
      try {
        synchronized (objectOut) {
          objectOut.writeObject (msg);
        }
      } catch (IOException ex) {
        stop ();
        ex.printStackTrace ();
      }
    }
  }

  protected void flush () {
    if (listener != null) {
      try {
        synchronized (objectOut) {
          objectOut.flush ();
        }
      } catch (IOException ex) {
        stop ();
        ex.printStackTrace ();
      }
    }
  }

  protected void execute () throws IOException, ClassNotFoundException {
    while (listener != null) {
      IDListMsg msg = (IDListMsg) objectIn.readObject ();
      synchronized (handlers) {
        if (msg instanceof AddElementMsg) {
          ID id = idList.allocateID ();
          Object element = ((AddElementMsg) msg).getElement ();
          if (idList.addElement (id, element))
            broadcast (new ElementAddedMsg (id, element));
        } else if (msg instanceof UpdateElementMsg) {
          ID oldID = ((UpdateElementMsg) msg).getOldID ();
          ID id = idList.allocateID ();
          Object element = ((UpdateElementMsg) msg).getElement ();
          if (idList.updateElement (oldID, id, element))
            broadcast (new ElementUpdatedMsg (oldID, id, element));
        } else if (msg instanceof ReplaceElementMsg) {
          ID oldID = ((ReplaceElementMsg) msg).getOldID ();
          ID id = idList.allocateID ();
          Object element = ((ReplaceElementMsg) msg).getElement ();
          if (idList.replaceElement (oldID, id, element))
            broadcast (new ElementReplacedMsg (oldID, id, element));
        } else if (msg instanceof RemoveElementMsg) {
          ID id = ((RemoveElementMsg) msg).getID ();
          if (idList.removeElement (id))
            broadcast (new ElementRemovedMsg (id));
        } else if (msg instanceof QuitMsg) {
          break;
        } else {
          throw new IOException ("Unknown message: " + msg);
        }
      }
      flushAll ();
    }
  }

  protected void broadcast (IDListMsg msg) {
    synchronized (handlers) {
      for (int i = 0; i < handlers.size (); ++ i) {
        SocketListServer handler = (SocketListServer) handlers.elementAt (i);
        handler.transmit (msg);
      }
    }
  }

  protected void flushAll () {
    Vector handlers = (Vector) this.handlers.clone ();
    for (int i = 0; i < handlers.size (); ++ i) {
      SocketListServer handler = (SocketListServer) handlers.elementAt (i);
      handler.flush ();
    }
  }

  public static void main (String[] args) throws IOException {
    if (args.length != 1)
      throw new IllegalArgumentException ("Syntax: SocketListServer <port>");
    int port = Integer.parseInt (args[0]);
    Vector handlers = new Vector ();
    IDList idList = new IDList ();
    ServerSocket serverSocket = new ServerSocket (port);
    while (true) {
      Socket client = serverSocket.accept ();
      SocketListServer handler =
        new SocketListServer (client, handlers, idList);
      handler.start ();
    }
  }
}

⌨️ 快捷键说明

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