Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.

What is the SMB

SMB (Server Message Block) is a network file sharing protocol. It can be used for Web connection and information communication between clients and servers, allowing applications and end users to access file resources from remote file servers.

SMB communication protocol is a protocol developed by Microsoft (Microsoft) and Intel (Intel) in 1987, mainly as the communication protocol of Microsoft network. SMB is a protocol at the Session layer, presentation Layer and a small part of the Application Layer.

It makes it easy to transfer files to a computer, but SMB is not very secure: SMB protocol adopts the control file safe transmission mechanism is to use client authentication way, the way to send by the client to the server authentication codes for file transfer, but for the mechanism of network attack is relatively serious, attack programs through to verify password intercepted to steal the file access permissions, The security of file transfer on the LAN is not guaranteed.

Usage scenarios

This method is usually not used to achieve specific functional requirements. Some special scenarios are used, for example:

  1. Some mobile phone wallpaper software, there are horizontal screen wallpaper, but the horizontal screen wallpaper is generally used on the computer, if you use SMB technology can directly upload the picture to the computer folder, do not download in the local and then through wechat /QQ to the computer, a lot of convenience.

  2. Again such as we sometimes have a big APK package, through wechat to the computer will change the suffix, QQ may have large file restrictions, this time can also consider the use of SMB to solve, after all, write their own transmission code, forced lattice directly pull full 🤡.

  3. In addition, we want to directly reflect the data of the operation of the equipment on the computer to view, for example, a tablet end vision detection tool, after detection, directly reflect the data on the computer for the use of the staff.

How to use

1. Prerequisites

  • The phone and computer are connected to the same LAN
  • The computer requires a user name and password
  • Set the shared folder (SMB ://username:password@ip/folder). (Login authentication)
    • Mac Settings: System preferences – Share – File Share – Add a shared folder
    • Windows Settings: Folder – Share – Advanced Share – Permissions – Open Change permissions
  • The computer can’t screen

To set a shared folder:

Mac set Windows setup

2. Code configuration

See GitHub: BySMB for more details

1). Code introduction

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

dependencies {
    implementation 'com. Making. Youlookwhat: BySMB: 1.1.0'
}
Copy the code

2). Enable networking permission

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

3). Initialize in Application

BySMB.initProperty()
Copy the code

4). Get the SMB instance

val bySmb = BySMB.with()
        .setConfig(
                et_ip.text.toString(),       // ip
                et_username.text.toString(),/ / user name
                et_password.text.toString(),/ / password
                et_foldName.text.toString()// Name of the shared folder
        )
        .setReadTimeOut(60)
        .setSoTimeOut(180)
        .build()
Copy the code

Check the IP:

  • Query IP addresses on Mac:ifconfig | grep "inet"
  • View IP on Windows:ipconfig

3. Upload files to the computer

fun upload(bySmb: BySMB) {
    // Generate the File File
    val writeStringToFile = writeStringToFile(
            instance,
            et_content.text.toString(), // Text content
            et_fileName.text.toString()// File name, for example, note.txt
    )
    / / upload
    bySmb.writeToFile(writeStringToFile, object : OnOperationFileCallback {

        override fun onSuccess(a) {
            / / success
        }

        override fun onFailure(message: String) {
            / / fail}})}Copy the code

Note: If you upload a file with the same file name, the content of the previous file will be overwritten.

4. Look up a list of files on your computer

fun listFile(bySmb: BySMB){
    // Read all files in the root directory, override method ("", "*.txt", callback)
    bySmb.listShareFileName(object : OnReadFileListNameCallback {
        override fun onSuccess(fileNameList: List<String>) {
            // successfully reading the fileNameList fileNameList
        }

        override fun onFailure(message: String) {
             / / fail}})}Copy the code

5. Delete files from your computer

fun deleteFile(bySmb: BySMB){
    bySmb.deleteFile(et_fileName.text.toString(), object : OnOperationFileCallback {
        override fun onSuccess(a) {
	    // The deletion succeeded
        }

        override fun onFailure(message: String) {
            / / fail}})}Copy the code

conclusion

SMB is generally not used, but there are some users with requirements, before doing a lot of confusing places, and a lot of information is in foreign language, I hope this article is helpful to some people. Attached source code address, GitHub: BySMB.

The relevant data

  • hierynomus/smbj
  • IPad /iPhone communicates with Windows through SMB
  • SMB 2.0 and 3.0 protocol Java applications
  • No QQ wechat, simple steps to let the computer mobile phone quickly share files!