esp32
#include <WiFi.h>
#define LED_BUILTIN 2
const char *ssid = "ESP32";
const char *password = "12345678";
const int port = 1122;
WiFiServer server(port);
void setup() {
delay(5000);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.print(myIP);
Serial.print(":");
Serial.println(port);
server.begin();
Serial.println("Server started!");
}
void loop() {
WiFiClient client = server.available(); // 监听新客户端
if (client) {
digitalWrite(LED_BUILTIN, HIGH); // 如果有客户端接入
Serial.println("New Client."); // 打印至串口
String currentLine = ""; // 用来保存客户端发来的数据
while (client.connected()) { // 当有持续连接时循环
if (client.available()) { // 如果有信息待接收
char c = client.read(); // read a byte, then 判断字符是不是\n 从而判断有没有结束
// Serial.write(c); // 串口打印
if(c != '\n'){
currentLine += c;
}else{
Serial.println(currentLine);
client.print(currentLine);
currentLine = "";
}
}
}
// close the connection:
client.stop();
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Client Disconnected.");
}
}
android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/server_ip_kj"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="192.168.4.1" />
<TextView
android:id="@+id/server_port_kj"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="1122" />
<Button
android:id="@+id/connect_btn_kj"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="连接服务器" />
<EditText
android:id="@+id/send_data_kj"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ems="10"
android:inputType="text"
android:text="Name" />
<Button
android:id="@+id/send_btn_kj"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="发送" />
</LinearLayout>
package com.example.esp32_tcp_client;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView server_ip_kj,server_port_kj;
private Button connect_btn_kj,send_btn_kj;
private EditText send_data_kj;
private Socket socket;
InputStream inputStream;
OutputStream outputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
server_ip_kj = findViewById(R.id.server_ip_kj);
server_port_kj = findViewById(R.id.server_port_kj);
connect_btn_kj = findViewById(R.id.connect_btn_kj);
send_btn_kj = findViewById(R.id.send_btn_kj);
send_data_kj = findViewById(R.id.send_data_kj);
// 连接服务器
connect_btn_kj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket(server_ip_kj.getText().toString(),Integer.valueOf(server_port_kj.getText().toString()));
if(socket.isConnected()){
//连接成功
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();
Log.i("Connect","OK");
RecvData();
}else{
//连接失败
Log.i("Connect","Fail");
}
} catch (IOException e) {
//连接失败
Log.i("Connect","Fail");
throw new RuntimeException(e);
}
}
}).start();
}
});
// 发送信息
send_btn_kj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//向byte数组msg的最后添加'\n'
byte[] msg = send_data_kj.getText().toString().getBytes();
byte[] tmp = new byte[msg.length +1];
for(int i=0;i<msg.length;i++){
tmp[i] = msg[i];
}
tmp[msg.length] = '\n';
outputStream.write(tmp);
Log.i("sendmsg","OK");
} catch (IOException e) {
Log.i("sendmsg","Fail");
throw new RuntimeException(e);
}
}
}).start();
}
});
}
// 接收消息
private void RecvData(){
new Thread(new Runnable() {
@Override
public void run() {
while(socket != null && socket.isConnected() == true){
byte[] recv_buff = new byte[255];
try {
int len = inputStream.read(recv_buff);
if(len != -1){
Log.i("RecvDataLength",String.valueOf(len)); // len是接收到的字符个数
Log.i("RecvData",new String(recv_buff));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
}
<uses-permission android:name="android.permission.INTERNET" />