Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main implements Runnable {
- public static void main(String[] args) {
- Main main = new Main();
- (new Thread(main)).start();
- }
- @Override
- public void run() {
- try {
- Socket socket = new Socket();
- socket.setSoTimeout(5000);
- socket.connect(new InetSocketAddress("localhost", 12345), 5000);
- PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
- InputStream in = socket.getInputStream();
- BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
- try {
- String userInput;
- while ((userInput = stdIn.readLine()) != null) {
- userInput = "?"+userInput;
- int len = userInput.length()+6;
- final String packet = "\\x00\\x83"+len+"\\x00\\x00\\x00\\x00\\x00"+userInput+"\\x00";
- System.out.println("packet: " + packet);
- out.print(packet);
- System.out.println("Packet sent");
- System.out.println("Reading data");
- int read = -1;
- while ((read = in.read()) != -1) {
- System.out.println("echo: " + read);
- }
- System.out.println("Read complete");
- }
- } finally {
- if(out != null) {
- out.close();
- out = null;
- }
- if(in != null) {
- in.close();
- in = null;
- }
- if(stdIn != null) {
- stdIn.close();
- stdIn = null;
- }
- if(socket != null) {
- socket.close();
- }
- }
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement