sockets - Java: I don't get the messages from other clients? -


does know whats wrong code?
when write client1 see on server , on client1 not on client2.

run() in client.java:

public void run() {         scanner input = new scanner(system.in);          try {              socket client = new socket(host, port);              system.out.println("client started");              outputstream out = client.getoutputstream();             printwriter writer = new printwriter(out);              inputstream in = client.getinputstream();             bufferedreader reader = new bufferedreader(new inputstreamreader(in));                  string = input.nextline();                 writer.write(clientname + ": " + + newline);                 writer.flush();                  string s = null;                  while((s = reader.readline()) != null) {                      system.out.println(s);                  }              writer.close();             reader.close();             client.close();          } 

if need server code or else ask.
in advance!!

additionally server:

public class server {      public static void main(string[] args) {         int port = 40480;         int max = 10;          executorservice executor = executors.newfixedthreadpool(max);          try {             serversocket server = new serversocket(port);             system.out.print("server started" + "\n");              while(true) {                  try {                     socket client = server.accept();                      executor.execute(new handler(client));                  }                  catch (ioexception e) {                     e.printstacktrace();                  }              }          } catch(exception e) {             e.printstacktrace();          }      }  } 

and handler:

public class handler implements runnable{      private socket client;      public handler(socket client) {         this.client = client;      }      @override     public void run() {          try {              outputstream out = client.getoutputstream();             printwriter writer = new printwriter(out);              inputstream in = client.getinputstream();             bufferedreader reader = new bufferedreader(new inputstreamreader(in));              string s = null;              while((s = reader.readline()) != null) {                  writer.write(s + "\n");                 writer.flush();                  system.out.println(s);              }              writer.close();             reader.close();             client.close();          }          catch(exception e) {           }      }  } 

this example - not complete should give idea how multicast output number of listening clients. there better ways this, wrote similar how appeared doing sockets. lacks error checking in many places , have left exercise reader. code written can used on java 1.6 or higher.

the code uses list of connected clients maintained in server object. when input received 1 client, output multicast each client in client list. writing done via write method in client class.

import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.serversocket; import java.net.socket; import java.util.linkedlist; import java.util.iterator; import java.util.list; import java.util.concurrent.executorservice; import java.util.concurrent.executors;  public class multicastechoserver {     list<client> clientlist = new linkedlist<client>();     executorservice executor;      int port = 40480;     int max = 10;      public multicastechoserver() {         this.executor = executors.newfixedthreadpool(max);     }      public void writetoallclients(string string) throws ioexception {         // multiple threads access must in synchronized block         synchronized (this.clientlist) {             iterator<client> iter = this.clientlist.iterator();             while (iter.hasnext())                 iter.next().write(string);         }     }      public void addclient(client client) {         // multiple threads access must in synchronized block         synchronized (this.clientlist) {             clientlist.add(client);         }     }      public void removeclient(client client) {         // multiple threads access must in synchronized block         synchronized (this.clientlist) {             clientlist.remove(client);         }     }      public void listen() {         try {             serversocket server = new serversocket(port);             system.out.println("server started , listening connections");              while (true) {                 try {                     socket socket = server.accept();                     system.out.print("connection accepted" + "\n");                      client newclient = new client(this, socket);                      this.addclient(newclient);                     this.executor.execute(newclient);                 } catch (ioexception e) {                     e.printstacktrace();                 }             }         } catch (exception e) {             e.printstacktrace();         }     }      public static void main(string[] args) {         new multicastechoserver().listen();     }      private class client implements runnable {          socket socket;         printwriter writer;         bufferedreader reader;         multicastechoserver server;          public client(multicastechoserver server, socket socket) throws ioexception {             this.server = server;             this.socket = socket;             this.writer = new printwriter(this.socket.getoutputstream());             this.reader = new bufferedreader(new inputstreamreader(socket.getinputstream()));         }          synchronized public void write(string string) throws ioexception {             writer.write(string);             writer.flush();         }          public void close() {             this.writer.close();             try {                 this.reader.close();             } catch (ioexception e) {             }             try {                 this.socket.close();             } catch (ioexception e) {             }         }          @override         public void run() {             system.out.println("client waiting");              string instring = null;             try {                 while ((instring = this.reader.readline()) != null) {                     this.server.writetoallclients(instring + "\n");                     system.out.println(instring);                 }             } catch (ioexception e1) {             }              server.removeclient(this);             this.close();             system.out.println("client closed");         }     }  } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -