/* * To change this template, choose Tools | Templates * and open the template in the editor.
*/ package socketdaochuoi; import [Link].*; import [Link].*; /** * * @author Admin */ public class Server { public static void main(String args[]) { try { ServerSocket ss = new ServerSocket(9999);// Tao cong 9999 de serv er lang nghe while (true)// Cho client ket noi { // Su dung multithread // Khi co 1 client gui yeu cau toi thi se tao ra 1 thread phu c vu client do new ThreadSocket([Link]()).start(); } } catch (IOException e) { [Link]("Exception: " + [Link]()); } //catch (InterruptedException ie) { } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package socketdaochuoi; import [Link]; import [Link].*; /** * * @author Admin */ public class ThreadSocket extends Thread{ Socket socket= null; public ThreadSocket(Socket socket) {
[Link]=socket; } public void run() { try { DataOutputStream sendToClient= new DataOutputStream([Link] putStream());// Tao output stream BufferedReader fromClient= new BufferedReader(new InputStreamRead er([Link]()));//Tao input stream while (true) { String sentence=[Link]();// Chui nhn c t C lient [Link]("FROM CLIENT: " + sentence); if ([Link]("quit")) break; String reverseSentence= reverse(sentence); //[Link](10000); // Gi s khi x l n mt khong 5s [Link](reverseSentence+'\n'); } } catch (Exception e) { [Link](); } } public String reverse(String input) throws InterruptedException { String output=""; StringBuilder strBuilder= new StringBuilder(input); output=[Link]().toString(); return output; } } /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package socketdaochuoi; import [Link].*; import [Link]; /** * * @author Admin */ public class Client { public static void main(String args[]) { try {
String cau1;// Cau duoc gui toi server String ketQua;//Cau duoc server xu ly va tra lai la in hoa BufferedReader inFormUser= new BufferedReader(new InputStreamRead er([Link]));// Tao input stream Socket clientSocket= new Socket("[Link]",9999);// Tao clinent socket de ket noi toi server DataOutputStream sendToServer= new DataOutputStream(clientSocket. getOutputStream());// Tao output stream ket noi toi socket BufferedReader inFromServer = new BufferedReader(new InputStreamR eader([Link]())); while(true) { cau1 = [Link]();// Nhap vao 1 cau [Link](cau1+'\n');// gui toi server if ([Link]("quit"))// Gap chuoi quit break; ketQua = [Link]();// Nhan lai tu server [Link]("FROM SERVER: "+ketQua); } [Link]();//Dong ket noi } catch (IOException e) { [Link]("Exception Client: "+[Link]()); } } }