├── .gitignore ├── PC ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── example │ └── PCClient.java ├── README.MD ├── app ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── gavinliu │ │ └── android_pc_socket_connection │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── gavinliu │ │ │ └── android_pc_socket_connection │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── cn │ └── gavinliu │ └── android_pc_socket_connection │ └── ExampleUnitTest.java ├── build.gradle ├── demo.gif └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | #Android generated 2 | bin 3 | gen 4 | gen* 5 | 6 | #Eclipse 7 | .project 8 | .classpath 9 | .settings 10 | 11 | #IntelliJ IDEA 12 | .idea 13 | *.iml 14 | *.ipr 15 | *.iws 16 | out 17 | 18 | #Maven 19 | target 20 | release.properties 21 | pom.xml.* 22 | 23 | #Ant 24 | build.xml 25 | local.properties 26 | proguard.cfg 27 | 28 | #Gradle 29 | .gradle 30 | build 31 | 32 | #OSX 33 | .DS_Store 34 | 35 | #Personal Files 36 | signing. 37 | 38 | 39 | # Built application files 40 | *.apk 41 | *.ap_ 42 | 43 | # Files for the Dalvik VM 44 | *.dex 45 | 46 | # Java class files 47 | *.class 48 | 49 | # Generated files 50 | bin/ 51 | gen/ 52 | 53 | # Gradle files 54 | .gradle/ 55 | build/ 56 | 57 | # Local configuration file (sdk path, etc) 58 | local.properties 59 | 60 | # Proguard folder generated by Eclipse 61 | proguard/ 62 | 63 | # Log Files 64 | *.log 65 | 66 | gradle.properties 67 | gradle/ 68 | gradlew 69 | gradlew.bat -------------------------------------------------------------------------------- /PC/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: ['*.jar']) 5 | } -------------------------------------------------------------------------------- /PC/src/main/java/com/example/PCClient.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.io.DataInputStream; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | import java.net.Socket; 7 | import java.util.Scanner; 8 | 9 | public class PCClient { 10 | 11 | public static void main(String[] args) throws IOException { 12 | System.out.println("任意字符, 回车键发送Toast"); 13 | Scanner scanner = new Scanner(System.in); 14 | while (true) { 15 | String msg = scanner.next(); 16 | sendToast(msg); 17 | } 18 | } 19 | 20 | public static void sendToast(String msg) throws IOException { 21 | Socket socket = new Socket("127.0.0.1", 8000); 22 | DataInputStream dis = new DataInputStream(socket.getInputStream()); 23 | DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 24 | dos.writeUTF(msg); 25 | socket.close(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | ## Android-Pc-Socket-Connection 2 | 3 | ![](/demo.gif) 4 | 5 | ### step 1 6 | 7 | ``` 8 | adb forward tcp:8000 tcp:9000 9 | ``` 10 | 11 | ### step 2 12 | 13 | run ``app`` 14 | 15 | 16 | ### step 3 17 | 18 | run `PCClient` 19 | 20 | ## Blog 21 | [Android-adb-forward实现PC和Android的Socket通讯](http://gavinliu.cn/2016/01/19/Android-adb-forward%E5%AE%9E%E7%8E%B0PC%E5%92%8CAndroid%E7%9A%84Socket%E9%80%9A%E8%AE%AF/) 22 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | applicationId "cn.gavinliu.android_pc_socket_connection" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.1.1' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/gavin/Develop/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/cn/gavinliu/android_pc_socket_connection/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cn.gavinliu.android_pc_socket_connection; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/cn/gavinliu/android_pc_socket_connection/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.gavinliu.android_pc_socket_connection; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.os.Message; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.widget.Toast; 8 | import android.util.Log; 9 | 10 | import java.io.DataInputStream; 11 | import java.io.DataOutputStream; 12 | import java.net.ServerSocket; 13 | import java.net.Socket; 14 | 15 | public class MainActivity extends AppCompatActivity { 16 | 17 | private static final String TAG = "ServerThread"; 18 | 19 | ServerThread serverThread; 20 | 21 | Handler handler = new Handler() { 22 | 23 | @Override 24 | public void handleMessage(Message msg) { 25 | Toast.makeText(getApplicationContext(), msg.getData().getString("MSG", "Toast"), Toast.LENGTH_SHORT).show(); 26 | } 27 | }; 28 | 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.activity_main); 33 | serverThread = new ServerThread(); 34 | serverThread.start(); 35 | } 36 | 37 | @Override 38 | protected void onDestroy() { 39 | super.onDestroy(); 40 | serverThread.setIsLoop(false); 41 | } 42 | 43 | class ServerThread extends Thread { 44 | 45 | boolean isLoop = true; 46 | 47 | public void setIsLoop(boolean isLoop) { 48 | this.isLoop = isLoop; 49 | } 50 | 51 | @Override 52 | public void run() { 53 | Log.d(TAG, "running"); 54 | 55 | ServerSocket serverSocket = null; 56 | try { 57 | serverSocket = new ServerSocket(9000); 58 | 59 | while (isLoop) { 60 | Socket socket = serverSocket.accept(); 61 | 62 | Log.d(TAG, "accept"); 63 | 64 | DataInputStream inputStream = new DataInputStream(socket.getInputStream()); 65 | DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream()); 66 | 67 | String msg = inputStream.readUTF(); 68 | 69 | Message message = Message.obtain(); 70 | Bundle bundle = new Bundle(); 71 | bundle.putString("MSG", msg); 72 | message.setData(bundle); 73 | handler.sendMessage(message); 74 | 75 | socket.close(); 76 | } 77 | 78 | } catch (Exception e) { 79 | e.printStackTrace(); 80 | } finally { 81 | Log.d(TAG, "destory"); 82 | 83 | if (serverSocket != null) { 84 | try { 85 | serverSocket.close(); 86 | } catch (Exception e) { 87 | e.printStackTrace(); 88 | } 89 | } 90 | } 91 | } 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/Android-Pc-Socket-Connection/16a7096106dc67c82b4373e7d1516c8d10335c5d/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/Android-Pc-Socket-Connection/16a7096106dc67c82b4373e7d1516c8d10335c5d/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/Android-Pc-Socket-Connection/16a7096106dc67c82b4373e7d1516c8d10335c5d/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/Android-Pc-Socket-Connection/16a7096106dc67c82b4373e7d1516c8d10335c5d/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/Android-Pc-Socket-Connection/16a7096106dc67c82b4373e7d1516c8d10335c5d/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Android-Pc-Socket-Connection 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/cn/gavinliu/android_pc_socket_connection/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package cn.gavinliu.android_pc_socket_connection; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:1.5.0' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gavinliu/Android-Pc-Socket-Connection/16a7096106dc67c82b4373e7d1516c8d10335c5d/demo.gif -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':PC' 2 | --------------------------------------------------------------------------------