Java Socket server with threads (multiple connections)
Ok so I've got two classes, (main)"server" and "threader". I've got client program and it's working great (the problem is not in the client program) (also it's client is actionsctipt2.0) How it should work:
Launching the server, launching few clients and start chatting with eachother, each client sends message to the server and server sends it to all the clients.
So the problem is that it's working incorrectly. When I launch server and then two same clients, and start chatting, server sends client's messages only to one of the clients. (It's quite hard to explain) and if I launch only one client, server doesn't output any on this client's messages. If I launch 3 clients, only two of them receiving the messages
Here is server class
import java.io.*; import java.net.*; import java.util.*; public class server { public static ArrayList<Socket> ConnectionArray = new ArrayList<Socket>(); public static ArrayList<String> CurrentUsers = new ArrayList<String>(); public static void main(String[] args) throws IOException { try { final int PORT = 2807; ServerSocket SERVER = new ServerSocket(PORT); System.out.println("Server launched."); int userid = 0; while(true) { Socket SOCK = SERVER.accept(); ConnectionArray.add(SOCK); userid += 1; System.out.println("User id " + userid +" connected."); //AddUserName(SOCK); threader CHAT = new threader(SOCK); Thread X = new Thread(CHAT); X.start(); } } catch(Exception X) {System.out.print(X);} } }
Here is threader class:
import java.io.*; import java.net.*; import java.util.*; public class threader implements Runnable { Socket SOCK; private Scanner INPUT; private PrintWriter OUT; String MESSAGE = ""; public threader(Socket X){ this.SOCK = X; } char EOF = (char)0x00; public void CheckConnection() throws IOException { if(!SOCK.isConnected()) { for(int i = 1; i<server.ConnectionArray.size(); i++) { if(server.ConnectionArray.get(i) == SOCK) { server.ConnectionArray.remove(i); } } for(int i = 1; i <server.ConnectionArray.size(); i++) { Socket TEMP_SOCK = (Socket) server.ConnectionArray.get(i-1); PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream()); TEMP_OUT.println(TEMP_SOCK.getLocalAddress().getHostName() + " dissconnected" + EOF); TEMP_OUT.flush(); System.out.println(TEMP_SOCK.getLocalAddress().getHostName() + " dissconnected"); } } } public void run(){ try { try { INPUT = new Scanner(SOCK.getInputStream()); OUT = new PrintWriter(SOCK.getOutputStream()); while(true) { CheckConnection(); if(!INPUT.hasNext()) { return;} MESSAGE = INPUT.nextLine(); System.out.println("User: "+ MESSAGE); for(int i = 1; i<server.ConnectionArray.size(); i++) { Socket TEMP_SOCK = (Socket) server.ConnectionArray.get(i-1); PrintWriter TEMP_OUT = new PrintWriter(TEMP_SOCK.getOutputStream()); TEMP_OUT.println("Someone: " + MESSAGE.trim() + EOF); TEMP_OUT.flush(); // System.out.println("Sent to: " + TEMP_SOCK.getLocalAddress().getHostName()); } } } finally { //SOCK.close(); } } catch(Exception X) { System.out.print(X);} } }
Would be REALLY awesome if someone gonna help me