aspose file tools*
The moose likes Java in General and the fly likes i want create a simple chat with private rooms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "i want create a simple chat with private rooms" Watch "i want create a simple chat with private rooms" New topic
Author

i want create a simple chat with private rooms

Roberto robbi
Greenhorn

Joined: Sep 21, 2007
Posts: 5
Hi!I've created a simple chat...now I want to make a chat that contains private rooms,so one client create a room and the other client coming into this room.
The problem is that I don't know how do I make to create these PRIVATE ROOMS?!
Is there anybody can help me?!

CLIENTI.JAVA

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public class Clienti{

String nome;
PrintStream ps;
//String hobby;
//Image mypicture;
//Date birth;



public Clienti (PrintStream ps, String name){

this.ps = ps;
this.name = name;


}

public String getNome(){

return name;
}

public PrintStream getStream() {
return ps;
}

}

CLIENT.JAVA

import java.io.*;
import java.net.*;
import java.lang.*;

public class Client {

public static void main (String args[]){

BufferedReader br = null;
String str = null;
String username = null;
InputStream is = null;
Socket cs = null;
PrintStream pw = null;
BufferedReader stdIn = null;
String str1 = null;
String str2 = null;
int i = 0;


try {

cs = new Socket ("localhost",1234);

br = new BufferedReader (new InputStreamReader (cs.getInputStream()));

pw = new PrintStream (cs.getOutputStream());

stdIn = new BufferedReader ( new InputStreamReader (System.in));

str = br.readLine();


System.out.println(str);

System.out.println ("Insert Username (max 20 characters)");

username = stdIn.readLine();

i = username.length();


while (i==0 || i>20) {//Control username

System.out.println ("Username is not correct");

System.out.println ("Insert Username (max 20 characters)");

username = stdIn.readLine();

i = username.length();

}



pw.println(username);//


new ThreadClient1(br).start();



while (true){



str2 = stdIn.readLine();//Client writes and send it

pw.println(str2);


}

}
catch (Exception e){

System.out.println("Error Connession Server");

} // end try catch

} // end method

} // end class


SERVER.JAVA

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public class Server {


public static void main(String[] args) {

VectorHandler vector_hand = new VectorHandler();

System.out.println ("Server in stand-by...");

ServerSocket ss = null;

try{

ss = new ServerSocket (1234);



while (true){ //Server is in stand-by

Socket clientsocket = ss.accept();// Wait socket

new ThreadServer1 (clientsocket, vector_hand).start();

}
}

catch (Exception e){
System.out.println("Error by opened door");

}
}
}


THREADCLIENT1.JAVA

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public class ThreadClient1 extends Thread {


private BufferedReader input;


public ThreadClient1 (BufferedReader input){
this.input = input;
}

public void run(){

try{
while (true){
String str1 = input.readLine();//client receive

System.out.println(str1);
}
}
catch (IOException e) {
System.out.println("Error by listening");
}
}
}


THREADSERVER1.JAVA

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public class ThreadServer1 extends Thread {

Socket client;
PrintStream p = null;
BufferedReader br = null;
VectorHandler server;
Clienti cliente;

public ThreadServer1 (Socket socket, VectorHandler server){

System.out.println("He/She's coming a new friend by "+socket.getInetAddress());
client = socket;
this.server = server;
}

public void run(){

try{
System.out.println ("Server ready to receive new friends by the 1234 door");

br = new BufferedReader (new InputStreamReader (client.getInputStream()));

p = new PrintStream (client.getOutputStream());

p.println("::WELCOME INTO MY CHAT::");

String myname = br.readLine();//receive the username

System.out.println (" "+ myname+" is coming into the chat ");

p.println("::HI "+ myname + "!NOW YOU ARE COMING INTO THE CHAT!!::");//send the welcome message

cliente = new Clienti (p,myname);//refresh the vector with client that are connected

server.addclienti(cliente);



int numclient = server.counterclient();//conta quanti clienti sono presenti nel vettore (dentro al ciclo perche altrimenti se fosse fuori ogni thread conterebbe diversamente


for (int y=0;y<numclient;y++) {

Clienti current_client = server.name(y);//select at every single element

PrintStream ps = current_client.getStream();

ps.println(myname+" is in the chat");//send it at every client
}


while (true){//Receive and send the messages

String str1 = br.readLine();

System.out.println("<"+myname+"> " + str1);

int numclient = server.counterclient();//conta quanti clienti sono presenti nel vettore (dentro al ciclo perche altrimenti se fosse fuori ogni thread conterebbe diversamente


for (int i=0;i<numclient;i++) {/

Clienti cliente1 = server.nome(i);//select every single element i

PrintStream ps = cliente1.getStream();

ps.println("<"+myname+"> "+str1);//send it ad every single element i
}

}


} // try
catch (Exception e)
{
System.out.println ("Error by communication with the client or the client is disconnetted");

int numberclient = server.counterclient();//conta quanti clienti sono presenti nel vettore (dentro al ciclo perche altrimenti se fosse fuori ogni thread conterebbe diversamente


for (int j=0;j<numberclient;j++)
{
Clienti cliente3 = server.name(j);//select every single element j

cliente3.ps.println("a friend is disconnetted");//send this string at every element j

}


server.remove(cliente);//Refresh the vector if somebody exit

} // end try catch
}

}


VECTORHANDLER.JAVA

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public class VectorHandler {

Vector vectorclienti;
public VectorHandler() {

vectoreclienti = new Vector();
}

public void addclienti(Clienti newclient) {//add an element
vectorclienti.addElement(newclient);
}

public int counterclienti(){//return the size of vector
return vectorclienti.size();

}

public Clienti name (int i) { //select the element i of the vector
return (Clienti) vectorclienti.elementAt(i);
}

public void remove(Clienti client) {//remove an elemento
vectorclienti.remove(client);
}


}
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It looks like your vector of users is a "room". You'll need a way to have several of those.

A client might be able to ask for a list of public rooms and then join any one of them.

A client might be able to create a private room which others cannot see in the list. But the creator could send an invitation to another user which would let them join a private room.

Does that give you some ideas to work with?

BTW: Try the CODE tag in the UBB buttons below the post editor. That will preserve all your indenting and other formatting, making things much easier on readers.

Cheers!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Roberto robbi
Greenhorn

Joined: Sep 21, 2007
Posts: 5
Hi!thanks for your answer...
I was thinking that the Object Rooms should be a vector and if clients are more than 4, the next client, who is connected, goes into the new room.

can you help me?!?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Vector should never be used in new code, unless you are stuck with Java 1.1.

Use ArrayList in preference.

If you are using Java 5 or later, consider using generics to give your ArrayLists type safety.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i want create a simple chat with private rooms
 
Similar Threads
Comunication between classes
Two way chat?
What is the class to use for server in single server multiclient chat programming
problem in simple chat application.
Client server communication programme