新学rmi,无法通讯,不理解!!
接口::
package server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Chat_server extends Remote {
public void show(String message) throws RemoteException;
}package client;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Chat_client extends Remote {
public void show(String message) throws RemoteException;
}
接口实现::
package server;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JButton;import javax.swing.JFrame;
import javax.swing.JTextArea;import client.Chat_client;public class Server extends UnicastRemoteObject implements Chat_server{ private JTextArea send;
private JTextArea show;
private JFrame frame;
private Chat_client client; /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Server window = new Server();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the application
*/
public Server() throws RemoteException{
initialize();
} /**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(128, 128, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 301, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
register();
}catch(Exception ee){
}
}
});
button.setText("注册");
button.setBounds(10, 10, 99, 23);
frame.getContentPane().add(button); show = new JTextArea();
show.setBounds(10, 50, 269, 128);
frame.getContentPane().add(show); send = new JTextArea();
send.setBounds(10, 214, 269, 88);
frame.getContentPane().add(send); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
send();
}catch(Exception eee){
}
}
});
button_1.setText("发送");
button_1.setBounds(10, 308, 99, 23);
frame.getContentPane().add(button_1);
}
public void send(){
String message=send.getText();
try{
show.append("server:"+message+"\n");
client.show(message);
}catch(Exception e){
}
}
public void show(String message) throws RemoteException{
show.append(message);
}
public void register()throws Exception{
// System.setSecurityManager(new RMISecurityManager());
Server server=new Server();
Naming.rebind("Chat_server", server);
show.append("ready to do\n");
}}
package client;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JButton;import javax.swing.JFrame;
import javax.swing.JTextArea;import server.Chat_server;public class Client extends UnicastRemoteObject implements Chat_client{ private JTextArea send;
private JTextArea show;
private JFrame frame;
private Chat_server server; /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Client window = new Client();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the application
*/
public Client() throws RemoteException{
initialize();
} /**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(128, 128, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 317, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
connect();
}catch(Exception ee){
}
}
});
button.setText("连接");
button.setBounds(10, 12, 99, 23);
frame.getContentPane().add(button); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
send();
}catch(Exception eee){
}
}
});
button_1.setText("发送");
button_1.setBounds(10, 291, 99, 23);
frame.getContentPane().add(button_1); show = new JTextArea();
show.setBounds(10, 41, 281, 92);
frame.getContentPane().add(show); send = new JTextArea();
send.setBounds(10, 206, 281, 79);
frame.getContentPane().add(send);
}
public void send(){
String message=send.getText();
try{
show.append("client:"+message+"\n");
server.show(message);
}catch(Exception e){
}
}
public void show(String message) throws RemoteException{
show.append(message);
}
public void connect() throws Exception{
// System.setSecurityManager(new RMISecurityManager());
server=(Chat_server)Naming.lookup("Chat_server");
show.append("successfull\n");
server.show("hello");
}}
接口::
package server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Chat_server extends Remote {
public void show(String message) throws RemoteException;
}package client;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Chat_client extends Remote {
public void show(String message) throws RemoteException;
}
接口实现::
package server;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JButton;import javax.swing.JFrame;
import javax.swing.JTextArea;import client.Chat_client;public class Server extends UnicastRemoteObject implements Chat_server{ private JTextArea send;
private JTextArea show;
private JFrame frame;
private Chat_client client; /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Server window = new Server();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the application
*/
public Server() throws RemoteException{
initialize();
} /**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(128, 128, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 301, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
register();
}catch(Exception ee){
}
}
});
button.setText("注册");
button.setBounds(10, 10, 99, 23);
frame.getContentPane().add(button); show = new JTextArea();
show.setBounds(10, 50, 269, 128);
frame.getContentPane().add(show); send = new JTextArea();
send.setBounds(10, 214, 269, 88);
frame.getContentPane().add(send); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
send();
}catch(Exception eee){
}
}
});
button_1.setText("发送");
button_1.setBounds(10, 308, 99, 23);
frame.getContentPane().add(button_1);
}
public void send(){
String message=send.getText();
try{
show.append("server:"+message+"\n");
client.show(message);
}catch(Exception e){
}
}
public void show(String message) throws RemoteException{
show.append(message);
}
public void register()throws Exception{
// System.setSecurityManager(new RMISecurityManager());
Server server=new Server();
Naming.rebind("Chat_server", server);
show.append("ready to do\n");
}}
package client;import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.JButton;import javax.swing.JFrame;
import javax.swing.JTextArea;import server.Chat_server;public class Client extends UnicastRemoteObject implements Chat_client{ private JTextArea send;
private JTextArea show;
private JFrame frame;
private Chat_server server; /**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Client window = new Client();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* Create the application
*/
public Client() throws RemoteException{
initialize();
} /**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(new Color(128, 128, 255));
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 317, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
connect();
}catch(Exception ee){
}
}
});
button.setText("连接");
button.setBounds(10, 12, 99, 23);
frame.getContentPane().add(button); final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try{
send();
}catch(Exception eee){
}
}
});
button_1.setText("发送");
button_1.setBounds(10, 291, 99, 23);
frame.getContentPane().add(button_1); show = new JTextArea();
show.setBounds(10, 41, 281, 92);
frame.getContentPane().add(show); send = new JTextArea();
send.setBounds(10, 206, 281, 79);
frame.getContentPane().add(send);
}
public void send(){
String message=send.getText();
try{
show.append("client:"+message+"\n");
server.show(message);
}catch(Exception e){
}
}
public void show(String message) throws RemoteException{
show.append(message);
}
public void connect() throws Exception{
// System.setSecurityManager(new RMISecurityManager());
server=(Chat_server)Naming.lookup("Chat_server");
show.append("successfull\n");
server.show("hello");
}}
解决方案 »
- 菜鸟一枚。渲染树的时候出现奇怪错误~急求~!!
- 怎么无法使用connection类型?
- class如何防止反编译?
- 关于IO流问题,怎样用另一种方式去实现,请指教。
- 做jsp页面 什么时候才能用到线程?
- java 小程序的问题
- 大哥们这是我最后30分了,全给了!是关于JTree的一个很简单的问题
- 如何使JTree中的跟节点不显示(急急急)
- jbuilder7在使用威盛CPU的机器上无法安装的问题
- 一个有关程序包的问题!
- 用jbx,如何在上面放入控件并对它进行控制啊,又在那里写你的控制代码呢?
- 花了2天时间写了个多线程网络爬虫,可以通过配置文件设定根网址和需要过滤的网站,以及保存的目录,JAVA版本的
就不用再使用rmi命令启动了
可能咱俩环境不一样