简体   繁体   English

使用线程从InputStream读取数据

[英]Read data from InputStream using Threads

I tried to use read() method from InputStream class but it blocks the thread, and i tried readLine() method that blocks the thread too. 我尝试使用InputStream类中的read()方法,但它阻塞了线程,并且我尝试了也阻塞线程的readLine()方法。

After this attempts I used this: 经过这种尝试,我使用了这个:

    public static void main(String[] args) throws IOException{
        Socket s = new Socket();
        String host = "";
        s.connect(new InetSocketAddress(host, 23));

        Response response = new Response(s);

        response .start();

        if(response .isInterrupted()){
            System.out.println(response .getSb().toString());
        }


    }

public class Response extends Thread {
    Socket s;
    BufferedReader s_in = null;
    InputStream in = null;
    private StringBuffer sb = new StringBuffer();

    public Response(Socket s){
        this.s = s;
    }

    @Override
    public void run() {
        try {
            s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            in = s.getInputStream();

            char ch;
            int i;


            while(!Thread.currentThread().isInterrupted()){
                i = s_in.read();
                ch = (char) i;
                System.out.print(ch);
                getSb().append(getSb());
            }

            System.out.print(getSb());

        } catch (IOException ex) {
            Logger.getLogger(Response.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public StringBuffer getSb() {
        return sb;
    }
}

public class Message extends Thread{
    Socket s;
    PrintWriter s_out = null;

    public Message(Socket s){
        this.s = s;
    }

    @Override
    public void run(){
        try {
            s_out = new PrintWriter(s.getOutputStream(), true);
        } catch (IOException ex) {
            Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

But when I run the application the socket does not have enough time to send all the data to InputStream. 但是,当我运行该应用程序时,套接字没有足够的时间将所有数据发送到InputStream。

So, I might verify when the stream ends. 因此,我可能会验证流何时结束。 To start reading. 开始阅读。

Thanks for helping ! 感谢您的帮助!

I don't know why you aren't using a buffered stream, or putting this reader in it's own thread, since it should always be reading (or ready to read) from the socket, which means blocking. 我不知道您为什么不使用缓冲流,或者不将此读取器放在自己的线程中,因为它应该始终从套接字读取(或准备读取),这意味着阻塞。

But, with the code you've provided, the easiest way to improve your situation is to read until there's no more bytes to read, which is not what you're actually doing. 但是,使用您提供的代码,改善情况的最简单方法是读取,直到没有更多字节要读取为止,这实际上不是您要做的。 More bytes could have come in while you're executing the for loop. 在执行for循环时,可能会输入更多字节。 Try this instead: 尝试以下方法:

int i;
char c;
while (in.available() > 0)
{
    i = s_in.read();
    System.out.print(ch = (char)i);
    sb.append(ch);
}

It really doesn't solve your problem, because you're not buffering your input with a separate thread, and you can still exit the loop before you receive all the data you expected to get. 它确实不能解决您的问题,因为您没有使用单独的线程来缓冲输入,并且仍然可以在收到期望获得的所有数据之前退出循环。

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

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