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

📄 rmiserver.java

📁 rmi C/S 客 户 端 与 服 务 端 连 接
💻 JAVA
字号:
package com.topking.rt.rmi.server.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.UnknownHostException;
import java.rmi.registry.LocateRegistry;

public class RMIServer implements Runnable{

	/**
	 * @param args
	 */
	private int serverPort = -1;
	private String serverURL = null;
	
	public void loadConfigFile(){
		try {
			String host = InetAddress.getLocalHost().getHostAddress();
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("config/rmi-config.txt")));
			String line = null;
			String serverName = null;
			System.out.println("ConfigFile loading......");			
			while((line= reader.readLine())!=null){				
				if(line.startsWith("RMI_ServerPort")){
					serverPort = Integer.parseInt(line.split(":")[1]);
				}
				if(line.startsWith("RMI_ServerName")){
					serverName = line.split(":")[1];
				}
			}
			serverURL = "//"+host+":"+serverPort+"/"+serverName;
			System.out.println("loadConfigFile Success");	
			System.out.println("serverURL : "+serverURL);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public  void startServer() {
		// TODO Auto-generated method stub
		loadConfigFile();
		try {
			System.out.println("RMI Server starting......");
			LocateRegistry.createRegistry(serverPort);
			RMIInterfaceImpl server = new RMIInterfaceImpl();			
			Naming.rebind(serverURL, server);//将服务类和URL绑定到命名空间			
			System.out.println("RMI Server Started");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void run(){		
		startServer();
	}
	
	public static void main(String[] args) {
		Thread th = new Thread(new RMIServer());
		th.setDaemon(true);
	}

}

⌨️ 快捷键说明

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