dnsservermain.java

来自「实现了一个由时间服务器」· Java 代码 · 共 72 行

JAVA
72
字号
package dnsServer;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class DnsServerMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		ServiceList list=new ServiceList();
		DnsServerMain.writeUserListToFile(list);
		InetAddress ip;
		String url;
		try {
			ip = InetAddress.getLocalHost();
			url = ip.getHostAddress();
			System.out.println("目录服务器的地址为:"+url+":1099");
			String ServiceURL = "rmi://"+url+":1099/DnsServerService";
			try {
				// 建立服务对象
				DnsServerServiceImpl impl = new DnsServerServiceImpl();
				// 启动注册表
				LocateRegistry.createRegistry(1099);
				// 绑定
				Naming.rebind(ServiceURL, impl);
				System.out.println("目录服务器正在运行...");
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
		
	}
	
	public static void writeUserListToFile(ServiceList list){
		ObjectOutputStream oos;
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("serviceList.txt",false);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(list);
			oos.flush();
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

⌨️ 快捷键说明

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