JAVA ATM 服务器和客户端实现
import com.cqu.web.ATM;
import com.cqu.web.Account;
import com.cqu.web.Data;
import javax.sound.midi.Soundbank;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
public class Client {
public Client(){
try{
Socket socket=new Socket("hostpost",8888);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
while(true){
//客户端先读后写
Object obj = null;
while ((obj = ois.readObject()) != null){
List<Account> otherData = (List<Account>) obj;
ATM.usersList = otherData;
break;
}
Account myAccount = ATM.currentAccount;
Account otherAccount = ATM.otherAccount;
oos.writeObject(myAccount);
oos.writeObject(otherAccount);
oos.flush();
oos.reset();
//(1000);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}