SocketUitls

public class SocketUitls{
    private Context mContext;
    private Socket LoginSckt = null;
    private DataOutputStream LoginOut = null;// 登录发送socket流
    private DataInputStream LoginIn = null;// 登录取得socket流
    private Thread LoginThread = null;// 登录线程
    private boolean bLive;
    private String connectIP;
    private int connectProt;

    public SocketUitls( Context context) {
        this.mContext = context;
        bLive = true;
    }

    public void connect() {
//        this.connectIP = (String) SPUtils.get(Contans.SOCKET_IP, "");
//        this.connectProt = Integer.parseInt((String) SPUtils.get(Contans.MediaPort, ""));
//        LoginThread = new Thread(runnable);
//        LoginThread.setPriority(Thread.MAX_PRIORITY);
//        LoginThread.start();

    }





    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            LoginSckt = new Socket();
            try {
                LoginSckt.setKeepAlive(true);

                if (!LoginSckt.isConnected()) {
                    SocketAddress socketAddress = new InetSocketAddress(connectIP, connectProt);
                    LoginSckt.connect(socketAddress,500);//Socket链接
                    LoginSckt.setSoTimeout(500);//设置超时
                    LoginOut = new DataOutputStream(LoginSckt.getOutputStream());
                    LoginIn = new DataInputStream(LoginSckt.getInputStream());

                    readLoginSocket();
                } else {

                }
            } catch (IOException e) {

            }
        }
    };

    private void readLoginSocket() {
        bLive = true;
        int plen = 0;
        byte buf[] = new byte[8192];
        byte catchbuf[] = new byte[50*1024*1024];
        while (bLive) {
            try {
                writeData();
                int bufread = LoginIn.read(buf, 0, 500);
                if (bufread > 0) {
                    System.arraycopy(buf, 0, catchbuf, plen, bufread);
                    plen += bufread;
                    while (plen >= Contans.CmdHeadSize) {
                        CmdHeadCon cmdHead = new CmdHeadCon();
                        ParseCmdHead.parseCmdHead(cmdHead, catchbuf);
                        if (plen < Contans.CmdHeadSize + cmdHead._cmdSize) {
                            cmdHead = null;
                            break;
                        }

                        plen = plen - (CommonRecord.CmdHeadSize + cmdHead._cmdSize);
                        if (plen > 0) {
                            System.arraycopy(catchbuf, CommonRecord.CmdHeadSize + cmdHead._cmdSize, catchbuf, 0, plen);
                        }
                    }
                }
            } catch (Exception Io) {

            }
        }
        if (bLive) {

            bLive = false;
        }

    }


    // 资源释放
    public void closeSocketRelease() {
        try {
            bLive = false;

            if (LoginOut != null)
                LoginOut.close();
            if (LoginIn != null)
                LoginIn.close();
            if (LoginSckt != null)
                LoginSckt.close();
            LoginThread.join();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}

你可能感兴趣的:(网络请求工具类)