Android端通过Usb建立Socket通讯(实时发送视频数据)

1.创建出 ServerSocket

serverSocket = new ServerSocket(端口10086);

2.adnroid端保持等待客户端等待连接状态

    Socket accept = serverSocket.accept();

3.添加网络请求权限

  <uses-permission android:name="android.permission.INTERNET" />

4.使用ADB命令端口映射 转端口

adb forward tcp:12580 tcp:10086

例:adb forward tcp:PC端连接的端口(12580) tcp:Android写的端口(10086)

5.pc端建立客户端连接
客户端连接的IP是127.0.0.1(就是本地IP),端口是:映射后的端口-以上面为例(12580)

            //推荐一个好用的调试工具 SocketTools 

贴上代码

package socketdemosss.xiake.com.testdemo.Server;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

import socketdemosss.xiake.com.testdemo.bean.AnyEventTypeTest;

/**
 * Created by Administrator on 2017/6/29.
 */

public class SocketServer extends IntentService {
    private int PROT = 10086;
    private Socket accept;

    private  InputStream in =null;
    public SocketServer() {
        super("SocketServer");

    }
    @Override
    protected void onHandleIntent(Intent intent) {
        ServerSocket serverSocket = null;
        BufferedOutputStream out = null;
        //out = new BufferedOutputStream(accept.getOutputStream());

        try {
            serverSocket = new ServerSocket(PROT);
          accept = serverSocket.accept();
        } catch (IOException e) {
            e.printStackTrace();
        }
            while (true){
              try {
                while (accept.isConnected() == true){
                             EventBus.getDefault().post(new AnyEventTypeTest("连接上了"));
                                 in = accept.getInputStream();
                                 String request = null;
                                 request = readCMDFromSocket(in);
                                 EventBus.getDefault().post(new AnyEventTypeTest(request));
                }


            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }


    /* 读取命令 */
    public static String readCMDFromSocket(InputStream in) {


            int MAX_BUFFER_BYTES = 2048;
            String msg = "";
            byte[] tempbuffer = new byte[MAX_BUFFER_BYTES];
            try {
                int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
                msg = new String(tempbuffer, 0, numReadedBytes, "gbk");
                tempbuffer = null;

            } catch (Exception e) {

            }
            // Log.v(Service139.TAG, "msg=" + msg);
            return msg;


    }
//        while (true){
//            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
//            String r = null;
//            try {
//                r = bufferedReader.readLine();
//                if (r.contains("\\&")){
//                    r = r.replaceAll("\\\\&","\n");
//
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//
//            return r;
//
//        }}


    }

你可能感兴趣的:(Android)