简体   繁体   English

我如何从Java套接字读取?

[英]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. 到目前为止,我得到的是连接到服务器并读取连接答复(来自爱立信电信交换机的标题),并将其打印在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 . 这是关于在写入输出流后如何从服务器获取输入的建议: 只需从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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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