09 April 2016

StringClient StringServer

package network.sample;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class StringClient {
 public static void main(String args[]) {
  try {
   Socket myClient = new Socket("localhost", 9768);
   
   DataOutputStream dos = new DataOutputStream(myClient.getOutputStream());
   DataInputStream dis = new DataInputStream(myClient.getInputStream());
   
   dos.writeUTF("String");
   String result = dis.readUTF();
   
   System.out.println("Result: " + result);
   
   myClient.close();
   
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
}

package network.sample;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class StringServer {
 public static void main(String args[]) {
  try {
   ServerSocket myServer = new ServerSocket(9768);
   
   Socket serverSocket = myServer.accept();
   
   DataOutputStream dos = new DataOutputStream(serverSocket.getOutputStream());
   DataInputStream dis = new DataInputStream(serverSocket.getInputStream());
   
   while(true){
    String str = dis.readUTF();
    StringBuilder strBuilder = new StringBuilder(str);
    String result = strBuilder.reverse().toString();
    
    dos.writeUTF(result);
    break;
   }
   myServer.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  
 }
}

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang