Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package server;
- import java.net.*;
- import java.io.*;
- import java.util.HashMap;
- class ClientWorker implements Runnable{
- private static HashMap<Integer, ClientWorker> asClients = new HashMap<Integer, ClientWorker>();
- private static int asClientID = 0;
- private Socket client;
- private int aID;
- private PrintWriter aOut = null;
- private BufferedReader aIn = null;
- ClientWorker(Socket client) {
- this.client = client;
- this.aID = asClientID;
- asClientID++;
- }
- public void run(){
- asClients.put(this.aID, this);
- this.login(this.aID);
- String line;
- try{
- aIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
- aOut = new PrintWriter(client.getOutputStream(), true);
- } catch (IOException e) {
- System.out.println("in or out failed");
- System.exit(-2);
- }
- while(true){
- try{
- line = aIn.readLine();
- //Send data back to client
- aOut.println(line);
- System.out.println("Klient id:"+this.aID+" : "+line);
- } catch (SocketException e) {
- //System.out.println(e.toString());
- System.out.println("Klient "+this.getWorkerAdress()+" je offline");
- this.leave(this.aID);
- //System.out.println(e.getMessage());
- return;
- } catch (IOException e) {
- System.out.println("Read failed");
- System.exit(-2);
- }
- }
- }
- public InetAddress getWorkerAdress(){
- return client.getInetAddress();
- }
- public void login(int paId){
- try{
- for(Integer kluc : asClients.keySet()){
- asClients.get(kluc).sendLogin(paId);
- System.out.println("ahoj");
- }
- } catch (NullPointerException e){
- }
- }
- public void leave(int paId){
- }
- public void message(String nick, String message){
- }
- public void sendLogin(int paId){
- aOut.println("Klient "+paId+" sa pripojil.");
- aOut.flush();
- }
- public void sendLeave(int paId){
- }
- public void sendMessage(String nick, String message){
- }
- }
Add Comment
Please, Sign In to add comment