1. 程式人生 > >1 TCP/IP通信

1 TCP/IP通信

you brush true .text light [] pack tac private

1 電腦(或ESP8266)連接WIFI,建立服務 192.168.3.8 8080

1.1 查看電腦地址

技術分享圖片

1.2 電腦建立服務 192.168.3.8 8080

技術分享圖片

2 手機連接WIFI,創建客戶申請服務

手機在wifi 下分配的地址是 192.168.3.9

技術分享圖片

2.1 手機打開軟件

技術分享圖片

點擊發送

電腦接收

技術分享圖片

手機端工程

1 布局管理

技術分享圖片

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dongdong.myapplication.MainActivity">



      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:context="com.itman.connectesp8266.MainActivity"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#fe9920"
        android:gravity="center"
        android:text="接收的內容" />

    <Button
        android:id="@+id/bt_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="發送" />

    <TextView
        android:id="@+id/tv_send_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/bt_send"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:text="發送的內容" />

</RelativeLayout>
</android.support.constraint.ConstraintLayout>

  2 添加網絡權限

技術分享圖片

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

  3 工程目錄

技術分享圖片

技術分享圖片

package com.example.dongdong.myapplication;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements OnClickListener {

  //  private EditText edit_pawd;
    private Button bt_send;
    private TextView tv_content;
    private TextView tv_send_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         // 1 控件初始化
        InitView();
        //2 開啟服務器
        MobileServer mobileServer = new MobileServer();
        mobileServer.setHandler(handler);
        new Thread(mobileServer).start();

    }

    private void InitView() {
        tv_content = (TextView) findViewById(R.id.tv_content);
        tv_send_text = (TextView) findViewById(R.id.tv_send_text);
        bt_send = (Button) findViewById(R.id.bt_send);

        bt_send.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_send:
                String str = "Sent to the ESP8266";
                new SendAsyncTask().execute(str);
                tv_send_text.setText(str);
                break;
        }

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    tv_content.setText("WiFi模塊發送的:" + msg.obj);
                    Toast.makeText(MainActivity.this, "接收到信息", Toast.LENGTH_LONG)
                            .show();
            }
        }
    };
}

  

技術分享圖片

package com.example.dongdong.myapplication;

import java.io.IOException;

import java.net.ServerSocket;
import java.net.Socket;

import java.io.DataInputStream;

import android.os.Handler;
import android.os.Message;

public class MobileServer implements Runnable {
    private ServerSocket server;
    private DataInputStream in;
    private byte[] receice;

    private Handler handler = new Handler();

    public MobileServer() {
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {

        try {
            //5000是手機端開啟的服務器的端口號,ESP8266進行TCP連接時使用的端口,而IP也是通過指令查詢的聯入設備的IP
            server = new ServerSocket(5000);
            while (true) {
                Socket client = server.accept();
                in = new DataInputStream(client.getInputStream());
                receice = new byte[50];
                in.read(receice);
                in.close();

                Message message = new Message();
                message.what = 1;
                message.obj = new String(receice);
                handler.sendMessage(message);
            }

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

  

技術分享圖片

package com.example.dongdong.myapplication;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import android.os.AsyncTask;



public class SendAsyncTask extends AsyncTask<String, Void, Void> {

    //這裏是連接ESP8266的IP和端口號,IP是通過指令在單片機開發板查詢到,而端口號可以自行設置,也可以使用默認的,333就是默認的
    private static final String IP = "192.168.3.8";
    private static final int PORT =8080;



    private Socket client = null;
    private PrintStream out = null;


    @Override
    protected Void doInBackground(String... params) {
        String str = params[0];
        try {
            client = new Socket(IP, PORT);
            client.setSoTimeout(5000);
            // 獲取Socket的輸出流,用來發送數據到服務端
            out = new PrintStream(client.getOutputStream());
            out.print(str);
            out.flush();

            if (client == null) {
                return null;
            } else {
                out.close();
                client.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}

  

1 TCP/IP通信