Java RMI客户端出口
我目前正在自学Java,并试图构建一个JavaRMI拍卖系统 我目前有很多课程Java RMI客户端出口,java,for-loop,rmi,Java,For Loop,Rmi,我目前正在自学Java,并试图构建一个JavaRMI拍卖系统 我目前有很多课程 auctionServer - starts the RMI registry, creates localhost service "AuctionService" auctionInterface - provides a description of remote methods available as provided by auctionImpl auctionImpl - implements the
auctionServer - starts the RMI registry, creates localhost service "AuctionService"
auctionInterface - provides a description of remote methods available as provided by auctionImpl
auctionImpl - implements the RMI interface
auctionBuyer - a client to offer options to bid on items
auctionSeller - a client to offer options to list items on the server
我所面临的困难是为用户提供一种退出客户端的方法,同时保持服务器运行
这是我的服务器代码
公共类拍卖服务器{
public auctionServer() {
try {
LocateRegistry.createRegistry(1099);
auctionInterface c = new auctionImpl();
Naming.rebind("rmi://localhost/AuctionService", c);
System.out.println("Server running.....");
}
catch (Exception e) {
System.out.println("Server Error: " + e);
}
}
public static void main(String args[]) {
new auctionServer();
}
}
目前我在拍卖样本中对卖家客户有以下方法:
public void sellerChoice(){
System.out.println("---- What would you like to do? ----\n");
System.out.println("1: List an item for auction\n"); /* Main menu interface */
System.out.println("2: Print a list of active items\n");
System.out.println("3: Remove an item from the auction\n");
System.out.println("4: Close the seller client\n");
int menuSelection = scanner.nextInt(); // Create switch case
switch(menuSelection){
case 1: // If user chose option 1
listItem();
break;
case 2: // If user chose option 2
printList();
break;
case 3: // If user chose option 3
removeItem();
break;
case 4:
System.exit(0);
break;
default: // If user chose an option other than 1*2*3
System.out.println("You didn't select 1, 2 or 3!!");
}
}
正如您所想象的,System.exit并没有按照我希望它在这里的方式工作,因为它不仅终止于客户端,还终止于服务器。因此,我正在努力找到一种方法来实现在不关闭服务器的情况下退出卖方客户机的方法
另一件我正在努力解决的事情是检查在创建物品时是否已经使用了auctionID
public void listItem(){
ItemInfo createdItem = new ItemInfo();
System.out.println("----Enter the auctionName----");
createdItem.setAuctionName(scanner.next());
System.out.println("----Enter the auctionID----");
createdItem.setAuctionID(scanner.nextInt());
System.out.println("----Enter the item startPrice in pound sterling----");
createdItem.setStartPrice(scanner.nextInt());
System.out.println("----Enter the buyoutPrice in pound sterling----");
createdItem.setBuyoutPrice(scanner.nextInt());
itemSet.add(createdItem);
System.out.println("---- Item successfully listed ----");
System.out.println("---- Press 1 to return to auction main menu ----");
if(scanner.nextInt() == 1){
sellerChoice();
}
}
我尝试编写一个for循环,检查auctionID是否已经存在,但是它根本不要求用户输入任何内容,而是直接询问起始价格。我试过的环看起来像这样
for(ItemInfo info : itemSet){
if(scanner.nextInt() == info.auctionID){
System.out.println("auctionID already exists!");
}
}
我对堆栈溢出和Java都是新手,所以如果这个问题太长,请告诉我。非常感谢您的帮助
MichaelGG您是否在同一个jvm中运行客户端和服务器?如果是这样,那就是你的问题。它们应该在单独的jvm实例中运行。我让它们在不同的命令行中运行,System.exit也会结束服务器进程。相当恼人的是.System.exit结束了整个JVM。所以不要从服务器代码中调用它。在客户端调用它。。您还从服务器中的用户获取输入,而服务器也应该在客户端中。坦率地说,你的设计太棒了。谢谢你的回答@EJP。你能告诉我如何改变我的设计吗?我刚才就是这么做的。在客户端中调用System.exit。在客户端中获取用户输入。