安卓Socket客户端开发注意事项:
网络访问不能在主线程(UI线程)中进行,否则程序会崩溃
要创建线程来发起网络访问
服务端
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class test {
public static void main(String[] args) {
try {
ServerSocket socket=new ServerSocket(8888);//创建套接字
System.out.println("套接字创建成功,等待连接!");
while(true){
new Thread(new Runnable() {
@Override
public void run() {
int len=0;
byte[] a=new byte[128];
Socket con= null;
try {
con = socket.accept();//获取客户端连接
System.out.println("连接成功,等待接收");
InputStream inputStream = con.getInputStream();//获取输入流,用来读取数据
len=inputStream.read(a);
System.out.println("收到数据:"+new String(a,0,len));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
try {
int len=0;
byte[] a=new byte[128];
Socket client=new Socket("192.168.31.111",8800);
Scanner sr=new Scanner(System.in);
String str=sr.nextLine();
OutputStream out =client.getOutputStream();//获取发送通道
out.write(str.getBytes());//发送数据
InputStream inputStream = client.getInputStream();//获取输入流,用来读取数据
len=inputStream.read(a);
System.out.println("收到数据:"+new String(a,0,len));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
安卓代码
package com.example.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View v) {
switch (v.getId()) {
case R.id.fowear:
M1.m1("qianjin");
break;
case R.id.back:
M1.m1("houtui");
break;
case R.id.turnlefe:
M1.m1("xiangzhuo");
break;
case R.id.turnright:
M1.m1("xiangyou");
break;
}
}
}
M1代码
package com.example.myapplication;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class M1 {
public static void m1(String Data){
new Thread(new Runnable() {
@Override
public void run() {
try {
Socket client = new Socket("192.168.31.151",8810);
OutputStream out =client.getOutputStream();//获取发送通道
out.write(Data.getBytes());//发送数据
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
<?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">
<Button
android:id="@+id/fowear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="176dp"
android:onClick="send"
android:text="前进"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/turnlefe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginTop="53dp"
android:text="向左"
android:onClick="send"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fowear" />
<Button
android:id="@+id/turnright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="30dp"
android:layout_marginBottom="28dp"
android:onClick="send"
android:text="向右"
app:layout_constraintBottom_toTopOf="@+id/back"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="37dp"
android:layout_marginTop="34dp"
android:text="后退"
android:onClick="send"
app:layout_constraintStart_toEndOf="@+id/turnlefe"
app:layout_constraintTop_toBottomOf="@+id/turnlefe" />
</androidx.constraintlayout.widget.ConstraintLayout>
注:使用网络都需加入权限