Estou tentando implementar no codigo do chat, para um cliente se comunicar direto com outro cliente sem passar pelo servidor.
Ja tentei de tudo... alguem poderia me ajudar, estou usando java-rmi.
Obrigado
Segue o codigo abaixo.public class SocketTextUI {
/**
* Please see that this works poorly...
* Just to show how distribution works...
* It breaks some OO patterns too.
*/
public static void main( String args[] ) {
Scanner scanner = new Scanner( System.in );
System.out.print( "Enter 0 to start a server and 1 for client:" );
int type = scanner.nextInt();
if ( type == 0 ) {
System.out.print( "Please type the port to be used:" );
int port = scanner.nextInt();
ChatServer server = new ChatServer( port );
System.out.println( "Waiting client..." );
} else {
System.out.print( "Please type the server's IP: " );
String ip = scanner.next();
System.out.print( "Please type the server's port: " );
int port = scanner.nextInt();
ChatClient client = new ChatClient( ip ,port );
}
}
}
public class RMITextUI {
/**
* Please see that this works poorly...
* Just to show how distribution works...
* It breaks some OO patterns too.
*/
public static void main( String args[] ) {
Scanner scanner = new Scanner( System.in );
System.out.print( "Enter 0 to start a server and 1 for client:" );
int type = scanner.nextInt();
if ( type == 0 ) {
try {
RMIChatServerIT server = new RMIChatServer();
Naming.rebind( RMIChatServer.HOST_ADD, server);
System.out.println( "Waiting client..." );
} catch (Exception e) {
System.err.println("RMIChatServer exception: " + e.getMessage());
e.printStackTrace();
}
} else {
try {
Scanner teclado = new Scanner( System.in );
System.out.print( "Tell me your NICK please: " );
String myNick = scanner.next();
RMIChatClientIT client = new RMIChatClient( myNick );
RMIChatServerIT server = ( RMIChatServerIT ) Naming.lookup( RMIChatServer.HOST_ADD );
server.connect( client );
String linha = ""; //TODO: should change!
while ( !linha.equals( "xau" ) ) {
linha = teclado.next();
server.sendMessage( new Message( myNick, linha ) );
}
server.disconnect( client );
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}