simple java rmi chat application source code
#1

i want free download of this projects source code.
Reply
#2
simple java rmi chat application source code

Chat program using JDK 6 and RMI
This chat application is developed using JDK 6 and Java RMI. This can be used for group-chat and one-to-one chat
To develop this application, we need following files.
ChatServerInt.java - An interface for chat server.
ChatClientInt.java - An interface for chat client.
ChatServer.java - Implementation for chat server program.
ChatClient.java - Implementation for chat client program.
chatclient.policy - Policy to access the remote methods.
ChatServerInt.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ChatServerInt extends Remote {
void connect(String name, ChatClientInt c) throws RemoteException;
void disconnect(ChatClientInt c) throws RemoteException;
void broadcast(String name, String s) throws RemoteException;
void list(ChatClientInt c) throws RemoteException;
ChatClientInt lookup(String name) throws RemoteException;
}

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

public interface ChatClientInt extends Remote {
void update(String name, String s) throws RemoteException;
String getName() throws RemoteException;
}

ChatServer.java
import java.io.PrintStream;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ChatServer implements ChatServerInt {

private static final long serialVersionUID = 1L;
private List myclients;
private List clientNames;
private static final String name = "server";

public ChatServer() throws RemoteException {
myclients = new ArrayList();
clientNames = new ArrayList();
}

public synchronized void disconnect(ChatClientInt c) throws RemoteException {
myclients.remove©;
clientNames.remove(c.getName());
writeLog(c.getName() + " disconnected at {0}");
for(ChatClientInt client: myclients) {
client.update(name, c.getName() + " has left.");
}
}

public synchronized void list(ChatClientInt c) throws RemoteException {
c.update(name, "Active users: " + clientNames.toString());
}

public synchronized ChatClientInt lookup(String name) throws RemoteException {
ChatClientInt c = null;
int index = clientNames.indexOf(name);
if (-1 != index) {
c = myclients.get(index);
}
return c;
}

public synchronized void connect(String n, ChatClientInt c) throws RemoteException {
for(ChatClientInt client: myclients) {
client.update(name, n + " is joining now...");
}
clientNames.add(n);
myclients.add©;
int count = myclients.size();
StringBuffer wcmMsg = new StringBuffer("Welcome ").append(n).append(", ");
wcmMsg.append("There ").append((1 == count)? "is " : "are ").append(
count).append((1 == count)? " user: " : " users: ");
wcmMsg.append(clientNames.toString());
c.update(name, wcmMsg.toString());
writeLog(n + " connected at {0}");
}

public synchronized void broadcast(String name, String s) throws RemoteException {
for(ChatClientInt client: myclients) {
client.update(name, s);
}
writeLog("{0}: " + name + ": " + s);
}

public static void main (String[] args) {
if (1 != args.length) {
System.out.println("Usage: java ChatServer <server_port>");
System.out.println("Example: java ChatServer 2001");
return;
}
int port = Integer.parseInt(args[0]);

try {
System.setOut(new PrintStream("server.log"));
ChatServer server = new ChatServer();
LocateRegistry.getRegistry(port).bind("ChatServer",
UnicastRemoteObject.exportObject(server, 0));
writeLog("Server started at {0}, waiting for connections...");
} catch(Exception e) {
e.printStackTrace();
}
}

private static void writeLog(String log) {
System.out.println(MessageFormat.format(log, new Date().toString()));
}
}

ChatClient.java

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

public class ChatClient extends UnicastRemoteObject implements ChatClientInt, Runnable {
private static final long serialVersionUID = 1L;
private ChatServerInt server;
private String name;
private ChatClientInt friend;
private static final String LIST = "LIST";
private static final String QUIT = "QUIT";
private static final String HELLO = "Hello ";
public ChatClient(ChatServerInt cs, String name) throws RemoteException {
this.name = name;
this.server = cs;
server.connect(name, this);
}

public synchronized void update(String name, String s) throws RemoteException {
if (! this.name.equals(name)) {
System.out.println(name + ": " + s);
}
}

public void run() {
Scanner in=new Scanner(System.in);
String msg;

while(true) {
try {
msg=in.nextLine();
msg = msg.trim();
if (QUIT.equals(msg)) {
server.disconnect(this);
in.close();
System.exit(0);
} else if (LIST.equals(msg)){
server.list(this);
} else if (msg.startsWith(HELLO) && msg.contains(",")) {
String s[] = msg.substring(0, msg.indexOf(",")).split(" ");
String user = s[s.length-1].trim();
friend = server.lookup(user);
if (null != friend) {
friend.update(name, msg);
} else {
server.broadcast(name, msg);
}
} else if (! "".equals(msg)) {
server.broadcast(name, msg);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
if (3 != args.length) {
System.out.println("Usage: java ChatClient <server_ip> <server_port> <user_name>");
System.out.println("Example: java ChatClient 127.0.0.1 2001 user1");
return;
}
String host = args[0];
int port = Integer.parseInt(args[1]);
String name = args[2];

try {
Registry registry = LocateRegistry.getRegistry(host, port);
ChatServerInt server = (ChatServerInt) registry.lookup("ChatServer");
Thread t = new Thread(new ChatClient(server, name));
t.start();
} catch (Exception e) {
e.printStackTrace();
}
}

public String getName() {
return name;
}
}

chatclient.policy

grant {
permission java.security.AllPermission;
};
Compile the source and put the classes into bin.
javac -d ../bin/ *.java

Now run rmiregstry (better to use a port) in a new console and leave it as it is.
rmiregistry 2001

Start the chat server program with the same port number 2001.
java -Djava.security.policy= -Djava.rmi.server.codebase=file:/ ChatServer 2001

Now start as many clients you want...
In case you want run the client from the same machine where chat server is running, use following...
java ChatClient 127.0.0.1 2001 user1

In case of remote machine, you must give the server ip in command-line...
java ChatClient 2001 user2
Reply
#3

(21-04-2016, 01:10 PM)Guest Wrote: i want free download of this projects source code.
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: staff management system project source code in rmi, future scope of chat application in pdf, simple project in java, chat application for srs ppt, java and rmi atm source code, free download source code for quiz using rmi, simple chat system using java rmi,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  today online result rajshree 30 39 chat 3 26,079 27-09-2018, 11:07 PM
Last Post: Awash debnath
  authentication schemes for session passwords using color and images project source code 2 2,231 03-02-2018, 09:35 AM
Last Post: Nischithnash
  free download source code for online movie ticket booking in java 2 18,493 15-08-2017, 03:21 PM
Last Post: Morshed
  source code for rsa encryption and decryption in java 2 7,929 29-05-2017, 04:21 PM
Last Post: Meghna Jadhav
  download liver tumor ct scan image in matlab with source code 4 7,987 21-05-2017, 09:54 PM
Last Post: abdulrahmanmashaal
  online cab booking source code in asp net 3 7,863 11-05-2017, 10:39 AM
Last Post: jaseela123d
Thumbs Up online catering management system on php with report and source code and ppt 4 8,719 29-04-2017, 10:59 AM
Last Post: jaseela123d
  source code for task scheduling using genetic algorithm using java 2 8,447 11-04-2017, 08:31 PM
Last Post: Guest
  when are the application for lilitha nursing training 2016 1 7,432 07-04-2017, 12:40 PM
Last Post: jaseela123d
  automatic timetable generator source code vb 1 7,525 07-04-2017, 12:31 PM
Last Post: jaseela123d

Forum Jump: