Java Reference
In-Depth Information
L ISTING 6.1 Continued
6
DataInputStream in = null;
private String thisClientId;
public OldBankClient() {
thisClientId = “client0”;
try {
socket = new Socket(“localhost”, 3333);
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
} catch (Exception e) {
System.out.println(“BankClient- Couldn't work out the connection: “+e);
System.exit(1);
}
try {
executeTransaction();
// dismiss connection
sendCommand('q');
out.close();
in.close();
socket.close();
} catch (IOException exce) {
System.out.println(“BankClient- executing transaction: “+exce);
}
System.out.println(“BankClient- Client Log out.”);
}
private void post(String data) throws IOException{
sendCommand('p');
out.writeUTF(data);
}
private byte[] get(String serverFilename) throws IOException{
sendCommand('g');
out.writeUTF(serverFilename);
int size = in.readInt();
byte[] b = new byte[size];
in.readFully(b);
return b;
}
private void executeTransaction() throws IOException{
byte[] b = get(“test.txt”);
post(“hello everybody”);
}
Search WWH ::




Custom Search