Projet en JAVA application chat avec socket
DESCRIPTION
Cet article propose en détaille un projet en JAVA application chat avec socket, c'est une Application Client Serveur de chat multi client
Vous pourrez télécharger le fichier au format zip il contient deux dossier client et serveur avec code sources et les classes.
Extrait du code source :
import java.rmi.*; // For Naming, RemoteException, etc.
import java.net.*; // For MalformedURLException
import java.io.*; // For Serializable interface
/**
* Get a Rem object from the specified remote host. Use its methods as though it
* were a local object.
*/
public class RemClient {
public static void main(String[] args) {
try {
String host = (args.length > 0) ? args[0] : "localhost";
String port = (args.length > 1) ? args[1] : "1099";
// Get the remote object and store it in remObject:
Rem remObject = (Rem) Naming.lookup("rmi://" + host + ":" + port + "/Rem");
// Call methods in remObject:
System.out.println(remObject.getMessage());
System.out.println(remObject.addition(10, 5));
} catch (RemoteException re) {
System.out.println("RemoteException: " + re);
} catch (NotBoundException nbe) {
System.out.println("NotBoundException: " + nbe);
} catch (MalformedURLException mfe) {
System.out.println("MalformedURLException: " + mfe);
}
}
}
… … …
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.net.*;
// Taken from Core Web Programming from Prentice Hall and
// Sun Microsystems Press, and may be freely used or adapted.
/**
* The server creates a RemImpl (which implements the Rem interface), then
* registers it with the URL Rem, where clients can access it.
*/
public class RemServer {
public static void main(String[] args) {
String port = (args.length > 0) ? args[0] : "1099";
try {
LocateRegistry.createRegistry(Integer.parseInt(port));
RemImpl localObject = new RemImpl();
Naming.rebind("rmi://localhost:" + port + "/Rem", localObject);
} catch (RemoteException re) {
System.out.println("RemoteException: " + re);
} catch (MalformedURLException mfe) {
System.out.println("MalformedURLException: " + mfe);
}
}
}
... ... ...
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
/** This is the actual implementation of Rem that the RMI
* server uses. The server builds an instance of this, then
* registers it with a URL. The client accesses the URL and
* binds the result to a Rem (not a RemImpl; it doesn't
* have this).
*/
public class RemImpl extends UnicastRemoteObject implements Rem {
public RemImpl() throws RemoteException
{
}
public String getMessage() throws RemoteException {
return("Here is a remote message.");
}
@Override
public int addition(int x, int b) throws RemoteException {
// TODO Auto-generated method stub
return (x+b);
}
}
...
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Rem extends Remote{
public String getMessage() throws RemoteException;
public int addition(int x, int b) throws RemoteException;
}