├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.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 │ │ │ └── layout │ │ │ │ ├── client_item_list.xml │ │ │ │ ├── activity_main.xml │ │ │ │ ├── item_list.xml │ │ │ │ ├── act_new_client.xml │ │ │ │ └── activity_server.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── accvmedia │ │ │ │ └── mysocket │ │ │ │ ├── bean │ │ │ │ ├── SocketBean.java │ │ │ │ ├── BroadcastBean.java │ │ │ │ └── TaskBean.java │ │ │ │ ├── receiver │ │ │ │ └── WifiChangedReceiver.java │ │ │ │ ├── Constant.java │ │ │ │ ├── socket │ │ │ │ ├── MulticastThread.java │ │ │ │ ├── ClientThread.java │ │ │ │ ├── ServerRunnable.java │ │ │ │ └── SocketManager.java │ │ │ │ ├── MainActivity.java │ │ │ │ ├── util │ │ │ │ ├── DebugLogger.java │ │ │ │ ├── WifiUtils.java │ │ │ │ └── FileUtils.java │ │ │ │ ├── ServerAdapter.java │ │ │ │ ├── jobqueue │ │ │ │ ├── SocketJobManager.java │ │ │ │ └── SocketJob.java │ │ │ │ ├── ClientActivity.java │ │ │ │ └── ServerActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── accvmedia │ │ │ └── mysocket │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── accvmedia │ │ └── mysocket │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── art └── socket.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── README.md ├── local.properties ├── .gitignore ├── gradle.properties ├── MySocket.iml ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /art/socket.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DempseyZheng/android-socket-file-pass/HEAD/art/socket.gif -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 分布式发布demo 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DempseyZheng/android-socket-file-pass/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DempseyZheng/android-socket-file-pass/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DempseyZheng/android-socket-file-pass/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DempseyZheng/android-socket-file-pass/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DempseyZheng/android-socket-file-pass/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DempseyZheng/android-socket-file-pass/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/test/java/com/accvmedia/mysocket/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.accvmedia.mysocket; 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() 13 | throws Exception 14 | { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/com/accvmedia/mysocket/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.accvmedia.mysocket; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest 10 | extends ApplicationTestCase 11 | { 12 | public ApplicationTest() { 13 | super(Application.class); 14 | } 15 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # android_socket 2 | ### 文件传输流程: 3 | 1. 通过局域网广播的方式通知其他客户端服务器ip地址和要发送的文件列表 4 | 2. 客户端收到广播后,主动连接服务器传入需要的文件名 5 | 3. 通信协议为子节流的前4个字节表示数据长度,后面紧跟要发的内容,发送时都是先发内容长度再发具体内容,接收端也是一样 6 | ### 项目特性: 7 | 1. 支持多个文件传输,用了任务调度框架[priority-jobqueue](https://github.com/yigit/android-priority-jobqueue "priority-jobqueue") 8 | 2. 对socket进行了一些封装,提供一系列回调,连接成功,文件进度,出现异常等 9 | 3. 传输过程中socket超时,支持自动重连 10 | 4. 局域网传输文件,支持断点续传 11 | 5. 交互协议通过json格式进行通信,通用型更强 12 | 6. 支持一对多传输 13 | 14 | #### *因为模拟器不能发局域网广播,只能手动输入ip和需要的文件名* 15 | ![df](art/socket.gif) -------------------------------------------------------------------------------- /local.properties: -------------------------------------------------------------------------------- 1 | ## This file is automatically generated by Android Studio. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | # 7 | # Location of the SDK. This is only used by Gradle. 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | #Tue Mar 14 17:53:49 CST 2017 11 | ndk.dir=D\:\\sdk\\ndk-bundle 12 | sdk.dir=D\:\\sdk 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/accvmedia/mysocket/bean/SocketBean.java: -------------------------------------------------------------------------------- 1 | package com.accvmedia.mysocket.bean; 2 | 3 | /** 4 | * Created by dempseyZheng on 2017/3/29 5 | */ 6 | public class SocketBean { 7 | 8 | /** 9 | * wifiMac : mac 10 | * offset : 0 11 | * file : txt 12 | */ 13 | 14 | public String wifiMac; 15 | public int offset; 16 | public String fileName; 17 | 18 | public SocketBean(String wifiMac, int offset, String fileName) { 19 | this.wifiMac = wifiMac; 20 | this.offset = offset; 21 | this.fileName = fileName; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | -------------------------------------------------------------------------------- /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 D:\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/main/res/layout/client_item_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 13 | 20 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /app/src/main/java/com/accvmedia/mysocket/bean/BroadcastBean.java: -------------------------------------------------------------------------------- 1 | package com.accvmedia.mysocket.bean; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by dempseyZheng on 2017/3/29 7 | */ 8 | public class BroadcastBean { 9 | 10 | /** 11 | * hostIp : ip 12 | * files : [{"name":"txt","length":"kb"},{"name":"zip","length":"kb"}] 13 | */ 14 | 15 | public String hostIp; 16 | public int type; 17 | /** 18 | * name : txt 19 | * length : kb 20 | */ 21 | 22 | public List files; 23 | 24 | public static final int TYPE_FILE=10001; 25 | public static final int TYPE_CLEAR=10002; 26 | public static class FilesBean { 27 | public String name; 28 | public long length; 29 | 30 | public FilesBean(String name, long length) { 31 | this.name = name; 32 | this.length = length; 33 | } 34 | 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.accvmedia.mysocket" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.4.4" 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.0.1' 26 | compile 'com.android.support:recyclerview-v7:23.0.1' 27 | compile 'com.nononsenseapps:filepicker:2.5.2' 28 | compile 'com.google.code.gson:gson:2.2.4' 29 | compile 'com.birbit:android-priority-jobqueue:2.0.1' 30 | } 31 | -------------------------------------------------------------------------------- /MySocket.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/accvmedia/mysocket/receiver/WifiChangedReceiver.java: -------------------------------------------------------------------------------- 1 | package com.accvmedia.mysocket.receiver; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.net.NetworkInfo; 7 | import android.net.wifi.WifiManager; 8 | import android.os.Parcelable; 9 | import android.util.Log; 10 | 11 | /** 12 | * Created by Administrator on 2016/11/24 0024. 13 | */ 14 | 15 | public class WifiChangedReceiver extends BroadcastReceiver { 16 | private static final boolean DEBUG = true; 17 | 18 | @Override 19 | public void onReceive(Context context, Intent intent) { 20 | 21 | if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) { 22 | Parcelable parcelableExtra = intent 23 | .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); 24 | if (null != parcelableExtra) { 25 | NetworkInfo networkInfo = (NetworkInfo) parcelableExtra; 26 | NetworkInfo.State state = networkInfo.getState(); 27 | switch (state) { 28 | case CONNECTED : 29 | // SocketManager.getInstance().newClient(Constant.HOST_IP, 30 | // Constant.PORT); 31 | break; 32 | 33 | case DISCONNECTED : 34 | 35 | break; 36 | } 37 | } 38 | } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/accvmedia/mysocket/Constant.java: -------------------------------------------------------------------------------- 1 | package com.accvmedia.mysocket; 2 | 3 | import android.os.Environment; 4 | 5 | /** 6 | * Created by dempseyZheng on 2017/3/17 7 | */ 8 | public class Constant { 9 | public static final String RECEIVE_FILE_DIR = Environment.getExternalStorageDirectory() 10 | .getAbsolutePath()+"/distribute"; 11 | public static final long RECEIVE_FILE_LENGTH = 0; 12 | public static final String SEND_DIR = Environment.getExternalStorageDirectory() 13 | .getAbsolutePath() + "/send"; 14 | public static final String GROUP_ID = "SOCKET_GROUP"; 15 | public static final int PRIORITY = 999; 16 | public static String RECEIVE_FILE_PATH =""; 17 | public static String HOST_IP ="192.168.1.173"; 18 | public static int PORT =10086; 19 | public static String multicastHost ="224.0.0.1"; 20 | public static int multicastPort =10010; 21 | public static boolean isReceiving =false; 22 | public static boolean isServer =false; 23 | public static int SoTimeout=5*1000;//socket连接超时时间5秒 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 18 | 23 |