代码


package com.example.myapplication;

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.net.UnknownHostException;


class ConnectThread  extends Thread{
    Socket socket = null;        //定义socket
    OutputStream outputStream = null;    //定义输出流(发送)
    InputStream inputStream=null;    //定义输入流(接收)
    String ip;
    int port;

    public ConnectThread(String valueOf, int intValue) {
        ip = valueOf;
        port=intValue;
        Log.d("MyTag", (String) ip + "hahahahha");
        Log.d("MyTag", String.valueOf(port) + "hahahha");

    }


        public static int returnActualLength(byte[] data) {
            int i = 0;
            for (; i < data.length; i++) {
                if (data[i] == '\0')
                    break;
            }
            return i;
        }




    public int bytesToInt(byte[] bytes) {
        int addr = bytes[0] & 0x000000FF;
        addr |= ((bytes[1] << 8) & 0x0000FF00);
        addr |= ((bytes[2] << 16) & 0x00FF0000);
        addr |= ((bytes[3] << 24) & 0xFF000000);
        return addr;
    }
    //java 合并两个byte数组
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){
        byte[] byte_3 = new byte[byte_1.length+byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }

    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2 , int len2){
        byte[] byte_3 = new byte[byte_1.length+len2];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, len2);
        return byte_3;
    }

    /**
     * byte数组转换为Stirng
     * @param s1 -数组
     * @param encode -字符集
     * @param err -转换错误时返回该文字
     * @return
     */
    public static String getString(byte[] s1,String encode,String err){
        try {
            return new String(s1,encode);
        } catch (UnsupportedEncodingException e) {
            return err==null?null:err;
        }
    }
    /**
     * byte数组转换为Stirng
     * @param s1-数组
     * @param encode-字符集
     * @return
     */
    public static String getString(byte[] s1,String encode){
        return getString(s1,encode,null);
    }

    public void run(){
        try {

            Log.d("MyTag", (String) ip + "hahahahha");
            Log.d("MyTag", String.valueOf(port) + "hahahha");
            socket = new Socket(ip, port);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("MyTag", "error");
        }

        //获取输出流
        try {
            outputStream = socket.getOutputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // read
        int size =0;
        byte[] sizebuff = new byte[4];
        byte[] data = new byte[0];

        try{
            while (true) //  if (unlink) break;
            {
                 byte[] buffer = new byte[1024];//创建接收缓冲区
                inputStream = socket.getInputStream();
                if (size == 0 ){

                    int len = inputStream.read(buffer,0,4);//数据读出来,并且返回数据的长度
                    Log.d("MyTag", String.valueOf(buffer[0])+"    "+String.valueOf(buffer[1])+"    "+String.valueOf(buffer[2])+"    "+String.valueOf(buffer[3]));
                    size = bytesToInt(buffer);
                }else{
                    Log.d("MyTag", String.valueOf(size)+"              Size");
                    int len = inputStream.read(buffer,0,size);//数据读出来,并且返回数据的长度
                    Log.d("MyTag", String.valueOf(len)+"              len");

                    size = size - len;
                    data = byteMerger(data,buffer,returnActualLength(buffer));
                    Log.d("MyTag", String.format("%d  data  is ok", returnActualLength(buffer)));
                    if(size==0){ // data ok
                        String res =  new String(data);;
                        Log.d("MyTag", res);
                    }
                }
                sleep(1000);
            }
        }
        catch (IOException | InterruptedException e) {

        }

    }

}

使用


                    ConnectThread ct = new ConnectThread(String.valueOf(Edit_IP.getText()), Integer.parseInt(String.valueOf(Edit_Port.getText())));
                    ct.start();

Last modification:August 12th, 2020 at 02:11 pm
如果觉得我的文章对你有用,请随意赞赏