Advertisement
MrHoangIt

Java04_Day8_28-03_TCP-IP

Mar 28th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. //Java04_Day8_28-03_TCP-IP
  2.  
  3. //Server
  4. public class MyServer{
  5.     public void runTime(){
  6.         try{
  7.             ServerSocket server = new ServerSocket(5000);
  8.             while(true){
  9.                 //cho thong tin tu client
  10.                 System.out.print("Server listen");
  11.                 Socket socket = server.accept();
  12.                 InputStream in = socket.getInputStream();
  13.                 DataInputStream din = new DataInputStream(in);
  14.                 String str = din.readUTF();
  15.                 System.out.println("Dao chuoi: \t" + str);
  16.                 //System.out.println("Thong tin tu client: \t" + new String(str,getByte(),"UTF-8"));
  17.                
  18.                 //thuc hien logic cua chuong trinh
  19.                 char[] chs = str.toCharArray();
  20.                 String strNew = "";
  21.                 for(int i = chs.length -1; i>-1; i--){
  22.                     strNew += chs[i];
  23.                 }
  24.                
  25.                 //tra ket qua cho client
  26.                 DataOutputStream dout = new DAtaOutputStream(socket.getOutputStream());
  27.                 dout.writeUTF(strNew);
  28.                 dout.close();
  29.                
  30.             }
  31.         }catch(Exception ex){
  32.             Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);
  33.         }
  34.     }
  35.    
  36.     public static void main(String arg[]){
  37.         MyServer obj = new MyServer();
  38.         obj.runTime();
  39.     }
  40.  
  41. }
  42.  
  43. //Client
  44.  
  45.  
  46. public class MyClass{
  47.     public void sendRequest(){
  48.         try{
  49.                 while(true){
  50.                     Socket client = new Socket("127.0.0.1", 5000);
  51.                     OutputStream out = client.getOutputStream();
  52.                     DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
  53.                     System.out.println("Nhap thong tin: ");
  54.                    
  55.                     Scanner sc = new Scanner(System.in);
  56.                     String str = sc.nextLine();        
  57.                     dout.writeUTF(str);
  58.                    
  59.                     InputStream in = socket.getInputStream();
  60.                     DataInputStream din = new DataInputStream(in);
  61.                     System.out.println("Ket qua\t:" + din.readUTF(str));
  62.                     }
  63.         }catch(Exception ex){
  64.             Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);
  65.         }
  66.     }
  67.    
  68.     public static void main(String argp[]){
  69.         MyClient obj = new MyClient();
  70.         obj.sendRequest();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement