Guest User

Untitled

a guest
Apr 26th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. package server;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5. import java.util.HashMap;
  6.  
  7. class ClientWorker implements Runnable{
  8. private static HashMap<Integer, ClientWorker> asClients = new HashMap<Integer, ClientWorker>();
  9. private static int asClientID = 0;
  10. private Socket client;
  11. private int aID;
  12. private PrintWriter aOut = null;
  13. private BufferedReader aIn = null;
  14.  
  15. ClientWorker(Socket client) {
  16. this.client = client;
  17. this.aID = asClientID;
  18. asClientID++;
  19. }
  20.  
  21. public void run(){
  22. asClients.put(this.aID, this);
  23. this.login(this.aID);
  24.  
  25. String line;
  26. try{
  27. aIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
  28. aOut = new PrintWriter(client.getOutputStream(), true);
  29. } catch (IOException e) {
  30. System.out.println("in or out failed");
  31. System.exit(-2);
  32. }
  33.  
  34. while(true){
  35. try{
  36. line = aIn.readLine();
  37. //Send data back to client
  38. aOut.println(line);
  39. System.out.println("Klient id:"+this.aID+" : "+line);
  40. } catch (SocketException e) {
  41. //System.out.println(e.toString());
  42. System.out.println("Klient "+this.getWorkerAdress()+" je offline");
  43. this.leave(this.aID);
  44. //System.out.println(e.getMessage());
  45. return;
  46. } catch (IOException e) {
  47. System.out.println("Read failed");
  48. System.exit(-2);
  49. }
  50. }
  51. }
  52.  
  53. public InetAddress getWorkerAdress(){
  54. return client.getInetAddress();
  55. }
  56.  
  57. public void login(int paId){
  58. try{
  59. for(Integer kluc : asClients.keySet()){
  60. asClients.get(kluc).sendLogin(paId);
  61. System.out.println("ahoj");
  62. }
  63. } catch (NullPointerException e){
  64.  
  65. }
  66. }
  67.  
  68. public void leave(int paId){
  69.  
  70. }
  71.  
  72. public void message(String nick, String message){
  73.  
  74. }
  75.  
  76. public void sendLogin(int paId){
  77. aOut.println("Klient "+paId+" sa pripojil.");
  78. aOut.flush();
  79. }
  80.  
  81. public void sendLeave(int paId){
  82.  
  83. }
  84.  
  85. public void sendMessage(String nick, String message){
  86.  
  87. }
  88. }
Add Comment
Please, Sign In to add comment