我一直在学校做一个RMI聊天系统,想要与它共享某种文件。之前没有做过这样的事情而且在学校没有这样做,但我想尝试一下。在谷歌和这个做了一些搜索 http://java.sun.com/developer/technicalArticles/RMI/rmi_corba/ 是我找到的最好的。这个“技术”是新的,所以我可以使用它吗?没有让它工作,但这可能是我缺乏Java知识。 一些提示为什么我不能让它工作?
答案 0 :(得分:0)
来回发送邮件和发送文件之间没有太大的区别。答案很大程度上取决于您当前的代码是什么样的。你有一些代码要分享吗?
您当前的RMI界面是什么样的?你的架构是什么样的?
您是否有客户端连接的服务器?
你有方法sendChat(String message)?
关于sendChat(),你可以添加一个sendFile(...)。那么问题就是你想为参数做些什么。
您要发送java.io.File对象吗?一个byte [](以及一个String名称,可能是一个区分二进制文件的布尔标志)?
看到这是你第一次这样做,你可能想要自己编写世俗的部分。但是,从文件中获取字节等任务是相当常见的烦恼。 Apache commons和Google guava库都提供了简化流程的方法。我想这取决于你对这个项目的学习目标。
答案 1 :(得分:0)
是。客户端连接到服务器,并能够在聊天室中聊天。
以下是我们迄今为止的一些代码,除了用于连接和与数据库通信的文件以及GUI之外。谢谢你回答。
Chatfront界面:
public interface ChatFront extends Remote {
boolean registerClient(Client client, String password) throws RemoteException;
void logMeOut(Client client) throws RemoteException;
void newChannelMessage(String toChannel, String fromUser, String message) throws RemoteException;
void newPrivateMessage(String fromUser, String toUser, String message) throws RemoteException;
String connectChannel(String username, String channel) throws RemoteException;
String disconnectChannel(String username, String channel) throws RemoteException;
boolean isUserRegistered(String username) throws RemoteException;
void errorMessage(String toUser, String message) throws RemoteException;
}
客户端界面:
public interface Client extends Remote {
String findName() throws RemoteException;
void newPrivateMessage(String fromUser, String toUser, String message) throws RemoteException;
void newMessageChannel(String toChannel, String fromUser, String message) throws RemoteException;
void newSystemMessageChannel(String toChannel, String fromUser, String message) throws RemoteException;
void errorMessage(String toUser, String message) throws RemoteException;
}
<强>了Serverside:强>
class ChatServer {
public static void main(String[] args) throws Exception {
String objectname = "chatter";
LocateRegistry.createRegistry(1099);
ChatFront cf = new ChatFrontImpl();
Naming.rebind(objectname, cf);
javax.swing.JOptionPane.showMessageDialog(null, "Tjener kjører. Trykk OK for å avslutte tjeneren.");
Naming.unbind(objectname);
System.exit(0);
}
}
ChatFrontImpl.java
package server;
import Interfaces.ChatFront;
import Interfaces.Client;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
/**
*
* @author sindre
*/
class ChatFrontImpl extends UnicastRemoteObject implements ChatFront {
private UserDAC u = new UserDAC();
private Hashtable<String, ArrayList<String>> channels = new Hashtable<String, ArrayList<String>>();
private ArrayList<Client> clients = new ArrayList<Client>();
public ChatFrontImpl() throws RemoteException {
}
/* Registrerer en ny client */
@Override
public synchronized boolean registerClient(Client client, String password) throws RemoteException {
if(!u.logIn(client.findName(), password)){
System.out.println("feil username eller password");
return false;
}
if (!clients.contains(client)) {
try {
clients.add(client);
System.out.println("Na er " + client.findName() + " registrert.");
} catch (Exception e){
System.out.println("Feil oppstatt i registrerMeg(): " + e);
}
return true;
} else
return false;
}
/* Kobler en client til en kanal */
@Override
public String connectChannel(String username, String kanal) throws RemoteException{
if(isUserRegistered(username)){
if (!channels.containsKey(kanal)) {
String message = "Bruker " + username + " har ankommet kanalen";
channels.put(kanal, new ArrayList<String>());
channels.get(kanal).add(username);
notifyChannelSystem(kanal, "SYSTEM", message);
notifySelf(username, "Skriv /? for mulige kommandoer");
return("Kanal opprettet, og bruker satt i kanalen.");
}
else{
if(channels.get(kanal).contains(username)){
return ("Brukeren er allerede i kanalen.");
} else {
channels.get(kanal).add(username);
String message = "Bruker " + username + " har ankommet kanalen";
notifyChannelSystem(kanal, "SYSTEM", message);
return ("Kanal fantes allerede. Bruker satt i kanalen.");
}
}
}
return "";
}
/*Sjekker om brukeren ikke er blank.*/
@Override
public boolean isUserRegistered(String username){
if(username.equals("")){
return false;
}
return true;
}
/* Kobler en bruker fra en kanal */
@Override
public String disconnectChannel(String username, String channel) throws RemoteException{
if(isUserInChannelX(username, channel)){
channels.get(channel).remove(username);
String message = "Bruker " + username + " har forlatt kanalen.";
notifySelf(username, " Du har nå forlatt kanalen " + channel);
notifyChannelSystem(channel, "SYSTEM", message);
if(channels.get(channel).isEmpty()){
channels.remove(channel);
}
}
return ("Bruker har forlatt kanalen.");
}
/* Kobler en client fra clientlisten */
@Override
public synchronized void logMeOut(Client client) throws RemoteException {
boolean isfound = false;
int clientIndeks = 0;
while (clientIndeks < clients.size() && !isfound) {
Client denne = clients.get(clientIndeks);
if (denne.equals(client)) { // bruker equals() for a sammenlikne stubbobjektene
isfound = true;
clients.remove(clientIndeks);
System.out.println("Na er clienten " + client.findName() + " fjernet.");
} else clientIndeks++;
}
}
/* Sjekker om brukeren finnes i en gitt kanal */
public boolean isUserInChannelX(String bruker, String kanal){
if(channels.containsKey(kanal) && channels.get(kanal).contains(bruker)){
return true;
} else {
return false;
}
}
/* Kaller metoden varsleKanal()*/
@Override
public void newChannelMessage(String tilKanal, String fraBruker, String message) throws RemoteException{
if(isUserInChannelX(fraBruker, tilKanal)){
notifyChannel(tilKanal, fraBruker, message);
}
else{
}
}
/* Kaller metoden varsleEn() */
@Override
public void newPrivateMessage(String fra, String toUser, String message) throws RemoteException{
notifyUser(fra,toUser,message);
}
@Override
public void errorMessage(String toUser, String message) throws RemoteException{
notifySelf(toUser, message);
}
private void notifySelf(String toUser, String message) throws RemoteException{
for(Client k: clients){
if(k.findName().equals(toUser)){
k.errorMessage(toUser, message);
}
}
}
/* Bruker metoden nymessagePrivat() + ser om brukeren er tilkoblet*/
private void notifyUser(String fromUser, String toUser, String message) throws RemoteException{
boolean funnet = false;
for(Client k : clients){
if(k.findName().equals(toUser)){
k.newPrivateMessage(fromUser, toUser, message);
funnet = true;
}
}
if (!funnet) errorMessage(fromUser, "Ingen brukere ved dette navnet!");
}
/* Kjører metoden nymessageKanal() */
private void notifyChannel(String toChannel, String fromUser, String message) throws RemoteException{
if(channels.containsKey(toChannel) && isUserRegistered(fromUser)){
for(String k : channels.get(toChannel)){
for(Client cli : clients){
if(cli.findName().equals(k)){
cli.newMessageChannel(toChannel, fromUser, message);
}
}
}
}
else{
System.out.println("Brukeren er ikke registrert.");
}
}
private void notifyChannelSystem(String toChannel, String fromUser, String message) throws RemoteException{
if(channels.containsKey(toChannel) && isUserRegistered(fromUser)){
for(String k : channels.get(toChannel)){
for(Client kli : clients){
if(kli.findName().equals(k)){
kli.newMessageChannel(toChannel, fromUser, message);
}
}
}
}
else{
System.out.println("Brukeren er ikke registrert.");
}
}
}
<强>客户机侧:强>
GUILogic.java
package gui;
import Interfaces.ChatFront;
import Interfaces.Client;
import java.rmi.Naming;
import java.rmi.RemoteException;
import klient.ClientImpl;
/**
*
* @author sindre
*/
public class GUILogic {
GUI gui;
ChatFront cf;
public String username;
public String channel;
public String password;
public String message;
public GUILogic(GUI gui){
this.gui = gui;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/* Kobler GUI klienten opp mot serveren */
public void createCFConnection(String username, String password) throws Exception{
String objectname = "chatter";
String hostname = "localhost";
String url = "rmi://" + hostname + "/" + objectname;
cf = (ChatFront) Naming.lookup(url);
Client k = new ClientImpl(username, gui);
cf.registerClient(k, password);
}
public void connectChannel(String username, String channel) throws RemoteException{
cf.connectChannel(username, channel);
}
public void disconnectChannel(String username, String channel) throws RemoteException{
cf.disconnectChannel(username, channel);
}
public void newChannelMessage(String toChannel, String fromUser, String message) throws RemoteException{
cf.newChannelMessage(fromUser ,toChannel, message);
}
public void newPrivateMessage(String fromUser, String toUser, String message) throws RemoteException{
cf.newPrivateMessage(fromUser, toUser, message);
}
public void errorMessage(String toUser, String message) throws RemoteException{
cf.errorMessage(toUser, message);
}
/* Logger klienten av GUIen */
public boolean logOut(String username) throws RemoteException{
Client k = new ClientImpl(username, gui);
if(k.findName().equals("")){
return false;
} else {
cf.logMeOut(k);
System.out.println("user " + username + " logget ut.");
return true;
}
}
/* Analysermessage har funnet / i starten av messageen, og da blir denne metoden kjørt. */
public String commandWritten(String username, String channel, String message) throws RemoteException{
String[] result = message.split(" ", 3);
String channel1;
if((result[0].equalsIgnoreCase("/join") || (result[0].equalsIgnoreCase("/j")))){
channel1 = result[1];
setChannel(channel1);
connectChannel(username, channel1);
}
else if(result[0].equalsIgnoreCase("/leave") || (result[0].equalsIgnoreCase(("/l")))){
channel = result[1];
disconnectChannel(username, channel);
}
else if(result[0].equalsIgnoreCase("/whisper") || (result[0].equalsIgnoreCase(("/w")))){
for (int x=2; x<result.length; x++)
newPrivateMessage(username, result[1], result[x]);
}
else if(result[0].equalsIgnoreCase("/exit") || (result[0].equalsIgnoreCase(("/e")))){
System.exit(0);
}
else if(result[0].equalsIgnoreCase("/?")){
errorMessage(username, "Mulige kommandoer er:" + "\n" +
"/leave 'channel' eller /l" + "\n" +
"/whisper 'username' eller /w" + "\n" +
"/join 'channel' eller /j" + "\n" +
"/exit eller /e" + "\n");
}
else{
errorMessage(username, "Feil kommando! Skriv /? for mulige kommandoer");
}
return message;
}
/* Analyserer messageen som skrives inn i messagefeltet. Kommandoer starter med / */
public void analyzeMessage(String username, String channel, String message) throws RemoteException{
String text = getMessage().toLowerCase();
char command = '/';
for(int i = 0; i<1; i++){
char c = text.charAt(0);
if(c == command){
commandWritten(username, channel, text);
}
else {
newChannelMessage(username, channel, text);
}
}
}
}