简体   繁体   English

运行服务器时出现错误

[英]Getting Error while running the server

I am creating a simplechat app with RMI, But I am not able recognize that where the exact problem is? 我正在用RMI创建一个simplechat应用程序,但是我不知道确切的问题在哪里? This is my code: 这是我的代码:

ChatServer.java ChatServer.java

import java.awt.Point;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nick
 */
public class ChatServer extends UnicastRemoteObject implements IChat{

    ArrayList<Point> list = new ArrayList<Point>();

    public ChatServer() throws RemoteException{
        try {
            Naming.bind("//localhost:1099/chat", this);
        } catch (Exception ex) {

            ex.printStackTrace();

        }

        System.out.println("Chat Server Ready");
    }




    public void put(Point p) throws RemoteException{

        p.getX();
        p.getY();

    }
 /*
    public ArrayList<Point> get() throws RemoteException{

        return list;

    } */
    public ArrayList<String> get() throws RemoteException
    {
        return list;
    }

    public static void main(String[] args) {
        try {
            new ChatServer();
        } catch (RemoteException ex) {
            Logger.getLogger(ChatServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}

ChatClient.java ChatClient.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author nick
 */
public class ChatClient extends JFrame implements Runnable, ActionListener, MouseListener, MouseMotionListener {

    String TAG = "kushal ";
    IChat ic;
    int oldx, oldy;
    JTextField jtf;
    JTextArea jta;

    public ChatClient() {
        try {
            Remote remote = Naming.lookup("//localhost:1099/chat");
            ic = (IChat) remote;
        } catch (Exception ex) {
            ex.printStackTrace();
        }



        setSize(300, 300);
        setVisible(true);

        Thread t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g) {
    }

    public void run() {
        while (true) {
            try {
                ArrayList<Point> list = ic.get();
                Thread.sleep(1000);
            } catch (Exception ex) {
                ex.printStackTrace();

            }

        }
    }

    public static void main(String[] args) {
        new ChatClient();
    }

    public void actionPerformed(ActionEvent e) {
      /*  try {
            ic.put(TAG + jtf.getText());
            jtf.setText("");
        } catch (RemoteException ex) {
            Logger.getLogger(ChatClient.class.getName()).log(Level.SEVERE, null, ex);
        }*/

    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        oldx = e.getX();
        oldy = e.getY();


    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        Graphics g = getGraphics();

        int x = e.getX();
        int y = e.getY();
        g.drawLine(oldx, oldy, x, y);
        oldx = x;
        oldy = y;


    }

    public void mouseMoved(MouseEvent e) {
    }
}

IChat.java IChat.java

import java.awt.Point;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author new
 */
public interface  IChat extends Remote{

    public void put(Point p) throws RemoteException;
    public ArrayList<Point> get() throws RemoteException;
}

Error in Netbeans log Netbeans日志中的错误

run:
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
Chat Server Ready
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)
    at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
    at java.rmi.Naming.bind(Naming.java:128)
    at ChatServer.<init>(ChatServer.java:26)
    at ChatServer.main(ChatServer.java:58)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:208)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 7 more

Any help would be greatful. 任何帮助将是巨大的。

Thanks in advance. 提前致谢。

In your server code, have a go at: 在您的服务器代码中,进行以下操作:

public ChatServer throws RemoteException
{
    Registry registry = LocateRegistry.getRegistry();
    registry.rebind( "chatserver" , this );
    ...
}

and then the client can do 然后客户可以做

...
Registry registry = LocateRegistry.getRegistry( "localhost" , 1099 );
IChat server = (IChat)registry.lookup( "chatserver" );
...

You haven't started the RMI Registry in the server host. 您尚未在服务器主机中启动RMI注册表。 You can use the rmiregistry.exe command, or start it in your code with LocateRegistry.createRegistry(). 您可以使用rmiregistry.exe命令,或者使用LocateRegistry.createRegistry()在代码中启动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM