Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Java04_Day8_28-03_TCP-IP
- //Server
- public class MyServer{
- public void runTime(){
- try{
- ServerSocket server = new ServerSocket(5000);
- while(true){
- //cho thong tin tu client
- System.out.print("Server listen");
- Socket socket = server.accept();
- InputStream in = socket.getInputStream();
- DataInputStream din = new DataInputStream(in);
- String str = din.readUTF();
- System.out.println("Dao chuoi: \t" + str);
- //System.out.println("Thong tin tu client: \t" + new String(str,getByte(),"UTF-8"));
- //thuc hien logic cua chuong trinh
- char[] chs = str.toCharArray();
- String strNew = "";
- for(int i = chs.length -1; i>-1; i--){
- strNew += chs[i];
- }
- //tra ket qua cho client
- DataOutputStream dout = new DAtaOutputStream(socket.getOutputStream());
- dout.writeUTF(strNew);
- dout.close();
- }
- }catch(Exception ex){
- Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void main(String arg[]){
- MyServer obj = new MyServer();
- obj.runTime();
- }
- }
- //Client
- public class MyClass{
- public void sendRequest(){
- try{
- while(true){
- Socket client = new Socket("127.0.0.1", 5000);
- OutputStream out = client.getOutputStream();
- DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
- System.out.println("Nhap thong tin: ");
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- dout.writeUTF(str);
- InputStream in = socket.getInputStream();
- DataInputStream din = new DataInputStream(in);
- System.out.println("Ket qua\t:" + din.readUTF(str));
- }
- }catch(Exception ex){
- Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void main(String argp[]){
- MyClient obj = new MyClient();
- obj.sendRequest();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement