readthread.java

来自「农业银行ATM对帐查询系统后台类包的源代码,实现了FTP,数据库管理等等功能」· Java 代码 · 共 85 行

JAVA
85
字号
package boco.webtrade;import java.net.*;import java.lang.*;import java.io.*;/** * 一个专用的从socket连接中读取数据的线程. */public class ReadThread extends Thread{    protected Socket connectionSocket;      // the socket you are reading from    protected DataInputStream inStream;     // the input stream from the socket    protected ReadCallback readCallback;    private   int timeout=15000;    public String recvstr="";               //从socket连接中收到返回的字符串/** * Creates an instance of a ReadThread on a Socket and identifies the callback * that will receive all data from the socket. * * @param callback the object to be notified when data is ready * @param connSock the socket this ReadThread will read data from * @exception IOException if there is an error getting an input stream *          for the socket */    public ReadThread(ReadCallback callback, Socket connSock) throws IOException    {        connectionSocket = connSock;        connectionSocket.setSoTimeout(timeout);        readCallback = callback;        inStream = new DataInputStream(connSock.getInputStream());    }/** * 用关闭socket的方法关闭socket连接 */    protected void closeConnection()    {        try {            connectionSocket.close();        } catch (Exception oops) {        }        interrupt();    }/** * Continuously reads a string from the socket and then calls dataReady in the * read callback. If you want to read something other than a string, change * this method and the dataReady callback to handle the appropriate data. */    public void run()    {            try {//将传递进来的输入源映射成DataInputStream对象,以便从流中读取数据                BufferedReader reader=new BufferedReader(new InputStreamReader(inStream));                //从流中读取一行字符串                String str=reader.readLine();                //存储读入的数据                String strin="";                while(str!=null){                    //通报callback并打印出读入的数据                    readCallback.dataReady(str);                    //把读入的数据加到字符串后面                    strin+=str;                    str=reader.readLine();                }                reader.close();                recvstr=strin;            }            catch (Exception oops) {//通知callback读入数据出错                readCallback.dataReady(null);                oops.printStackTrace();            }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?