[Android] socket client sends a string

In order to simplify as much as possible, the PC uses the debugging tool as the server, and the data received from the mobile terminal is as follows

To avoid common sense errors, I usually write permissions first, and add the following permissions to Android manifest.xml

<?xml version="1.0" encoding="utf-8"?>

    <!--Allow applications to change network state-->
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

    <!--Allow application changes WIFI Connection status-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <!--Allow applications to access relevant network information-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <!--Allow application access WIFI Network information of network card-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <!--Allow applications to fully use the network-->
    <uses-permission android:name="android.permission.INTERNET"/>

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

                    android:name="android.intent.action.MAIN" />

                    android:name="android.intent.category.LAUNCHER" />


MainActivity.java. The relevant explanations should be in the comments. The main steps are
1. Get socket (connection), 2. Get stream, 3. Organize the string to be sent, 4. Send
The most troublesome step is the third step. If there is a need for business logic in the later stage, most of the work is spent on processing strings. Here, simply send a string

package com.bm.printer0105;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    private Socket socket;
    private OutputStream outputStream;

    private String download;
    private byte[] download_head;

    protected void onCreate(Bundle savedInstanceState) {

        //Start a thread to perform time-consuming operations. This is a method written by yourself, which is easy to identify


    private void initThread(){

        new Thread(new Runnable() {
            public void run() {

                //The thread performs four methods: getting socket (connected), getting output stream, making string and sending
                initSocket("",9100);//Write the IP and port of the server here. You can see the IP on the PC. if you can't see the IP with other tools, you can use cmd and enter ipconfig


    private void initSocket(final String host, final int port){

        //This method is used to obtain socket and perform connection operation. As long as the server is opened, it can be connected generally. Note that ip and port are correct
        // (also, in some places, the network environment is complex. If the transmission is not successful, you can also try to use mobile hotspot as AP, remember to change IP)

        try {
            socket=new Socket(host,port);//This step has already been connected. The difference with C is that there is no need to establish a family or specify the connection type selection. It is very convenient to be in place in one step
        } catch (IOException e) {
            Log.e("Obtain socket",e.toString());


    private void initStream(){
        try {
            outputStream=socket.getOutputStream();//This step also needs to be obtained in the sub process. I've tried to report errors when executing in the main process. It's not good to open a single process. It must be in the same process as the above method. In fact, it can be a little more streamlined and put these in a try, so that a lot of code can be saved, but it's not easy to find errors
        } catch (IOException e) {
            Log.e("Get stream",e.toString());


    private void initString(){

        download = "TEST BM IT IoT";
        download_head = download.getBytes();//To be converted to byte array, that is, byte [], sending is allowed

    private void initSend(){
        try {
            outputStream.write(download_head);//The sending method is to call the write of the output stream, and the data will be sent to the server
        } catch (IOException e) {
            Log.e("Send data to the server (output stream)",e.toString());



Source code download:
In fact, you can copy the code directly

Tags: Android socket network Java

Posted on Tue, 05 May 2020 03:46:42 -0400 by nikefido