Server.java
- import java.rmi.*;
public interface Server extends Remote{
public void send(String msg) throws RemoteException;
public void register(Client client, String name) throws RemoteException;
public void unregister(Client client, String name) throws RemoteException;
}
ServerImpl.java
- import java.rmi.server.*;
import java.rmi.*;
import java.util.*;
public class ServerImpl extends UnicastRemoteObject implements Server {
Vector clientList;
public ServerImpl() throws RemoteException {
super();
clientList = new Vector();
}
public synchronized void register(Client client, String name) throws RemoteException {
clientList.add(client);
send(name + "님이 입장하셨습니다.");
}
public void send(String msg) throws RemoteException {
synchronized(clientList) {
Enumeration e = clientList.elements();
while( e.hasMoreElements() ) {
Client c = (Client)e.nextElement();
c.back(msg);
}
}
}
public synchronized void unregister(Client client, String name) throws RemoteException {
clientList.removeElement(client);
send(name + "님이 퇴장하셨습니다.");
}
public static void main(String[] args) {
try {
ServerImpl s = new ServerImpl();
Naming.rebind("rmi://61.103.103.184/chat", s);
System.out.println("RMI Chat Server is ready.");
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
Client.java
- import java.rmi.*;
public interface Client extends Remote {
public void back(String msg) throws RemoteException ;
}
ClientImpl.java
- import java.awt.*;
import java.awt.event.*;
import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.*;
public class ClientImpl extends JFrame implements Client, ActionListener {
JTextArea display, display2;
JTextField input, id;
JLabel label, nick;
Server server;
JButton register, close, enter, clear;
String name, serverName;
CardLayout card;
JPanel npanel;
public ClientImpl() {
super("RMI 채팅");
Container c = getContentPane();
c.setLayout(new BorderLayout());
display = new JTextArea();
display.setEditable(false);
JScrollPane spane = new JScrollPane(display);
c.add(spane, "Center");
npanel = new JPanel();
card = new CardLayout();
npanel.setLayout(card);
label = new JLabel("대화명 : ");
id = new JTextField(10);
register = new JButton("등록");
register.addActionListener(this);
close = new JButton("종료");
close.addActionListener(this);
JPanel loginPanel = new JPanel();
loginPanel.add(label);
loginPanel.add(id);
loginPanel.add(register);
loginPanel.add(close);
nick = new JLabel("즐팅!!");
input = new JTextField(10);
input.addActionListener(this);
enter = new JButton("입력");
enter.addActionListener(this);
clear = new JButton("지우기");
clear.addActionListener(this);
JPanel chatPanel = new JPanel();
chatPanel.add(nick);
chatPanel.add(input);
chatPanel.add(enter);
chatPanel.add(clear);
npanel.add(loginPanel, "login");
npanel.add(chatPanel, "chat");
c.add(npanel, "North");
card.show(npanel, "login");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
if ( server != null ) {
try {
server.unregister(ClientImpl.this, name);
} catch ( Exception ee ) {
ee.printStackTrace();
}
}
dispose();
System.exit(0);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,500);
setVisible(true);
}
private void connect(){
try {
UnicastRemoteObject.exportObject(this);
server = (Server)Naming.lookup("rmi://61.103.103.184/chat");
server.register(this, name);
} catch ( Exception e ) {
e.printStackTrace();
}
}
public synchronized void back(String msg){
try {
display.append(msg + "\n");
} catch (Exception e) {
display.append(e.toString());
}
}
public void actionPerformed(ActionEvent e){
Object o = e.getSource();
if ( o == register ) {
name = id.getText().trim();
card.next(npanel);
connect();
nick.setText(name+"님, 즐팅!!");
} else if ( o == input || o == enter ) {
try {
server.send(name + " : " + input.getText());
input.setText("");
} catch ( Exception ex ) {
display.append(ex.toString());
}
} else if ( o == clear ) {
display.setText("");
} else if ( o == close ) {
System.exit(0);
}
}
public static void main(String[] args) {
new ClientImpl();
}
}
첫 실행화면
대화명 입력 후 화면
다른이가 입장하였을 경우의 화면
유저가 퇴장하였을 때 화면
이 글은 스프링노트에서 작성되었습니다.
'Java > Language' 카테고리의 다른 글
미니프로젝트 : VideoShop 프로그램 만들기(JTabbPanel 이용) (0) | 2009.06.12 |
---|---|
스크롤바를 하단부에 위치시키자. (0) | 2009.06.10 |
JAVA RMI, 기본 개념 정리 (2) | 2009.06.08 |
자바(JSP)를 이용한 암호화 기능 만들기 (2) | 2009.06.06 |
Javaone : Change (Y)our World : 우리의 세상을 바꿀 수 있을까? (0) | 2009.06.03 |