?? nwclient.java
字號(hào):
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);
}
}
}
}
//** main() has been reverted by Crane
/*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();
}
}
}*/
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -