Android studio socket客户端应用设计

news/2024/5/17 19:46:03 标签: android studio, gitee, android

一、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文库


http://www.niftyadmin.cn/n/5291938.html

相关文章

《C#程序设计教程》总复习

一、单项选择题 1.short 类型的变量在内存中占据的位数是 ( )。 A. 8 B. 16 C. 32 D. 64 2.对千 int[ 4,5]型的数组 a, 数组元素 a[2,3] 存在数组第 ( )个位置上。 A. 11 B. 12 C. 14 D. 15 3.设 int 类型变量 x,y,z 的值分别是2、3、6 , 那么…

热迁移

一、两台机器均做nfs # 迁移机器 [rootlocalhost ~]# yum install nfs-utils [rootlocalhost ~]# systemctl start nfs-server [rootlocalhost ~]# systemctl enable nfs-server [rootlocalhost ~]# vim /etc/exports /opt/kvm/template/ 192.168.75.*(rw,sync,no_root_squ…

轻量应用服务器与云服务器CVM对比——腾讯云

腾讯云轻量服务器和云服务器CVM该怎么选&#xff1f;不差钱选云服务器CVM&#xff0c;追求性价比选择轻量应用服务器&#xff0c;轻量真优惠呀&#xff0c;活动 https://curl.qcloud.com/oRMoSucP 轻量应用服务器2核2G3M价格62元一年、2核2G4M价格118元一年&#xff0c;540元三…

如何使用mac电脑,1、使用快捷命令打开访达,2、使用终端命令创建文件,3、使用命令打开创建的文件,并且在vscode中打开

如何使用mac电脑 1、使用快捷命令打开访达 optioncommand空格键 快速进入访达 shiftcmmandn 创建一个空目录 2、使用终端命令创建文件 2.1进入文件夹 在终端页面输入“cd /Users/yunf/Desktop/”并按回车键&#xff08;此时进入到桌面文件夹&#xff0c;如果需要进入到其它…

【ES】Elasticsearch常见问题与解决(持续更新)

目录 Elasticsearch常见问题 1. 集群健康问题 2. 性能问题 3. 映射问题 4. 分片问题 5. 内存问题 6. 硬件问题 7. 配置问题 8. 安全问题 9. 网络问题 10. 版本不兼容 Elasticsearch日常使用小结 【Q】离线告警&#xff0c;有IP已离线 【Q】统计某个应用的某个索引…

gzip引入后node_modules中.cache compression-webpack-plugin占用内存过多

1.Gzip Gzip&#xff08;GNU zip&#xff09;是一种常见的文件压缩格式和压缩算法&#xff0c;通常用于在 Web 服务器上对静态资源文件进行压缩&#xff0c;以减小文件大小并加快文件传输速度。在前端开发中&#xff0c;经常会使用 Gzip 压缩来优化网站的性能。 Gzip 压缩通过…

C++药房管理系统设计模块代码分析

药房管理系统涉及到药品管理、库存管理、销售管理等多个模块。其中类设计如下&#xff08;使用C语言&#xff09;&#xff1a; 1. 药品管理模块——药品类&#xff08;Drug&#xff09;的定义&#xff1a; cpp class Drug { private: string name; int quantity; …

C语言实现RSA算法加解密

使用c语言实现了RSA加解密算法&#xff0c;可以加解密文件和字符串。 rsa算法原理 选择两个大素数p和q&#xff1b;计算n p * q;计算φ(n)(p-1)(q-1)&#xff1b;选择与φ(n)互素的整数d&#xff1b;由de1 mod φ(n)计算得到e&#xff1b;公钥是(e, n), 私钥是(d, n);假设明…