Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Chat Help

Wed Jun 10, 2009 6:03 pm

Hi, this is my first post here, i'm kinda newbie at java, tbh i hate it as well as c++ :\
and i have a problem in my program, i made 3 classes, one server, one client and one to chat private between two users connected to the same server, and the problem is here, the window to start talking opens, but the other one to receive the message don't. All the other rmi* classes have been given by the school teacher.
I'd like to know if anyone can help me.
Best Regards :)

I also would like to do in my chat the following tasks :

-replace some charactes such as ":)" or ";)" or ":D" for images, similar to msn, how can i do that?
-play a sounds when i press "activate" , how can i do that as well?

Code snippet:
java code
/*this class implements the RMI example client, that calls the
*Remote Methods, from the server.
*/

//java rmi imports
import java.io.*;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.NotBoundException;
import java.rmi.AlreadyBoundException;
import java.rmi.server.UnicastRemoteObject;

//swing imports
import java.util.Vector;
import javax.swing.JList;
import javax.swing.JTextArea;


public class RmiExampleClient implements RmiExampleClientMethods{

//stub
RmiExampleServerMethods stub;
//stubc
RmiExampleClientMethods stubc;
//registry, hostname and port
Registry registry;
String hostName = "localhost";
String username;
int port = 1099;

//chat are where to place text
JTextArea chatArea;
JList listArea;

//methods to make available
String[] methodsNames = {"Say Something", "ReceiveClients", "sendMsg", "receiveMsg","receiveFile"};

//clients online variable
Vector<String> usersOnline = new Vector<String>();

//private windows opened
Vector<Privado> openedPrivateWindows = new Vector<Privado>();

//Constructor Method
/*-------- ------------------------------------*/
public RmiExampleClient(String hostname, int port, JTextArea chatArea, JList listArea, String username) {

//update global variables
this.hostName = hostname;
this.port = port;
this.chatArea = chatArea;
this.listArea = listArea;
this.username = username;
}

/*-------------------- ------------------------------*/

// User methods
/*--------------------- ---------------------------*/
public void run() throws RemoteException, NotBoundException, AlreadyBoundException {

//declare stubc - Client stub
stubc = (RmiExampleClientMethods) UnicastRemoteObject.exportObject(this, 0);

//get RMI registry information
registry = LocateRegistry.getRegistry(hostName, port);

//register client methods
registerMethods();
}

public void checkMethod(String methodName) throws RemoteException, NotBoundException {

//check RMI Remote Method - first look for the service name in the registry
stub = (RmiExampleServerMethods) registry.lookup(methodName);
}

public String callGreatings() throws RemoteException {

//call the method
return (stub.serverHello());
}

public String callEcoGirl() throws RemoteException {

//call the method
return (stub.ecoGirl("Teste Eco!"));
}

public boolean callLogin(String username) throws RemoteException {

//call the method
return (stub.Login(username) );
}

public void callreceiveMsg(String username , String msg) throws RemoteException {

stub.ReceiveMsg(username, msg) ;
}

public void callreceivePrivateMsg(String username , String msg) throws RemoteException {

stubc.receivePrivateMessage(username, msg);
}

public void registerMethods() throws RemoteException, AlreadyBoundException {

//register all methods, if they are not alreay registered
for (int i = 0; i < methodsNames.length; i++) {

//register method named "x"
registry.bind(methodsNames[i]+ "_" + username, stubc);
}
}

//metodo que permite enviar um ficheiro
public boolean EnviarFicheiro(String destinatario, String nome){

try{

//procura o metodo clienteOnline
RmiExampleServerMethods stub = (RmiExampleServerMethods) registry.lookup("clienteOnline");
//verifica se o destinatario esta online
if ( stub.clienteOnline(destinatario)) {

//procura o metodo para receber a ficheiro juntamente com o destinatario do ficheiro
stubc = (RmiExampleClientMethods) registry.lookup("ReceberFicheiro" + destinatario);

//create file object
File file = new File("imagem.jpg");

//ler do ficheiro que se quer enviar
InputStream fin = new FileInputStream(file);



/*
* Create byte array large enough to hold the content of the file.
* Use File.length to determine size of the file in bytes.
*/
byte fileContent[] = new byte[(int) file.length()];
/*
* To read content of the file in byte array, use
* int read(byte[] byteArray) method of java FileInputStream class.
*
*/
fin.read(fileContent);

//chama o metodo para receber o ficheiro
stubc.ReceberFicheiro(nome, fileContent);

}

return (true);
}
catch(Exception e){
return false;
}

}

//metodo que permite receber um ficheiro e identifica o emmisor
public boolean ReceberFicheiro(String nome, byte[] data) throws RemoteException {

try {
// Open the file that is the first
// command line parameter
FileOutputStream fstream = new FileOutputStream(nome);

// Get the object of DataInputStream
DataOutputStream out = new DataOutputStream(fstream);

//write to file
out.write(data, 0, data.length);

//close file
out.close();
fstream.close();

} catch (Exception e) {//Catch exception if any
chatArea.append("Error Receiving file: " + e.getMessage());
return (false);
}

return(true);
}


/*----------------------------------------------*/
//RMI methods implementation
/*--------------------------------------------*/

//RMI Method that returns a message string
public String clientHello() {

//update text box
chatArea.append("Method " + methodsNames[0] + "was called.\n");
return ("Hello from the Client");
}

public boolean receiveClients(Vector usersOnline){

if (usersOnline != null){
this.usersOnline = usersOnline;

//print the users list in GUI
printClientList(usersOnline);
return (true);
}
else{
//vector sent was empty
return false;
}
}

public void sendMensageAll(String msg)
{

}

private void printClientList(Vector usersOnline){

listArea.setListData(usersOnline);
}

public void receiveMessage(String username, String message){

if( message.trim().length() != 0){

chatArea.append(username + message);
}

}

public boolean receivePrivateMessage(String username,String message){

//check if there is already a opened windows for this username
for ( int i=0; i< openedPrivateWindows.size(); i++ ){

//check username
if ( openedPrivateWindows.elementAt(i).getUsername().equals(username)){

//send message to window
openedPrivateWindows.elementAt(i).receiveMessage(message);
return true;
}
}

//there is no opened window! Let's make one
openedPrivateWindows.addElement( new Privado(username, this) );

//send message to window
openedPrivateWindows.elementAt( openedPrivateWindows.size() ).receiveMessage(message);

return false;
}



/*public void SendList(){
listArea.append("vector");

}
*/


public String sendMsg(String msg) {

//update text box
chatArea.append("Method " + methodsNames[0] + "was called.\n");
return (msg);
}

/*---------------------------------------------*/
}


please take a look at my code, it was build with NetBeans 6.5.1. Thanks!


Attachments
RmiBiExample.rar
(68.81 KiB) Downloaded 791 times

Re: Chat Help

Thu Jun 11, 2009 7:47 pm

no one? :s

Re: Chat Help

Fri Jun 12, 2009 9:22 am

hey brother , sorry for late reply ,
did u saw this link
finished-projects/java-chat-t644.html

Post a reply
  Related Posts  to : Chat Help
 video chat     -  
 Chat Room in JSP     -  
 Java Chat     -  
 I need chat application using php     -  
 Java Chat With Customizable GUI     -  
 Chat Room application     -  
 java chat program.     -  
 java code for chat     -  
 MultiClient Bluetooth Chat     -  
 CMD ie ChatServer & chat client     -