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

📄 genericchat.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.awt.*;
import java.awt.event.*;

public class GenericChat {
  protected String host, name;
  protected int port;
  
  public GenericChat (String host, int port, String name) {
    this.host = host;
    this.port = port;
    this.name = name;
    initAWT ();
  }

  protected Frame frame;
  protected ChatboardClient cb;
  protected WhiteboardClient wb;
  
  protected void initAWT () {
    frame = new Frame ("Generic Chat");
    frame.setLayout (new GridLayout (2, 1));
    cb = new ChatboardClient ();
    wb = new WhiteboardClient ();
    frame.add (wb);
    frame.add (cb);
    frame.pack ();
    frame.addWindowListener (new WindowAdapter () {
      public void windowClosing (WindowEvent event) {
        try {
          stop ();
        } catch (IOException ex) {
          ex.printStackTrace ();
        }
      }
    });
  }

  protected GenericClient client;
  
  public void start () throws IOException {
    client = new GenericClient (host, port, name);
    client.register ("chat", cb);
    client.register ("wb", wb);
    client.start ();
    frame.setVisible (true);
  }

  public void stop () throws IOException {
    frame.setVisible (false);
    client.shutdown ();
  }

  public static void main (String[] args) throws IOException {
    if (args.length != 3)
      throw new IllegalArgumentException
        ("Syntax: GenericChat <host> <port> <name>");
    GenericChat chat = new GenericChat
      (args[0], Integer.parseInt (args[1]), args[2]);
    chat.start ();
  }
}

⌨️ 快捷键说明

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