Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 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 Socket;
  7.  
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.net.Socket;
  12. import java.util.Scanner;
  13.  
  14. /**
  15. *
  16. * @author Dell
  17. */
  18. public class client {
  19.  
  20. /**
  21. * @param args the command line arguments
  22. */
  23. public static void main(String[] args) throws Exception{
  24. // TODO code application logic here
  25. // Nối kết socket đến server
  26. // Nhận inputstream, outputstream
  27. try {
  28. Socket s = new Socket("127.0.0.1",7);
  29. // Nhận inputstream, outputstream
  30. InputStream is=s.getInputStream();
  31. OutputStream os=s.getOutputStream();
  32. // Gửi 1 kí tự cho server
  33.  
  34. Scanner scanner = new Scanner(System.in);
  35. while(true){
  36. System.out.print("Nhập kí tự:");
  37. int ch = scanner.nextLine().charAt(0);
  38. // Kiểm tra điều kiện để thoát
  39. // System.out.print(ch + "\n");
  40. os.write(ch);
  41. if(ch=='@') break;
  42. // Nhận kí tự trả về từ server
  43. int ch1=is.read();
  44. // Hiển thị
  45. System.out.print("Kí tự nhận được:"+(char)ch1 + "\n");
  46. }
  47. s.close();
  48. }
  49. catch(IOException e){
  50. System.out.println("Lỗi kết nối");}
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement