Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3.  
  4. public class Client
  5. {
  6. public static void main(String[] args) throws IOException
  7. {
  8. Socket socket = null;
  9. PrintWriter out = null;
  10. BufferedReader in = null;
  11.  
  12. try
  13. {
  14. //aby utworzyć Socket trzeba podać adres i port serwera
  15. socket = new Socket("szymon.ia.agh.edu.pl", 3000);
  16.  
  17. //PrintWriter wypisuje sformatowaną reprezentację obiektów
  18. out = new PrintWriter(socket.getOutputStream(), true);
  19.  
  20. //BufferedReader czyta tekst ze strumienia wejściowego, używa buforowania (czyta po kawałku)
  21. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  22. }
  23. catch(UnknownHostException e)
  24. {
  25. System.err.println("Don't know about host: szymon.ia.agh.edu.pl");
  26. System.exit(1);
  27. }
  28. catch(IOException e)
  29. {
  30. System.err.println("Couldn't get I/O for the connection to: szymon.ia.agh.edu.pl.");
  31. System.exit(1);
  32. }
  33.  
  34. BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
  35. String userInput;
  36.  
  37. System.out.println("Type: ");
  38. while((userInput = stdIn.readLine()) != null) //dopóki nie ma końca znaku
  39. {
  40. out.println(userInput); //pisanie do gniazda
  41. System.out.println(in.readLine()); //czytanie z gniazda
  42. }
  43.  
  44. out.close();
  45. in.close();
  46. stdIn.close();
  47. socket.close();
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement