Socket编程之SocketClientThread

安卓端子线程请求socket

package yskj.buletooth;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import util.L;


/**
 * Created by admin on 2016/3/21.
 */
public class ServerThread extends Thread{

    private final static String IP = "192.168.0.2";
    private final static int PORT = 30001;
    private final static int TIME_OUT = 5000;

    private boolean isPause = false;
    private boolean isClose = false;
    private List<String> loopList = new ArrayList<String>();

    private  ServerThread(){

    }

    private static ServerThread single = null;

    public synchronized static ServerThread getInstance() {
        if (single == null) {
            single = new ServerThread();
        }
        return single;
    }

    public synchronized void addString(String str) {
        loopList.add(str);
        //有数据要处理时唤醒线程
        this.notify();
    }

    /**
     * 暂停线程
     */
    public synchronized void onThreadPause() {
        isPause = true;
    }


    /**
     * 线程等待,不提供给外部调用
     */
    private void onThreadWait() {
        try {
            synchronized (this) {
                this.wait();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 线程继续运行
     */
    public synchronized void onThreadResume() {
        isPause = false;
        this.notify();
    }


    /**
     * 关闭线程
     */
    public synchronized void closeThread() {
        try {
            notify();
            setClose(true);
            interrupt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public boolean isClose() {
        return isClose;
    }

    public void setClose(boolean isClose) {
        this.isClose = isClose;
    }

    private ThreadCallBack callBack;

    public void setCallBack(ThreadCallBack callBack)
    {
        this.callBack = callBack;
    }


    @Override
    public void run() {
        super.run();

        try {
            while (!isClose && !isInterrupted())
            {
                if ( !isPause){

                    Socket socket = new Socket();
                    socket.connect(new InetSocketAddress(IP, PORT), TIME_OUT);
                    InputStream in = socket.getInputStream();
                    OutputStream out = socket.getOutputStream();

                    BufferedReader bff = new BufferedReader(new InputStreamReader(in));
                    String line = null;
                    String buffer = "";
                    while ((line = bff.readLine()) != null) {
                        buffer = line + buffer;
                    }

                    out.write(("android has recever - " + System.currentTimeMillis()).getBytes("utf-8"));
                    out.flush();

                    callBack.response(buffer);

                    bff.close();
                    out.close();
                    socket.close();


                }else{
                    onThreadWait();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public interface ThreadCallBack
    {
        void response(String response);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值