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

📄 nwserver.java

📁 some easy projects in java, can teach you basic project design method in java.
💻 JAVA
字号:
package trader.nw;

import java.io.*;
import java.net.*;
import trader.*;
import trader.db.*;

class NwServer {
  int port;
  private ServerSocket serverSocket;
  private IoStrategy ioServer;
  private Socket skt;

  public NwServer(int port, IoStrategy ioServer) {
    //** 1 Code in this constructor is similar (but not
    //** identical) to the constructor of CommandServer
    this.port = port;
    this.ioServer = ioServer;
    try {
      serverSocket = new ServerSocket(port);
      System.out.println
        ("NwServer created ServerSocket on port " + port);
      this.acceptConnection();
    } catch(Exception e) {
      System.out.println("NwServer.constructor: " + e);
    }
  }

  public void acceptConnection() {
    System.out.println("NwServer.acceptConnection ");
    //** 1 Code in this method is similar (but not
    //** identical) to the corresponding method of CommandServer
    //** Hint the skt from the acceptConnection method
    //   of serverSocket must be passed to the ioService
    //   method of the ioServer object
    while(true) {
      try {
        skt = serverSocket.accept(); // listening;
        System.out.println("NwServer.acceptConnection + accepted");
        ioServer.ioService(skt);
      } catch(Exception e) {
        System.out.println("NwServer.acceptConnection: " + e);
      }
    }
  }

  public static void main(String args[]) {
    try {
      BrokerModel bd = new BrokerModelDbImpl("localhost");
      BrokerServer bs = new BrokerServer(bd);
      NwServer server = new NwServer(6001, bs);
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

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