服务器端
权限
<uses-permission android:name=”android.permission.INTERNET”/>
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="355dp"
android:layout_height="398dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edittext1"
android:layout_width="185dp"
android:layout_height="49dp"
android:layout_marginBottom="116dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/button"
android:layout_width="67dp"
android:layout_height="44dp"
android:onClick="start_serv"
android:text="开启"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/edittext1"
app:layout_constraintTop_toBottomOf="@+id/edittext1"
app:layout_constraintVertical_bias="0.247" />
<Button
android:id="@+id/button2"
android:layout_width="67dp"
android:layout_height="44dp"
android:onClick="close_server"
android:text="关闭"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/edittext1"
app:layout_constraintTop_toBottomOf="@+id/edittext1"
app:layout_constraintVertical_bias="0.247" />
<EditText
android:id="@+id/edittext2"
android:layout_width="199dp"
android:layout_height="41dp"
android:layout_marginBottom="68dp"
app:layout_constraintBottom_toTopOf="@+id/edittext1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button3"
android:layout_width="83dp"
android:layout_height="44dp"
android:onClick="send"
android:text="发送"
app:layout_constraintBottom_toTopOf="@+id/edittext1"
app:layout_constraintEnd_toEndOf="@+id/edittext2"
app:layout_constraintStart_toStartOf="@+id/edittext2"
app:layout_constraintTop_toBottomOf="@+id/edittext2"
app:layout_constraintVertical_bias="0.099" />
<TextView
android:id="@+id/textView4"
android:layout_width="28dp"
android:layout_height="21dp"
android:text="port"
app:layout_constraintBottom_toBottomOf="@+id/edittext1"
app:layout_constraintEnd_toStartOf="@+id/edittext1"
app:layout_constraintTop_toTopOf="@+id/edittext1" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.server;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private ServerSocket server;
private Socket socket;
private DataInputStream in;
private String txt="";
private Handler handler;
private DataOutputStream out;
private static String[] PERMISSIONS_STORAGE = {
//依次权限申请
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.INTERNET
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
applypermission();
//权限申请
handler = new Handler(){
//handler用于处理更新组件属性
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if(msg.what==1){
//判断消息
final TextView textview = (TextView) findViewById(R.id.textView);
//获取文本组件 TextView
textview.append(txt);
}else if(msg.what==2){
final TextView textview = (TextView) findViewById(R.id.textView);
final EditText edittext = (EditText) findViewById(R.id.edittext2);
textview.append("服务器:"+edittext.getText()+"\n");
edittext.setText("");
}
}
};
}
public void start_serv(View view){
//启动服务器
server_start.start();
}
public void close_server(View view){
//关闭服务器
try {
server.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(View view){
//发送消息
send_message.start();
}
Thread send_message = new Thread(
//构建发送消息线程
new Runnable() {
@Override
public void run() {
try {
final EditText edittext = (EditText) findViewById(R.id.edittext2);
//获取组件
out.writeUTF("服务器:"+edittext.getText().toString());
handler.sendEmptyMessage(2);
} catch (Exception e) {
e.printStackTrace();
}
}
}
);
Thread server_start = new Thread(
//构造线程启动socket通信 获取客户端发送的内容
new Runnable() {
public void run() {
try {
final EditText edittext = (EditText) findViewById(R.id.edittext1);
//获取组件
server = new ServerSocket(Integer.valueOf(edittext.getText().toString()));
//设置端口
socket = server.accept();
//创建一个socket连接对象socket,等待服务器有客户端访问
//server.accept();调用服务器的server的接受方法
in = new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
// 创建DataOutputStream对象 发送数据
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true) {
try {
txt = in.readUTF()+"\n";
handler.sendEmptyMessage(1);
//发送空消息 1主要为了区分消息好执行改变组件信息的内容
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
//定义判断权限申请的函数,在onCreat中调用就行
public void applypermission(){
if(Build.VERSION.SDK_INT>=23){
boolean needapply=false;
for(int i=0;i<PERMISSIONS_STORAGE.length;i++){
int chechpermission= ContextCompat.checkSelfPermission(getApplicationContext(),
PERMISSIONS_STORAGE[i]);
if(chechpermission!= PackageManager.PERMISSION_GRANTED){
needapply=true;
}
}
if(needapply){
ActivityCompat.requestPermissions(MainActivity.this,PERMISSIONS_STORAGE,1);
}
}
}
}
客户端
权限
<uses-permission android:name=”android.permission.INTERNET”/>
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<EditText
android:id="@+id/edittext"
android:layout_width="186dp"
android:layout_height="40dp"
app:layout_constraintBottom_toTopOf="@+id/edittext2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.568"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3"
app:layout_constraintVertical_bias="1.0" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:onClick="connection"
android:text="连接"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.346"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edittext2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:onClick="stop"
android:text="断开"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.081"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/edittext2" />
<TextView
android:id="@+id/textView1"
android:layout_width="40dp"
android:layout_height="30dp"
android:text="ipv4"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/edittext"
app:layout_constraintEnd_toStartOf="@+id/edittext"
app:layout_constraintTop_toTopOf="@+id/edittext"
app:layout_constraintVertical_bias="1.0" />
<EditText
android:id="@+id/edittext2"
android:layout_width="186dp"
android:layout_height="40dp"
android:layout_marginTop="470dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.568"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.199" />
<TextView
android:id="@+id/textView2"
android:layout_width="38dp"
android:layout_height="31dp"
android:text="port"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/edittext2"
app:layout_constraintEnd_toStartOf="@+id/edittext2"
app:layout_constraintTop_toTopOf="@+id/edittext2"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView3"
android:layout_width="357dp"
android:layout_height="251dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.326"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.071" />
<EditText
android:id="@+id/edittext3"
android:layout_width="255dp"
android:layout_height="43dp"
android:layout_marginTop="56dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sentxt"
android:text="发送"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edittext3" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.customer;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private DataInputStream in;
private Socket socket;
private Handler handler;
private DataOutputStream out;
private String txt="";
private static String[] PERMISSIONS_STORAGE = {
//依次权限申请
Manifest.permission.INTERNET
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
applypermission();
//权限申请
handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if(msg.what==24){
final EditText edittext3 = (EditText) findViewById(R.id.edittext3);
final TextView textview3 = (TextView) findViewById(R.id.textView3);
//获取组件
textview3.append("客户端:"+edittext3.getText().toString()+"\n");
edittext3.setText("");
}else if(msg.what==98){
final TextView textview3 = (TextView) findViewById(R.id.textView3);
//获取组件
textview3.append(txt);
}
}
};
}
public void connection(View view){
connection.start();
}
public void stop(View view){
try {
socket.close();//关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
Thread connection = new Thread(
//连接服务器
new Runnable() {
@Override
public void run() {
try {
final EditText edittext = (EditText) findViewById(R.id.edittext);
final EditText edittext2 = (EditText) findViewById(R.id.edittext2);
//获取编辑框组件
socket = new Socket(edittext.getText().toString(), Integer.valueOf(edittext2.getText().toString()));
//连接服务器
out=new DataOutputStream(socket.getOutputStream());
// 创建DataOutputStream对象 发送数据
in = new DataInputStream(socket.getInputStream());
} catch (Exception e) {
// TODO Auto-generatetd catch block
e.printStackTrace();
}
while(true) {
try {
txt = in.readUTF()+"\n";
handler.sendEmptyMessage(98);
//发送空消息 1主要为了区分消息好执行改变组件信息的内容
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
);
public void sentxt(View view){
sendmessage.start();
handler.sendEmptyMessage(24);
//发送空消息 数字主要为了区分消息好执行改变组件信息的内容
}
Thread sendmessage = new Thread(
new Runnable() {
@Override
public void run() {
final EditText edittext3 = (EditText) findViewById(R.id.edittext3);
//获取组件
try {
out.writeUTF("客户端:"+edittext3.getText().toString());
//发送消息
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
//定义判断权限申请的函数,在onCreat中调用就行
public void applypermission(){
if(Build.VERSION.SDK_INT>=23){
boolean needapply=false;
for(int i=0;i<PERMISSIONS_STORAGE.length;i++){
int chechpermission= ContextCompat.checkSelfPermission(getApplicationContext(),
PERMISSIONS_STORAGE[i]);
if(chechpermission!= PackageManager.PERMISSION_GRANTED){
needapply=true;
}
}
if(needapply){
ActivityCompat.requestPermissions(MainActivity.this,PERMISSIONS_STORAGE,1);
}
}
}
}
只能一发一收两次,再发送就闪退了是怎么回事?
你看看报错是什么情况,我这边调试的时候基本没这种情况的
博主你好,最近在学安卓开发,APP连接自带WIFI的设备,可以用socket建立通信读取设备内的内存卡吗?