一、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/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginTop="32dp"
android:gravity="center"
android:text="IP地址:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="端口号:"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="发送数据:"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/editTextTextPostalAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ems="10"
android:gravity="center"
android:inputType="textPostalAddress"
android:text="192.168.8.140"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/editTextTextPostalAddress2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:ems="10"
android:gravity="center"
android:inputType="textPostalAddress"
android:text="5050"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintStart_toStartOf="@+id/editTextTextPostalAddress"
app:layout_constraintTop_toTopOf="@+id/textView" />
<EditText
android:id="@+id/editTextTextPostalAddress3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:ems="10"
android:gravity="center"
android:inputType="textPostalAddress"
android:text="Hello!"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintStart_toStartOf="@+id/editTextTextPostalAddress2"
app:layout_constraintTop_toTopOf="@+id/textView2" />
<Button
android:id="@+id/button"
android:layout_width="161dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:onClick="link"
android:text="连接服务器"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />
<Button
android:id="@+id/button2"
android:layout_width="147dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:onClick="transmitter"
android:text="发送数据"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="接收数据:"
android:gravity="center"
app:layout_constraintEnd_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:gravity="center"
app:layout_constraintBottom_toBottomOf="@+id/textView4"
app:layout_constraintStart_toStartOf="@+id/editTextTextPostalAddress3"
app:layout_constraintTop_toTopOf="@+id/textView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
二、增加wifi权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>
三、MainActivity.java部分代码:
1)、连接服务器按钮onClick事件: public void link(View view) { if (btn1.getText().equals("连接服务器")) { ConnectThread connectthread = new ConnectThread(); connectthread.start(); btn1.setText("断开服务器"); } else if (btn1.getText().equals("断开服务器")) { btn1.setText("连接服务器"); try { socket.close(); socket = null; } catch (Exception e) { e.printStackTrace(); } } }
2)、socket连接thread:
class ConnectThread extends Thread {
public void run() {
InetAddress ipAddress;
try {
if (socket == null) {
ipAddress = InetAddress.getByName(ipEt.getText().toString());
int port = Integer.valueOf(portEt.getText().toString());
socket = new Socket(ipAddress, port);
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
ReadDataThread readDataThread = new ReadDataThread();
readDataThread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3)、发送数据thread:
class SendDataThread extends Thread { public void run() { try { outputStream.write(txDataEt.getText().toString().getBytes()); btn2.setText("发送完成"); Thread.sleep(1000); btn2.setText("发送数据"); } catch (Exception e) { e.printStackTrace(); } } }
4)、接收数据thread:
四、运行效果:
五、项目工程:
Androidstudiosocket客户端应用设计资源-CSDN文库