Загрузка...

(자바 RMI 채팅프로그램)JAVA RMI를 이용한 콘솔기반 채팅프로그램, 원격객체, 원격인터페이스, RMI서버, RMI클라이언트 구현(자바강의/자바동영상/자바교육/JAVA/자바)

RMI를 이용한 콘솔기반 채팅 실습 영상 입니다. 천천히 한번 따라해 보세요~
감사합니다...

실행방법

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

package chat.server;

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

import chat.client.Client;


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

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 {

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

}
}
}

Видео (자바 RMI 채팅프로그램)JAVA RMI를 이용한 콘솔기반 채팅프로그램, 원격객체, 원격인터페이스, RMI서버, RMI클라이언트 구현(자바강의/자바동영상/자바교육/JAVA/자바) канала 오라클자바커뮤니티
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять