I created a simple MultiThreaded server. Each time it receives a client, a DataInputStream and a DataOutputStream are created, and the start of communication
Server:
public class Connection implements Runnable{ boolean isAlreadyOpened = false; @Override public void run() { // TODO Auto-generated method stub try { ServerSocket ss = new ServerSocket(7000); while(true){ System.out.println("Il Server sta cercando Connessioni"); Socket s = ss.accept(); System.out.println("Il Server ha accettato un Client"); Thread t2 = new Thread(new Runnable(){ public void run(){ try { DataInputStream dis = new DataInputStream(s.getInputStream()); isAlreadyOpened = true; DataOutputStream dos = new DataOutputStream(s.getOutputStream()); while(true){ String test = dis.readUTF(); dos.writeUTF(test); System.out.println(test); dos.flush(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); isAlreadyOpened = false; } } }); t2.start(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
I tried to connect an Android app and JavaApp; every 40 seconds both of these programs send a line through writeUTF. The connection is established correctly from both Clients, but the server receives data only from the last one that connects to the server. How can I let the server receive / send data from / to ALL clients?
EDIT:
I tried to install this:
public class Connection implements Runnable { @Override public void run() { try { ServerSocket ss = new ServerSocket(7000); while (true) { System.out.println("Server is listening"); Socket s = ss.accept(); System.out.println("Client Accepted"); Thread t2 = new Thread(new Runnable() { public void run() { try { DataInputStream dis = new DataInputStream(s.getInputStream()); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); while (true) { String test = dis.readUTF(); dos.writeUTF(test); System.out.println(test); dos.flush(); } } catch (IOException e.printStackTrace(); } finally { try { s.close(); } catch (IOException e) { e.printStackTrace(); } } } }); t2.start(); } } catch (IOException e) {
But the result is the same
EDIT:
Andorid Client Code
public class Connection implements Runnable { @Override public void run() { try { Socket s = new Socket("127.0.0.1", 7000); DataInputStream dis = new DataInputStream(s.getInputStream()); DataOutputStream dos = new DataOutputStream(s.getOutputStream()); while(true){ dos.writeUTF("FromAndroid"); Log.d("InputStreammmm", dis.readUTF()); Thread.sleep(10000) } } catch (IOException e) { e.printStackTrace(); } }
JAVA APP KEYBOARD CODE
@Override public void run() { try { Socket socket = new Socket("127.0.0.1", 7000); System.out.println("Connessooo"); DataInputStream dis = new DataInputStream(socket.getInputStream()); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); while(true){ dos.writeUTF("Invioooooooooooooooooooooooooo"); result = dis.readUTF(); System.out.println(result); Thread.sleep(10000); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
EDIT WITH SOCKET LIST:
public class Connection implements Runnable { List<Socket> sList = new ArrayList<>(); Socket s; int i = 0; @Override public void run() { // TODO Auto-generated method stub try { ServerSocket ss = new ServerSocket(7000); while (true) { System.out.println("Server Listening"); s = ss.accept(); sList.add(s); System.out.println("Accepted Client --- " +s.toString()); Thread t2 = new Thread(new Runnable() { public void run() { try { DataInputStream dis = new DataInputStream(s.getInputStream()); while (true) { String test = dis.readUTF(); System.out.println("Message sent from -- " + sList.get(i).toString()); System.out.println(test); while(i < sList.size()){ DataOutputStream dos = new DataOutputStream(sList.get(i).getOutputStream()); dos.writeUTF(test); System.out.println("Message Sent to -- " + sList.get(i).toString()); dos.flush(); ++i; } i=0; } } catch (IOException e) { e.printStackTrace(); } finally { try { System.out.println("Closing Socket --- " + sList.get(i).toString()); sList.get(i).close(); sList.remove(i); } catch (IOException e) { e.printStackTrace(); } } } }); t2.start(); } } catch (IOException e) { e.printStackTrace(); } }
By doing so, my problem is resolved as suggested by EJP ...
source share