Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Socket;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import java.util.Scanner;
- /**
- *
- * @author Dell
- */
- public class client {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws Exception{
- // TODO code application logic here
- // Nối kết socket đến server
- // Nhận inputstream, outputstream
- try {
- Socket s = new Socket("127.0.0.1",7);
- // Nhận inputstream, outputstream
- InputStream is=s.getInputStream();
- OutputStream os=s.getOutputStream();
- // Gửi 1 kí tự cho server
- Scanner scanner = new Scanner(System.in);
- while(true){
- System.out.print("Nhập kí tự:");
- int ch = scanner.nextLine().charAt(0);
- // Kiểm tra điều kiện để thoát
- // System.out.print(ch + "\n");
- os.write(ch);
- if(ch=='@') break;
- // Nhận kí tự trả về từ server
- int ch1=is.read();
- // Hiển thị
- System.out.print("Kí tự nhận được:"+(char)ch1 + "\n");
- }
- s.close();
- }
- catch(IOException e){
- System.out.println("Lỗi kết nối");}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement