简体   繁体   中英

How can I read from java socket?

I'm trying to send and read the reply from the socket with no luck. What I got so far is, connected to the server and read connection reply (header from Ericsson telco exchange) and printed it on System.out. What I need to be able to do is send a command to the exchange and get the reply and that's where I'm stuck. Below is my test code.

Any help is appreciated.

public class ExchangeClientSocket {

  public void run() {
      try {
        int serverPort = 23;
        InetAddress host = InetAddress.getByName("my.host.ip.address");
        Socket socket = new Socket(host, serverPort);

        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(isr);

        int i;
        StringBuffer buffer = new StringBuffer();
        while (true) {
            i = in.read();
            if (i == 60) {
                break;
            } else {
                buffer.append((char) i);
            }
        }
        // this returns the head from exchange
        System.out.println(buffer.toString());

        out.write("allip;"); // command I want to send to exchange
        out.flush();

        System.out.println(in.ready()); // this returns false
        System.out.println(in.read());
        System.out.println("damn..");

        buffer = new StringBuffer();
        // can't get into this loop
        // this is where I want to read the allip response
        while ((i = in.read()) != -1) {
            i = in.read();
            if (i == 60) {
                break;
            } else {
                buffer.append((char) i);
            }
        }
        System.out.println(buffer.toString());

        out.close();
        in.close();
        socket.close();
      } catch (UnknownHostException ex) {
          ex.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
   }
}

Here is a suggestion on how to get input from the server after writing to the output stream: simply get another input stream from the socket .

So instead of reading from the same stream you create a new stream from the socket after you sent the command and read it.

I would therefore suggest to change your code to this here:

public void run() {
    try {
        int serverPort = 23;
        InetAddress host = InetAddress.getByName("my.host.ip.address");
        Socket socket = new Socket(host, serverPort);

        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(isr);

        int i;
        StringBuffer buffer = new StringBuffer();
        while (true) {
            i = in.read();
            if (i == 60) {
                break;
            } else {
                buffer.append((char) i);
            }
        }
        // this returns the head from exchange
        System.out.println(buffer.toString());

        out.write("allip;"); // command I want to send to exchange
        out.flush();

        // Create a new input stream here !!!
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        buffer = new StringBuffer();
        // can't get into this loop
        while ((i = in.read()) != -1) {
            i = in.read();
            if (i == 60) {
                break;
            } else {
                buffer.append((char) i);
            }
        }
        System.out.println(buffer.toString());

        out.close();
        in.close();
        socket.close();
    } catch (UnknownHostException ex) {
        ex.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM