繁体   English   中英

互联网上的RMI聊天

[英]RMI chat over Internet

我设法使客户端将对象发送到服务器,并且服务器确实能够正确答复,但是仅发送给发送对象的客户端,我不得不在服务器端转发端口并允许在服务器端进行端口连接,现在我不能似乎向特定客户端发送了答复/消息,并且总是收到连接被拒绝的错误,不可能在客户端与portforwardind / firewall进行干预,因为任何人都应该可以使用聊天功能(客户端必须成为客户端,而不能成为客户端服务器)。 任何想法如何使这项工作? 我听说过HTTP隧道或rmi代理,但是它如何在代码方面起作用?

这是我在客户端的主要代码:

public class Main {

    public static void main(String [] args) {

        String input;
        Scanner in = new Scanner(System.in);
        input = in.nextLine();      
        try 
        {
            Message b =(Message) Naming.lookup("//xx.xx.xx.xx:1099/Message");
                Client c=new Client(input);
                UnicastRemoteObject.exportObject(c, 1100);
                b.addUser(c);
            while(true)
            {
                input = in.nextLine();
                if(input.contentEquals("deconnection"))
                {
                    b.exit();
                    break;
                }
                else if(input.startsWith(">"))
                {
                        b.broadcast(c,"test");
                }           
            }
            in.close(); 
        }
        catch (NotBoundException re) { System.out.println(re) ; }
        catch (RemoteException re) { System.out.println(re) ; }
        catch (MalformedURLException e) { System.out.println(e) ; }
    }
}

在服务器端:

public class Serveur 
{

    public static void main(String [] args) {

        try {
            MessageImpl objLocal = new MessageImpl();

            Naming.rebind("rmi://localhost:"+1099+"/Message" , UnicastRemoteObject.exportObject(objLocal, 1100)) ;

            System.out.println("Serveur pret"); 

        }
        catch (RemoteException re) { System.out.println(re) ; }
        catch (MalformedURLException e) { System.out.println(e) ; }
    }
}

与MessageImpl.java在哪里找到客户端列表:

public class MessageImpl 
    implements Message  {
    public Vector<ClientInterface> clientlist;

    public MessageImpl () throws RemoteException {super();listec=new Vector<ClientInterface>();};

    public String envoiMessage() throws RemoteException { 
        return( "message test" );
    }

    public boolean isNew(ClientInterface c)     throws RemoteException 
    {
    return false;   
    }

    public String test() throws RemoteException 
    {
        System.out.println("re");
        return "test";
    }

    public void addUser(ClientInterface c) throws RemoteException 
    {   
        test();
        clientlist.add(c);
    }

    public void broadcast(ClientInterface c,String message) throws RemoteException
    {
        int i;
        for(i=0;i<clientlist.size();i++)
        {
            if(clientlist.elementAt(i).getUsername().equals(c.getUsername()))
            {}
            else
            {
                clientlist.elementAt(i).getMessage(c.getUsername()+" : "+message);
                }
            }
        }


    public String exit() throws RemoteException
    {
        try{
           return "exiting messenger";  
        }
        catch(Exception e){return "erreur deconnection";}
    }

}

如果无法与客户端防火墙进行“干预”,则您的系统无法按设计实现。 您将不得不使用轮询而不是客户端上的回调。

暂无
暂无

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

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