Port ing between the Intranet and the Internet during Remote calling of RMI (how to penetrate the firewall for remote calling of RMI)

Source: Internet
Author: User

Reprinted from: http://www.juuluu.org/html/softDoc/2011/10/19/09/45/wtpnkjxlwt.html

The following are the implementation Notes for an Rmi project:

Environment: Firewall (XX guardian) Internet IP Address: x. x. x.135 port: 8400/8500 Intranet IP Address: x. x. x.90 port: 8400/8500

The Intranet and Internet IP addresses and ports are mapped to each other. (the Internet IP addresses are configured on the firewall and configured by the Administrator)

 

1. the RMI remote call service is registered on port 8400 of the Intranet (stub/skeleton is not required, and the default 1099 is not used)

// Locateregistry. createregistry (rmiserverport); // define service registration and search for service port 8400

2. The data communication port of RMI Remote Call is bound to port 8500 of the Intranet.

// Rmisocketfactory. setsocketfactory (New smrmisocket (); // defines data transmission port 8500

3. Set the access to the Internet: x. x. x.135: 8400 when the firewall is used to access the Intranet: x. x. x.90: 8400

// When sun. RMI. server. unicastref is called, the Service IP address is: x. x. x.135, instead of x. x. x.90 (the customer's local RMI service does not exist)

// System. setproperty ("Java. RMI. server. hostname", "x. x. x.135 ");

 

4. the RMI call to penetrate the firewall is not automatic and needs to be manually specified, as indicated by 3. Without configuration, the RMI service bound to the Intranet IP address cannot be accessed through the Internet IP address.

 

5. through this project, we understand that RMI can directly start the RMI service without creating stub/skeleton by using rmic or starting rmiregistry through a program, which facilitates code modification and project implementation.

 

Because it is a project code, you do not need to package and upload the code. The abbreviation is as follows:

////// RMI server code

String hostip = inetaddress. getlocalhost (). gethostaddress ();
Int rmiserverport = 8400; // The data transmission service is 8400.
String bindname = "licenseserver ";
// The following line of code should not be less; otherwise, when the vro x. x. x.135 maps to the Intranet IP address x. x. x.90,
// When accessing the RMI service, it directs to the local x. x. x.90. Then, the client accesses the local x. x. x.90,
// This is an absolute error. The service is behind the public network router (including public IP) and is not in the customer's local
System. setproperty ("Java. RMI. server. hostname", "x. x. x.135 ");
Try {
Rmisocketfactory. setsocketfactory (New smrmisocket (); // defines data transmission port 8500
Locateregistry. createregistry (rmiserverport); // define service registration and search for service port 8400
}
Catch (exception ex ){
System. Out. println ("server port binding error:" + ex. getmessage ());
Ex. printstacktrace ();
}
// Create the service object of the license Generator
Makelicense LicenseService = new makelicenseservice ();
// Bind a service object to a service port
// URL format (without the scheme component)
String bindurl = "//" + hostip + ":" + rmiserverport + "/" + bindname;
Naming. rebind (bindurl, LicenseService );
System. Out. println (bindurl + "server is ready .");

 

/// Smrmisocket. Java

Import java. RMI. server .*;
Import java. Io .*;
Import java.net .*;

Public class smrmisocket
Extends rmisocketfactory {

Public socket createsocket (string host, int port) throws ioexception {
Return new socket (host, Port );
}

Public serversocket createserversocket (INT port) throws ioexception {
If (Port = 0)
Port = 8500;

System. Out. println ("RMI server registration and data transmission Port =" + port );
Return new serversocket (port );
}

}

/// Client code:

Private string remotehost = "x. x. x.135"; // public IP address or lan ip Address
Private int rmiserverport = 8400; // search for service port 8400
Private string bindname = "licenseserver"; // RMI service name
Private int revcount = 0;
Private makelicense remoteobject = NULL;
Public licenserequestclient (){
Try {
// If (system. getsecuritymanager () = NULL ){
// System. setsecuritymanager (New rmisecuritymanager ());
//}
String bindurl = "//" + remotehost + ":" + rmiserverport + "/" + bindname;
System. Out. println ("requested remote service url =" + bindurl );
Makelicense remoteobject = (makelicense) Naming. Lookup (bindurl );
This. remoteobject = remoteobject;
// System. Out. Print ("remote remoteobject =" + remoteobject );
}
Catch (RemoteException re ){
System. Out. println ("RemoteException:" + RE );
}
Catch (notboundexception NBE ){
System. Out. println ("notboundexception:" + NBE );
}
Catch (malformedurlexception MfE ){
System. Out. println ("malformedurlexception:" + MfE );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.