java - Sending data to sockets in an arraylist of Sockets -


i have made chat server clients can connect clients don't messages other sent. code all. sending , receiving , setting output streams.

public void run() {     while(true)     {         for(int = 0; < clientconnector.connections.size(); i++)         {              try             {                 if(socket != null)                 {                     objectoutputstream output = new objectoutputstream(socket.getoutputstream());                     objectinputstream input = new objectinputstream(socket.getinputstream());                     whilechatting(input, output);                 }             }             catch (ioexception e)             {                 e.printstacktrace();             }         }     } }  public static void sendmessage(string message, string returnedmessage, objectoutputstream out) {     try     {         system.out.println("[server] " + message);         if(!message.isempty())         {              out.writeobject("\247c[server]\247d " + message);             out.flush();             system.out.println("[chat] sent: " + message);         }         else         {             out.writeobject(returnedmessage);             system.out.println("[chat] sent: " + returnedmessage);         }         out.flush();         system.out.println("[info] flushing remaining data stream.");      }     catch(ioexception ioexception)     {         system.out.println("[warning] error: ioexception @ sendmessage line 76.");     } }  public static void whilechatting(objectinputstream input, objectoutputstream output) throws ioexception {     string message = "";         {         try         {             message = (string) input.readobject();             system.out.println(message);             sendmessage("", message, output);         }         catch(classnotfoundexception classnotfoundexception)         {             system.out.println("[warning] error: classnotfoundexception @ whilechatting line 1-7.");             system.out.println("idk wtf user sent!");         }     }while(!message.equals("/stop")); } 

i wondering. how make send 1 person sends clients? keep list of sockets in array list of sockets. looks this.

public static arraylist<socket> connections = new arraylist<socket>(); 

as each client connects stores socket list. if there better way of doing please let me know.

if these remote clients, multicastsocket might trick. if these local clients, publish/subscribe messaging service hornetq work (clients subscribe message queue, , hornetq takes care of routing).


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -