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

📄 client.java

📁 一个rmi程序例子
💻 JAVA
字号:
import java.rmi.*;
import java.io.*;
import java.util.*;

/*
 * Created on 2006-5-29
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author lizhao
 * @version 1.0.0
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		BufferedReader stdIn = new BufferedReader(new InputStreamReader(
				System.in));

		String registry = "localhost";
		String registration = "";

		if (args.length == 1) {

			registry = args[0];
		}
		registration = "rmi://" + registry + "/message";

		try {
			Remote remoteServiceCenter = Naming.lookup(registration);
			RMIInterface str = (RMIInterface) remoteServiceCenter;

			while (true) {
				/**
				 * Loop forever printing the menu.
				 */
				int choice = 0;
				System.out.println("");
				System.out.println("----------------------------");
				System.out.println("Distributed Message:");
				System.out.println("(1) Register ");
				System.out.println("(2) AddMeeting");
				System.out.println("(3) QueryMeeting");
				System.out.println("(4) DeleteMeeting");
				System.out.println("(5) ClearMeeting");
				System.out.println("(6) Exit");
				System.out.println("-----------------------------");
				System.out.println("Enter Choice: ");

				try {

					choice = Integer.parseInt(stdIn.readLine());
				} catch (Exception e) {
					System.out.println(e);
				}

				switch (choice) {

				case 1: {

					try {
						System.out.println("using  the register()\n");
						System.out.println("Input Unique Username:");
						String userName = stdIn.readLine();
						System.out.println("Input Password:");
						String password = stdIn.readLine();

						String ret = str.register(userName, password);
						System.out.println(ret);
					} catch (Exception e) {
						System.out.println(e);
					}
					break;
				}

				case 2: {

					try {
						System.out.println("using  the addMeeting()\n");
						System.out.println("Input send user's userName: ");
						String hostName = stdIn.readLine();
						System.out.println("Input send user's password: ");
						String password = stdIn.readLine();
						System.out.println("Input accept user's userName: ");
						String guestName = stdIn.readLine();
						System.out
								.println("Input the start time as following yy-mm-dd hh--mm--ss: ");
						Date begin = getDate(stdIn.readLine());
						System.out
								.println("Input the end time as following yy-mm-dd hh--mm--ss: ");
						Date over = getDate(stdIn.readLine());
						System.out.println("Input the meeting's title: ");
						String title = stdIn.readLine();

						if (begin == null || over == null) {

							System.out
									.println("The fomat of the date is wrong!!!! Please input again!!!");
							break;
						}

						String ret = str.addMeeting(hostName, password,
								guestName, begin, over, title);
						System.out.println(ret);

					} catch (Exception e) {
						System.out.println(e);
					}
					break;
				}

				case 3: {

					try {
						System.out.println("using  the queryMeeting()\n");
						System.out.println("Input send user's userName: ");
						String name = stdIn.readLine();
						System.out.println("Input send user's password: ");
						String password = stdIn.readLine();
						System.out
								.println("Input the start time as following yy-mm-dd hh--mm--ss: ");
						Date begin = getDate(stdIn.readLine());
						System.out
								.println("Input the end time as following yy-mm-dd hh--mm--ss: ");
						Date over = getDate(stdIn.readLine());

						if (begin == null || over == null) {

							System.out
									.println("The fomat of the date is wrong!!!! Please input again!!!");
							break;
						}

						String ret = str.queryMeeting(name, password, begin,
								over);
						System.out.println(ret);
					} catch (Exception e) {
						System.out.println(e);
					}
					break;
				}

				case 4: {

					try {
						System.out.println("using  the deleteMeeting()\n");
						System.out.println("Input send user's userName: ");
						String name = stdIn.readLine();
						System.out.println("Input send user's password: ");
						String password = stdIn.readLine();
						System.out.println("Input the meeting's title: ");
						String title = stdIn.readLine();
						String ret = str.deleteMeeting(name, password, title);
						System.out.println(ret);
					} catch (Exception e) {
						System.out.println(e);
					}
					break;
				}

				case 5: {

					try {
						System.out.println("using  the clearMeeting()\n");
						System.out.println("Input send user's userName: ");
						String name = stdIn.readLine();
						System.out.println("Input send user's password: ");
						String password = stdIn.readLine();

						String ret = str.clearMeeting(name, password);
						System.out.println(ret);
					} catch (Exception e) {
						System.out.println(e);
					}
					break;
				}

				case 6: {
					System.exit(0);
				}

				default: {
					System.out.println(choice
							+ " Please Input Choice Integer 1 to 6 .");
				}

				}
			}

		} catch (NotBoundException e) {
			System.out.println("there is  no registry!");
		} catch (RemoteException e) {
			System.out.println(e);
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public static Date getDate(String st) {

		try {
			StringTokenizer tknzr = new StringTokenizer(st, " ");
			String ct1 = tknzr.nextToken();
			String ct2 = tknzr.nextToken();
			StringTokenizer tkn = new StringTokenizer(ct1, "-");
			int year = Integer.parseInt(tkn.nextToken());
			year -= 1900;
			int month = Integer.parseInt(tkn.nextToken());
			month -= 1;
			int day = Integer.parseInt(tkn.nextToken());

			StringTokenizer str = new StringTokenizer(ct2, "-");
			int hour = Integer.parseInt(str.nextToken());
			int minute = Integer.parseInt(str.nextToken());
			int second = Integer.parseInt(str.nextToken());
			Date date = new Date(year, month, day, hour, minute, second);
			return date;
		} catch (Exception e) {

			return null;
		}

	}

}

⌨️ 快捷键说明

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