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

📄 nwclient.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.*;

class NwClient {
  String host;
  int port;
  Socket skt;
  InputStream is;
  ObjectInputStream ois;
  OutputStream os;
  ObjectOutputStream oos;
  int retry;

  public NwClient(String host, int port) {
    //** 1 Initialize the corresponding host attribute
    this.host = host;
    //** 2 Initialize the corresponding port attribute
    this.port = port;
    //** 3 Invoke the connect() method
    connect();
  }

  public void send(Object obj) throws Exception {
    retry = 10;
    while(retry > 0) {
      try {
    //** 1. Invoke the writeObject method of oos with 
    //**    obj as parameter
        oos.writeObject(obj);
        retry = 0;
        System.out.println("NwClient.send: " + obj + "sent");
      } catch(Exception e) {
        System.out.println("NwClient.send: " + e);
        retry--;
        if (retry == 0) {
          throw e;
        }
        Thread.sleep(6000);
        connect();
      }
    }
  }

  public Object receive() throws Exception{
    Object obj = null;
    try {
      if (ois == null) {
        //** 1. Create a new ObjectInputStream object with is 
        //**    as the input parameter. Assign this to ois.
        //Q? Why is ois created here, rather than in the connect
        //   method, where all the other streams are created?
        ois = new ObjectInputStream(is);
      }
      //** 2. Invoke the readObject method of ois and assign the 
      //**    return value to obj
      obj = ois.readObject();
    } catch(Exception e) {
      System.out.println("NwClient.receive: " + e);
      connect();
      throw e;
    }
    return obj;
  }

  public void connect() {
    try {
      //** 1. Create a new Socket with host, port as parameters
      //**    and assign this socket to skt.
      skt = new Socket(host, port);
      //** 2. Invoke getInputStream method on skt and assign the
      //**    return value to is.
      is = skt.getInputStream();
      //** 3. Assign null to ois.
      //Q? Why is ois not created here, but instead assigned 
      //   to null?
      ois = null;
      //** 4. Invoke getOutputStream method on skt and assign
      //**     the return value to os.
      os = skt.getOutputStream();
      //** 5. Create a new ObjectOutputStream object with os as
      //**    parameter and assign this newly created object
      //**    to oos.
      oos = new ObjectOutputStream(os);
    } catch(Exception e) {
      System.out.println("NwClient.connect: " + e);
    }
  }

  public void connect(String host, int port) {
    //** 1 Initialize the corresponding host attribute
    this.host = host;
    //** 2 Initialize the corresponding port attribute
    this.port = port;
    //** 3 Invoke the connect() method
    connect();
  }

  public void close() {
    try {
    } finally {
      if (skt != null) {
        try {
          skt.close();
        } catch (IOException e) { 
          System.out.println(e);
        }
      }
    }
  }

 /*
  public static void main(String args[]) {
    Command cmd;
    String id = "111-11-1111";
    Customer cust;
    String modelHost = "localhost";
    if (args.length > 0) {
      modelHost = args[0];
    }
    NwClient client = new NwClient(modelHost, 6001);
    while (true){
      try {
        cmd = new GetCustomerCommand(id);
        client.send(cmd);
        Thread.sleep(5000);
        cmd = (Command)client.receive();
        cust = (Customer)cmd.getResult();
        System.out.println("received " + cust);
      } catch(Exception e) {
        e.printStackTrace();
      } 
    }
  }
 */
}

⌨️ 快捷键说明

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