(자바 RMI 미니채팅프로그램)JAVA RMI를 이용한 미니 채팅프로그램, 원격객체, 원격인터페이스, RMI서버, 클라이언트 …

(자바 RMI 미니채팅프로그램)JAVA RMI를 이용한 미니 채팅프로그램, 원격객체, 원격인터페이스, RMI서버, 클라이언트 구현 

 

package chat.server;

 

import java.rmi.Remote;

import java.rmi.RemoteException;

 

import chat.client.Client;

 

/**

 * 클라이언트에서 호출할 원격메소드를 정의한 원격인터페이스

 * 

 * @author jclee

 */

public interface Server extends Remote {

public void setClient(Client client) throws RemoteException;

 

public void setMessage(String msg) throws RemoteException;

}




------------------------------------------------------------

package chat.server;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import chat.client.Client;

public class ServerImpl extends UnicastRemoteObject implements Server {

private static ArrayList<Client> clients = new ArrayList<Client>();

public ServerImpl() throws RemoteException {
}

public void setClient(Client client) throws RemoteException {
clients.add(client);
}

public void setMessage(String msg) throws RemoteException {
System.out.println(">>>>>>>>>>>>>>>> " + msg);
for (Client client : ServerImpl.clients) {
// 클라이언트의 원격메소드 setMessage를 호출하여 글을 보낸다.
client.setMessage(msg);
}
}

public static void main(String[] args) throws Exception {
ServerImpl serverImpl = new ServerImpl();
Naming.rebind("chatServer", serverImpl);
System.out.println("Chatting Server Executed...");
}
}



-------------------------------------------------------------------

package chat.client;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
 * 서버가 호출하여 메소드를 전달할 원격메소드 정의
 * @author jclee
 *
 */
public interface Client extends Remote{
public void setMessage(String msg) throws RemoteException;
}




-------------------------------------------------------------


package chat.client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import chat.server.Server;

public class ClientImpl extends UnicastRemoteObject implements Client {
public ClientImpl() throws RemoteException {
}

public void setMessage(String msg) throws RemoteException {
System.out.println(msg);

}

public static void main(String[] args) throws Exception {
ClientImpl clientImpl = new ClientImpl();
Server server = (Server) Naming.lookup("rmi://localhost/chatServer");

server.setClient(clientImpl);

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String msg = in.readLine();

// 채팅 메시지를 원격메소드인 setMessage로 원격 서버에 전달
if (!"".equals(msg)) server.setMessage("[메시지] " + msg);
}
}
}

실행방법

1. 서버에서(이클립스 워크스페이스 : C:\dev\workspace, 프로젝트 명칭 : rmi)
C:\dev\workspace\rmi\bin>rmic chat.server.ServerImpl
C:\dev\workspace\rmi\bin>start rmiregistry 1099
C:\dev\workspace\rmi\bin>java chat.server.ServerImpl

2. 클라이언트
C:\dev\workspace\rmi\bin>java chat.client.ClientImpl



267db3df7e6ea04a1af19780fb35b4ea_1599887


267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892
267db3df7e6ea04a1af19780fb35b4ea_1599892


#RMI채팅, #자바채팅, #JAVA채팅, #채팅소스, #채팅강좌, #자바채팅소스, #자바, #JAVA, #자바동영상, #JAVA동영상, #JAVA채팅영상, #자바ㅑ채팅예제, #자바교육, #자바강의, #JAVA강의, #JAVA교육, #자바RMI, #RMI채팅

Comments