Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 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 klientserwer;
  7.  
  8. import java.net.*;
  9. import java.io.*;
  10.  
  11. /**
  12. *
  13. * @author hubert
  14. */
  15.  
  16. class Klient_Watek1 extends Thread
  17. {
  18. Socket socket;
  19. InputStream in;
  20. OutputStream out;
  21. BufferedReader fromKeyboard = new BufferedReader(new InputStreamReader(System.in));
  22.  
  23. Klient_Watek1(Socket s, InputStream in, OutputStream out)
  24. {
  25. socket =s;
  26. this.in = in;
  27. this.out=out;
  28. }
  29.  
  30. @Override
  31. public void run()
  32. {
  33. try
  34. {
  35. int inf=0;
  36.  
  37. while(inf==0)
  38. {
  39. System.out.print("Klient wysyła-> ");
  40.  
  41. //Czytanie danych ze standardowego urzadzenia wejscia(klawiatury) po linii
  42. String data = fromKeyboard.readLine();
  43. //wysylanie danych strumieniem wyjsciowym do serwera
  44. out.write(data.getBytes());
  45. out.write("\r\n".getBytes());// dokladanie znakĂłw konca wiersza
  46.  
  47. if(data.toLowerCase().equals("exit"))
  48. {
  49. // W pzypadku gdy uzytkownik wpisal 'exit' zamykamy strumienie, polaczenie oraz aplikacje
  50. in.close();
  51. out.close();
  52. socket.close();
  53. inf=1;
  54. }
  55. }
  56. }catch(IOException ioe) {
  57. }
  58. }
  59. }
  60.  
  61. class Odbieranie extends Thread
  62. {
  63.  
  64. }
  65.  
  66. public class Klient1 {
  67.  
  68. /**
  69. * @param args the command line arguments
  70. */
  71. public static void main(String[] args) {
  72.  
  73. try{
  74.  
  75. Socket socket = new Socket("localhost", 2222);
  76. System.out.println("Polaczony ...");
  77.  
  78. InputStream in = socket.getInputStream();
  79. OutputStream out = socket.getOutputStream();
  80.  
  81. Klient_Watek1 t = new Klient_Watek1(socket, in, out);
  82. t.start();
  83.  
  84.  
  85. while(true){
  86.  
  87. int k = 0;
  88. StringBuffer sb = new StringBuffer();
  89. while((k=in.read())!=-1 && k!='\n')// czytanie po jednym bajcie
  90. sb.append((char)k); // zamiana bajtu na char i wstawianie do bufora
  91.  
  92. //pobieranie danych z bufora i obcinanie pustych znakĂłw
  93. String data = sb.toString().trim();
  94.  
  95. if(data.toLowerCase().equals("exit"))
  96. {
  97. in.close();
  98. out.close();
  99. socket.close();
  100. System.exit(0);
  101. }
  102.  
  103. System.out.println(data);
  104.  
  105. }
  106.  
  107. // obsluga wyjatkow
  108. }catch(UnknownHostException uhe){
  109. System.err.println(uhe);
  110. }catch(IOException ioe){
  111. System.err.println(ioe);
  112. }
  113.  
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement