测试Socket协议

package com.hf_socket;

import android.app.Activity;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/16.
 */

public class MyClientTest implements Runnable {
    public static Socket client = null;
    public String serverIP="";
    public  boolean socketState= true;
    public Message msg;
    String TAG="RIDi测试==";

    public final String  Download="26";//0x26
    public final String  DownloadCommandSucceed="62";//0x62
    public final String  DownloadFileSucceed="E2";//0xE2
    public final String  ResendCommand ="E2";//0xE2

    public Socket_Activity.SocketHandler socketHandler;
    public MyClientTest(String serverIP, Socket_Activity.SocketHandler handler) {
        this.serverIP=serverIP;
        this.socketHandler=handler;
        this.msg=new Message();
    }

    @Override
    public void run() {
        Looper.prepare();
        socketState=true;
        try {
            client = new Socket(serverIP, 9000);
            client.setKeepAlive(true);
            client.setSoTimeout(5000);
            client.setTcpNoDelay(true);
            client.setSoLinger(true, 4);
            InputStream instream =client.getInputStream();
            OutputStream outstream =client.getOutputStream();
            while (socketState== Utils.network(MyApplication.getContext())) {
                long  needSendfileLength= 1024;//Utils.getFileLength(MyApplication.PERSIN_IDENTIFY_FILE);
                byte[] mm= getWrite("FF55","26",40,2000,1,"tmp48A.tmp",needSendfileLength,"AAEE");
                Log.e("发送数据==",""+mm.length);
                outstream.write(mm);






                byte[] bufferAll = new byte[1024];
                int readLength = instream.read(bufferAll, 0, 1024);//协议数据 receive Datalength <1024
                // frameHeader=2byte state=1byte packageLength=4byte dataBody=packageLength(byte) checksum=8byte frameTail=2byte
                //dataBody【Version=4byte  FileType=1byte FileName=10byte FileLength=8byte 】
                //Determine the heads
                Log.e(TAG+"链接成功==","读取字节个数=="+readLength);
                byte[] bufferFH = new byte[4];
                System.arraycopy(bufferAll,0,bufferFH,0,bufferFH.length);
                String FH= Tools.hexStr2Str(Tools.Bytes2HexString(bufferFH,bufferFH.length));//The frame Tail ; 46463535=FF55

                byte[] bufferFT = new byte[4];
                System.arraycopy(bufferAll,readLength-bufferFT.length,bufferFT,0,bufferFT.length);
                String FT= Tools.hexStr2Str(Tools.Bytes2HexString(bufferFT,bufferFT.length));//The frame Tail

                Log.e(TAG+"==","==bufferFH=="+FH+"==bufferFT=="+FT);


                if((FH.contains("FF55")||FH.contains("ff55"))&&
                        (FT.contains("AAEE")||FT.contains("aaee"))){
                    byte[] bufferSTSTE = new byte[2];
                    int stateIndex=bufferFH.length;
                    System.arraycopy(bufferAll,stateIndex,bufferSTSTE,0,bufferSTSTE.length);
                    String STSTE= Tools.hexStr2Str(Tools.Bytes2HexString(bufferSTSTE,bufferSTSTE.length));//3236
                    Log.e(TAG+"",STSTE+"==STSTE==");

                    byte[] bufferPACKAGELENGTH = new byte[8];
                    int dataLengthIndex=stateIndex+bufferSTSTE.length;
                    System.arraycopy(bufferAll,dataLengthIndex,bufferPACKAGELENGTH,0,bufferPACKAGELENGTH.length);
                    String dataLength= Tools.hexStr2Str(Tools.Bytes2HexString(bufferPACKAGELENGTH,bufferPACKAGELENGTH.length));
                    int PACKAGELENGTH = Integer.valueOf(dataLength,16);
                    Log.e(TAG+"==","==PACKAGELENGTH=="+PACKAGELENGTH);

                    //判断包长 与读取个数是否相等
                   // if(PACKAGELENGTH==(readLength+12) ){
                        byte[] bufferVERSION = new byte[8];
                        int versionIndex=dataLengthIndex+bufferPACKAGELENGTH.length;
                        System.arraycopy(bufferAll,versionIndex,bufferVERSION,0,bufferVERSION.length);
                        String version= Tools.hexStr2Str(Tools.Bytes2HexString(bufferVERSION,bufferVERSION.length));
                        int VERSION = Integer.valueOf(version,16);

                        Log.e(TAG+"==","==VERSION=="+VERSION);
                        byte[] bufferFILETYPE = new byte[2];
                        int filetypeIndex= versionIndex+bufferVERSION.length;
                        System.arraycopy(bufferAll,filetypeIndex,bufferFILETYPE,0,bufferFILETYPE.length);
                        String filetype= Tools.hexStr2Str(Tools.Bytes2HexString(bufferFILETYPE,bufferFILETYPE.length));
                        int  FILETYPE= Integer.valueOf(filetype,16);
                        Log.e(TAG+"==","==FILETYPE=="+FILETYPE);

                        byte[] bufferFILENAME = new byte[20];
                        int fileNameIndex=filetypeIndex+bufferFILETYPE.length;
                        System.arraycopy(bufferAll,fileNameIndex,bufferFILENAME,0,bufferFILENAME.length);
                        String   fileName = new String(bufferFILENAME, 0, bufferFILENAME.length, "utf-8");
                        String FILENAME= Tools.hexStr2Str(fileName);
                        Log.e(TAG+"==","==FILENAME=="+"=="+FILENAME);


                        byte[] bufferCHECKSUM = new byte[4];//CHECKSUM define=8byte,but  read=2byte
                        System.arraycopy(bufferAll,readLength-bufferFT.length-bufferCHECKSUM.length,bufferCHECKSUM,0,bufferCHECKSUM.length);
                        String checkSum= Tools.hexStr2Str(Tools.Bytes2HexString(bufferCHECKSUM,bufferCHECKSUM.length));//The checksum
                        long CHECKSUM=   Long.valueOf(checkSum,16);
                        Log.e(TAG+"CHECKSUM==","=="+CHECKSUM);


                        byte[] bufferFILELENGTH = new byte[16];
                        int fileLengthindex= readLength-bufferFT.length-bufferCHECKSUM.length-bufferFILELENGTH.length;
                        System.arraycopy(bufferAll,fileLengthindex,bufferFILELENGTH,0,bufferFILELENGTH.length);
                        String fileLength= Tools.hexStr2Str(Tools.Bytes2HexString(bufferFILELENGTH,bufferFILELENGTH.length));
                        long FILELENGTH=Long.parseLong(fileLength,16);
                        Log.e(TAG+"==","==FILELENGTH=="+FILELENGTH);
                        //校验和
                        byte[] check1 = Utils.byteMerger(bufferFH,bufferPACKAGELENGTH);
                        byte[] check2 = Utils.byteMerger(check1, bufferVERSION);
                        byte[] check3 = Utils.byteMerger(check2, bufferFILETYPE);
                        byte[] check4 = Utils.byteMerger(check3, bufferFILELENGTH );
                        byte[] check5 = Utils.byteMerger(check4, bufferFILENAME);
                        long  sum=0;
                        for(int i=0;i0){
            for(Activity context1:Socket_Activity.socket_ActivityContextList){
                try {
                    Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
                context1.finish();
            }
        }

    }












}

你可能感兴趣的:(android)