• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not able to use two ports in RMI chat application

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am trying to create a chat application in java using RMI.
To make communication active between client - client I am using Socket programming in RMI to get client information, but once ServerSocket tries to accept connection on specified port it throws BindException::Address already in use Jvm_bind exception.

I dont know how to resolve this port conflict.Please help in solving this problem.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be better if you can show us the code.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My dear friend.
Actually there is no need to use two different port for same application.
In order to create chat application in RMi, just gather the reference of each client in an Vector or LinkedList or any other data structure you feel feasible.
Then loop your message through all the clients using the LinkList, which will broadcast the message to all the clients.

I am damn sure this would workout as I have implemented the same in my chat application.
All the best.

ChatClient.java



import java.rmi.*;

public interface ChatClient1 extends Remote
{
public void receive (String s,String username,String IPAdd) throws RemoteException;

public void popUp(boolean flag,String uname) throws RemoteException;
}




ChatClientImpl.java


import java.net.InetAddress;
import java.rmi.*;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.Vector;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

public class ChatClientImpl1 extends PortableRemoteObject implements ChatClient1, Runnable {
private boolean flag,check=false;
private ChatServer1 server;
private Vector uName = new Vector();
private String username,password,IPAddress,msg="hello",receiverIP,receiverUname;

//private static Thread thread=null;

public ChatClientImpl1 (ChatServer1 cs) throws RemoteException {
try {
server = cs;
InetAddress inet = InetAddress.getLocalHost();
IPAddress = inet.getHostAddress();
System.out.println("Client::" + inet.getHostAddress());
System.out.println("Username::");
Scanner sc = new Scanner(System.in);
username = sc.nextLine();
System.out.println("Password::");
password = sc.nextLine();
flag=server.userLogin(this, username, password, inet.getHostAddress());
if(flag == true){
System.out.println("Either Login Failed OR " +username+" is already logged in.");
server.userLogout(username);
System.exit(1);
}
uName=server.getUsers();
System.out.println("\nUsers currently online::");
for(int i=0;i<uName.size();i++){
System.out.println(uName.get(i).toString());
}
System.out.println("\nEnter name of user with whom you want to chat:::");
if(sc.hasNext()) {
receiverUname = sc.nextLine();
}
else {
server.userLogout(username);
System.out.println(username+" has logged out.");
sc.close();
System.exit(1);
}


while(check == false){
uName=server.getUsers();
System.out.println("\nUsers currently online::");
for(int j=0;j<uName.size();j++){
System.out.println(uName.get(j).toString());
if(receiverUname.equals(uName.get(j).toString())){
check=true;
break;
}
}
if(check == true){
break;
}else {
System.out.println("User is not online.");
System.out.println("Please enter some other user name:");
receiverUname = sc.nextLine();
}
}
receiverIP=server.getUserInfo(receiverUname);
System.out.println("\nEnter text to Chat : ");
}catch (Exception ex) {
System.out.println("We have encountered a problem,please login again.");
}
}

public synchronized void receive(String s,String user,String IP) throws RemoteException {
System.out.println(user+"::"+s);
}

public synchronized void popUp(boolean flag, String uname) throws RemoteException{
if(flag==true){
System.out.println(uname+" is now online");
}else{
System.out.println(uname+" is now offline");
}
}
public void run(){
Scanner in=new Scanner(System.in);
while(!msg.equals("exit")){
try{
if(in.hasNext())
msg=in.nextLine();
else{
server.userLogout(username);
System.out.println(username+" has logged out.");
in.close();
System.exit(1);
}
server.sendMessage(msg,username,IPAddress,receiverIP);
if(msg.equals("exit"))
{
server.userLogout(username);
System.out.println(username+" has logged out.");
in.close();
System.exit(1);
}
}catch(Exception e){
System.out.println("Server is encountering some problems, please login again.");
}
}
}

public static void main (String[] args){
try{
String str1 = args[0];
String s1 = "iiop://" + str1 + ":1050";
Hashtable hash = new Hashtable();
hash.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory");
hash.put("java.naming.provider.url", s1);
Context con = new InitialContext(hash);
Object obj = con.lookup("ChatServer");
ChatServer1 cs = (ChatServer1) PortableRemoteObject.narrow(obj,ChatServer1.class);
new Thread(new ChatClientImpl1(cs)).start();
}catch (Exception e){
System.out.println("Server is encountering some problems, please login again.");
}
}
}



With Regards,
Sunil Parmar
GET Java Team,
Hrut Infotech.
sunil04ce@gmail.com
 
dhaval Rajput
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


thank you very much Sir
It really helped me a lot.
Once Again thank you very much.


With Regards,
Dhaval Rajput
GET Java Team,
Hrut Infotech
dhaval.rajput.p@gmail.com
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic