Java 网上聊天

Java 网上聊天,java,rmi,Java,Rmi,我成功地让客户端向服务器发送对象,而服务器确实正确地回复了对象,但只回复了发送对象的客户端,我必须转发服务器端的端口,并允许服务器端的端口连接,现在我似乎无法向特定的客户端发送回复/消息,并且总是收到连接被拒绝的错误,不可能在客户端干预portforwardind/firewall,因为任何人都应该能够使用聊天(客户端必须是客户端,而不是服务器)。有没有办法让这一切顺利进行?我听说过http隧道或rmi代理,但它在代码方面是如何工作的 以下是我在客户端的主要代码: public class Ma

我成功地让客户端向服务器发送对象,而服务器确实正确地回复了对象,但只回复了发送对象的客户端,我必须转发服务器端的端口,并允许服务器端的端口连接,现在我似乎无法向特定的客户端发送回复/消息,并且总是收到连接被拒绝的错误,不可能在客户端干预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) ; }
    }
}
对于找到clientlist的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";}
    }

}
公共类MessageImpl
实现消息{
公共向量客户列表;
public MessageImpl()抛出远程异常{super();listec=new Vector();};
公共字符串envoiMessage()引发RemoteException{
返回(“消息测试”);
}
公共布尔值isNew(ClientInterface c)引发RemoteException
{
返回false;
}
公共字符串test()引发RemoteException
{
系统输出打印项次(“re”);
返回“测试”;
}
public void addUser(ClientInterface c)引发RemoteException
{   
test();
客户列表。添加(c);
}
公共无效广播(ClientInterface c,字符串消息)引发RemoteException
{
int i;

对于(i=0;i如果无法“干预”客户端防火墙,则您的系统无法按设计实现。您必须在客户端使用轮询而不是回调。

如何使用轮询?无论客户端请求多少,都应拒绝从服务器到客户端的连接?客户端不要求连接服务器请求连接。啊,好的,但是我怎么做?你能给我一个小例子吗?我已经回答了。客户端将不得不轮询服务器。