java RMI Chat - 远程服务器不执行代码

时间:2016-12-02 15:14:16

标签: java server localhost rmi

我的聊天在本地主机上工作正常但是当我尝试在远程服务器上执行时,它不会继续。

RemoteClass>

import Interface.ChatInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class ChatClient extends UnicastRemoteObject implements ChatInterface{

    private String name;
    private ChatInterface server;

    public ChatClient(String name) throws RemoteException{
        this.name = name;
    }

    @Override
    public String getName() throws RemoteException {
        return this.name;
    }

    @Override
    public void sendMsg(String message) throws RemoteException {
        System.out.println(message);
    }

    @Override
    public void setInterface(ChatInterface server) throws RemoteException {
        this.server = server;
    } }

ClientImpl>

import Interface.ChatInterface;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class Client{

    public static void main(String[] args){
        if (System.getSecurityManager() == null) {
           System.setSecurityManager(new SecurityManager());
        }
        try {
            Registry registry = LocateRegistry.getRegistry("hostname", port);

            ChatInterface chatClient = new ChatClient("User01");
            ChatInterface chatServer = (ChatInterface) registry.lookup("ChatServer");

            System.out.println("[System] Chat is ready");

            chatServer.setInterface(chatClient);
            String message = "["+chatClient.getName()+"] got connected";
            chatServer.sendMsg(message);

            Scanner s = new Scanner(System.in);
            while (true) {                
                message = s.nextLine().trim();
                message = "["+ chatClient.getName()+"]" + message;
                chatServer.sendMsg(message);
            }
        } catch (Exception e) {
            System.out.println("[System] Server failed: " + e);
            System.exit(1);
        }
    } }

ClientMain>

import Interface.ChatInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

public class ChatServer extends UnicastRemoteObject implements ChatInterface{

    private String name;
    private ArrayList<ChatInterface> clients;

    public ChatServer(String name)throws RemoteException{
        this.name = name;
        this.clients = new ArrayList<>();
    }

    @Override
    public String getName() throws RemoteException {
        return this.name;
    }

    @Override
    public void sendMsg(String message) throws RemoteException {
        broadcast(message);
    }

    @Override
    public synchronized void setInterface(ChatInterface client) throws RemoteException {
        clients.add(client);
    }

    public ArrayList<ChatInterface> getClients(){
        return clients;
    }

    private void broadcast(String message) throws RemoteException {
        for (ChatInterface client : clients) {
            client.sendMsg(message);
        }
    } }

ServerImpl&gt;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {

    public static void main(String[] args) {
        try {
            LocateRegistry.createRegistry(9876);
            Registry registry = LocateRegistry.getRegistry(port);

            ChatServer chatServer = new ChatServer("Server");
            registry.bind("ChatServer", chatServer);

            System.out.println("Server ready :)");
        } catch (Exception e) {
            System.out.println("[System] Server failed: " + e.toString());
            System.exit(1);
        }
    }
}

ServerMain&gt;

chatServer.setInterface(chatClient);

服务器在此行没有做任何事情

.navbar {
 position: relative;
 }

$(document).ready(function(){
    $(window).scroll(function(){
        if ($(window).scrollTop() > 100){
            $('.navbar').css('position','fixed');
        }
        else{
            $('.navbar').css('position','relative');
        }
    });
});

但不会引发任何错误。它继续为客户和客户提供服务。服务器

如果您有任何建议,谢谢

0 个答案:

没有答案