質問

I managed to make the client send objects to the server and the server does reply correctly but only to the client who sent the object, I had to forward ports on the server side and allowed port connections on the server side, now I can't seem to send a reply/message to a specific client and always get a connection refused error, meddling with portforwardind/firewall on the client side is not possible since anyone should be able to use the chat(the client must stay a client and not become a server). Any ideas how to make this work ? I heard about http tunneling or rmi proxy but how does it work code-wise ?

here's my main code on the client side :

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) ; }
    }
}

on the server side :

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) ; }
    }
}

with the MessageImpl.java where the clientlist is found :

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";}
    }

}
役に立ちましたか?

解決

If 'meddling' with the client firewall isn't possible, your system is unimplementable as designed. You would have to use polling instead of callbacks on the client side.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top