Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. public class Main implements Runnable {
  2.     public static void main(String[] args) {
  3.         Main main = new Main();
  4.         (new Thread(main)).start();
  5.     }
  6.  
  7.     @Override
  8.     public void run() {
  9.         try {
  10.             Socket socket = new Socket();
  11.  
  12.             socket.setSoTimeout(5000);
  13.             socket.connect(new InetSocketAddress("localhost", 12345), 5000);
  14.  
  15.             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
  16.             InputStream in = socket.getInputStream();
  17.             BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
  18.  
  19.             try {
  20.                 String userInput;
  21.                 while ((userInput = stdIn.readLine()) != null) {
  22.                     userInput = "?"+userInput;
  23.                    
  24.                     int len = userInput.length()+6;
  25.                    
  26.                     final String packet = "\\x00\\x83"+len+"\\x00\\x00\\x00\\x00\\x00"+userInput+"\\x00";
  27.  
  28.                     System.out.println("packet: " + packet);
  29.                     out.print(packet);
  30.                     System.out.println("Packet sent");
  31.                    
  32.                     System.out.println("Reading data");
  33.                     int read = -1;
  34.                     while ((read = in.read()) != -1) {
  35.                         System.out.println("echo: " + read);
  36.                     }
  37.                     System.out.println("Read complete");
  38.                 }
  39.             } finally {
  40.                 if(out != null) {
  41.                     out.close();
  42.                     out = null;
  43.                 }
  44.                 if(in != null) {
  45.                     in.close();
  46.                     in = null;
  47.                 }
  48.                 if(stdIn != null) {
  49.                     stdIn.close();
  50.                     stdIn = null;
  51.                 }
  52.                 if(socket != null) {
  53.                     socket.close();
  54.                 }
  55.             }
  56.         } catch (UnknownHostException e) {
  57.             e.printStackTrace();
  58.         } catch (IOException e) {
  59.             e.printStackTrace();
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement