• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

i want create a simple chat with private rooms

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}


}
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Roberto robbi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?!?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic