2020.11.10 고급자바 javaRmiChatClient , javaRmiChatServer 수업노트

 인터페이스 부분 


1) ClientChatInf 


package kr.or.ddit.inf;


import java.rmi.Remote;

import java.rmi.RemoteException;


public interface ClientChatInf extends Remote {


//서버가 보내온 메시지를 화면에 출력하는 메서드 

public void printMessage (String msg) throws RemoteException; 

}


2) ServerChatInf




package kr.or.ddit.inf;

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

public interface ServerChatInf extends Remote{
//접속한 클라이언트 정보를 List에 추가하는 메소드
public void setClient(ClientChatInf client) throws RemoteException; 
//접속이 해제된 클라이언트 정보를 List 에서 삭제하는 메서드 
public void disConnect(ClientChatInf client) throws RemoteException; 
//하나의 클라이언트가 보내온 메시지를 List 에 등록된 모든 클라이언트에게 전달하는 메서드
public void sednToAll(String msg) throws RemoteException; 

}




클라이언트 부분



package kr.or.ddit.main;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Scanner;

import kr.or.ddit.inf.ClientChatInf;
import kr.or.ddit.inf.ServerChatInf;

public class RmiChatClient extends UnicastRemoteObject implements ClientChatInf {

// 생성자

public RmiChatClient() throws RemoteException {
}

// 서버가 보내온 메시지를 화면에 출력하는 메서드
@Override
public void printMessage(String msg) throws RemoteException {
System.out.println(msg);

}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

try {

// 클라이언트 객체를 생성한다.
ClientChatInf client = new RmiChatClient();

Registry reg = LocateRegistry.getRegistry("localhost", 1099);

// 서버에 접속하기
ServerChatInf server = (ServerChatInf) reg.lookup("rmiChat");

// 서버에 접속되면 현재 클라이언트 정보를 서버에 등록한다.
server.setClient(client);

while (true) {
// 메시지를 입력 받아서 채팅 작업을 수행한다.
String msg = scan.nextLine();
if ("/end".equals(msg)) {// 채팅방 빠져나오기
server.disConnect(client); // 서버에서 현재 클라이언트 정보를 삭제한다.
break; //반복문 탈출
}
server.sednToAll(msg);// 모든클라이언트에게 메세지를 보내는 서버의 메서드 호출 
}
server.disConnect(client); //서버에서 현재 클라이언트 정보를 삭제한다. 
//RMI 로 연결된 클라이언트와 서버의 접속을 끝내기

UnicastRemoteObject.unexportObject(client, true);
} catch (RemoteException e) {
// TODO: handle exception
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}



서버부분 RmiChatServer 


package kr.or.ddit.main;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import kr.or.ddit.inf.ClientChatInf;
import kr.or.ddit.inf.ServerChatInf;

//서버용 인터페이스를 구현한 클래스
public class RmiChatServer extends UnicastRemoteObject implements ServerChatInf {

// 접속한 클라이언트 정보가 저장될 List
List<ClientChatInf> clientList = new ArrayList<>();

// 생성자
public RmiChatServer() throws RemoteException {
}

//접속한 클라이언트 정보를 List 에 추가하는 메서드 
@Override
public void setClient(ClientChatInf client) throws RemoteException {
clientList.add(client);
System.out.println("등록완료...");
System.out.println("현재 접속자 수 : " + clientList.size() + "명");

}
//접속을 해제한 클라이언트 정보를 List 에서 삭제하는 메서드 

@Override
public void disConnect(ClientChatInf client) throws RemoteException {
// TODO Auto-generated method stub

clientList.remove(client); 
System.out.println("접속해제 완료....");
System.out.println("현재 접속자 수 : " + clientList.size() + "명");
}

//List 에 등록된 모든 클라이언트에게 메세지 보내기 
@Override
public void sednToAll(String msg) throws RemoteException {
for (ClientChatInf client : clientList) {
client.printMessage(msg);
}

}

public static void main(String[] args) {
try {
ServerChatInf server = new RmiChatServer();
Registry reg = LocateRegistry.createRegistry(1099); 
reg.rebind("rmiChat", server);
System.out.println("채팅 서버 준비 완료");
} catch (RemoteException e) {
// TODO: handle exception
}

}

}






댓글