Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package echoserwer;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.DataOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.net.ServerSocket;
  13. import java.net.Socket;
  14.  
  15. /**
  16. *
  17. * @author kie
  18. */
  19. public class EchoServerThread implements Runnable{
  20.  
  21. protected Socket socket;
  22. public EchoServerThread(Socket clientSocket){
  23. this.socket = clientSocket;
  24.  
  25. }
  26.  
  27. public void run(){
  28.  
  29. BufferedReader brinp = null;
  30. DataOutputStream out = null;
  31. //OutputStream out;
  32. // InputStream brinp;
  33. try{
  34. brinp = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  35. out = new DataOutputStream(socket.getOutputStream());
  36. String threadName = Thread.currentThread().getName();
  37.  
  38. // out = socket.getOutputStream();
  39. // brinp= socket.getInputStream();
  40. }
  41. catch(IOException e){
  42. System.out.println("Błąd przy tworzeniu strumieni.");
  43.  
  44. System.exit(-1);
  45. } System.out.println("Zakończona inicjalizacja strumieni...");
  46.  
  47. System.out.println("Rozpoczęcie pętli głównej...");
  48. while(true){
  49. try{
  50. String line = brinp.readLine();
  51. // int line = brinp.read();
  52. System.out.println("Odczytano linię: " + line);
  53. if(line == null || "quit".equals(line)){
  54. try{ socket.close();
  55.  
  56. }
  57. catch(IOException e){
  58. System.out.println( "Błąd przy zamykaniu gniazda serwerowego.");
  59. }
  60. System.out.println("Zakończenie pracy...");
  61. System.exit(0);
  62. }
  63. out.writeBytes(line + "\n");
  64. // out.write(line + "\n\r");
  65. System.out.println("Wysłano linię: " + line);
  66.  
  67. }
  68. catch(IOException e){
  69. System.out.println("Błąd wejścia-wyjścia.");
  70. System.exit(-1);
  71. }
  72. }
  73. } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement