Android studio socket客户端应用设计
一、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"><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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" /><EditTextandroid: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" /><EditTextandroid: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" /><EditTextandroid: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" /><Buttonandroid: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" /><Buttonandroid: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" /><TextViewandroid: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" /><EditTextandroid: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文库
相关文章:
Android studio socket客户端应用设计
一、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…...
【UTC格式】Oracle低版本实现UTC格式转换
Oracle低版本实现UTC格式转换 文章目录 Oracle低版本实现UTC格式转换前言一、创建 FUNCTION(GET_UTC_DATE)二、使用步骤1.跨年日期2.跨月日期(判断 闰年 or 平年、用于计算2月天数,正常大小月)3.跨日日期3.正常日期 总…...
[BUG] Hadoop-3.3.4集群yarn管理页面子队列不显示任务
1.问题描述 使用yarn调度任务时,在CapacityScheduler页面上单击叶队列(或子队列)时,不会显示应用程序任务信息,root队列可以显示任务。此外,FairScheduler页面是正常的。 No matching records found2.原…...
Java 缓存中间件
Java 缓存中间件 关键词:Spring Cache、J2Cache、JetCache 一 、JSR 107 JSR107 中制订了 Java 缓存的规范。 因此,在很多缓存框架、缓存库中,其 API 都参考了 JSR 107 规范。 img Java Caching 定义了 5 个核心接口 CachingProvider - 定义…...
使用cmake配置matplotlibcpp生成VS项目
https://gitee.com/feboreigns/matplotlibcpp 这篇文章需要一些cmake基础,python基础,visualstudio基础 准备环境 注意如果在VS平台使用必须要手动下载python,不能使用conda里面的,比如3.8版本,因为conda里面没有py…...
Android : 画布绘制矩形和文字 让其居中显示简单应用
示例图: CenterView.java package com.example.demo;import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.View;public class Center…...
科研学习|论文解读——融合类目偏好和数据场聚类的协同过滤推荐算法研究
论文链接(中国知网): 融合类目偏好和数据场聚类的协同过滤推荐算法研究 - 中国知网 (cnki.net) 摘要:[目的/意义]基于近邻用户的协同过滤推荐作为推荐系统应用最广泛的算法之一,受数据稀疏和计算可扩展问题影响&#x…...
算法学习系列(十五):最小堆、堆排序
目录 引言一、最小堆概念二、堆排序模板(最小堆)三、模拟堆 引言 这个堆排序的话,考的还挺多的,主要是构建最小堆,并且在很多情况下某些东西还用得着它来优化,比如说迪杰斯特拉算法可以用最小堆优化&#…...
HCIA-Datacom题库(自己整理分类的)——OSPF协议多选
ospf的hello报文功能是 邻居发现 同步路由器的LSDB 更新LSA信息 维持邻居关系 下列关于OSPF区域描述正确的是 在配置OSPF区域正确必须给路由器的loopback接配置IP地址 所有的网络都应在区域0中宣告 骨干区域的编号不能为2 区域的编号范围是从0.0.0.0到255.255.255.255…...
elasticsearch-hadoop.jar 6.8版本编译异常
## 背景 重新编译 elasticsearch-hadoop 包; GitHub - elastic/elasticsearch-hadoop at 6.8 编译 7.17 版本时很正常,注意设置下环境变量就好,JAVA8_HOME/.... 编译 6.8 版本时(要求jdk8 / jdk9),出现…...
面试经典150题(50-53)
leetcode 150道题 计划花两个月时候刷完,今天(第二十二天)完成了4道(50-53)150: 50.(141. 环形链表)题目描述: 给你一个链表的头节点 head ,判断链表中是否有环。 如果链表中有某个…...
Jetpack Compose中使用Android View
使用AndroidView创建日历 Composable fun AndroidViewPage() {AndroidView(factory {CalendarView(it)},modifier Modifier.fillMaxWidth(),update {it.setOnDateChangeListener { view, year, month, day ->Toast.makeText(view.context, "${year}年${month 1}月$…...
《MySQL》事务篇
事务特性 ACID Atomicity原子性:事务中的操作要么全部完成,要么全部失败。 Consistency一致性:事务操作前后,数据满足完整性约束。 Isolation隔离性:允许并发执行事务,每个事务都有自己的数据空间&…...
高阶组件和高阶函数是什么
高阶组件和高阶函数都是在函数式编程中常见的概念。 高阶组件(Higher-Order Component, HOC)是一种函数,接受一个组件作为参数,并返回一个新的组件。它可以用来增强现有的组件,给它添加额外的功能或属性。高阶组件在R…...
初步认识API安全
一、认识API 1. 什么是API API(应用程序接口):是一种软件中介,它允许两个不相关的应用程序相互通信。它就像一座桥梁,从一个程序接收请求或消息,然后将其传递给另一个程序,翻译消息并根据 API 的程序设计执行协议。A…...
MybatisX逆向工程方法
官方文档链接:MybatisX快速开发插件 | MyBatis-Plus (baomidou.com) 使用MybatisX可以快速生成mapper文件,实体类和service及实现 效果 方法:首先下载mybatisX插件 然后创建数据库信息 然后选中表,右键,点击Mybatis…...
每日一题:LeetCode-LCR 179. 查找总价格为目标值的两个商品
每日一题系列(day 16) 前言: 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🔎…...
SpringBoot 3.2.0 基于Logback定制日志框架
依赖版本 JDK 17 Spring Boot 3.2.0 工程源码:Gitee 日志门面和日志实现 日志门面(如Slf4j)就是一个标准,同JDBC一样来制定“规则”,把不同的日志系统的实现进行了具体的抽象化,只提供了统一的日志使用接…...
微软发布安卓版Copilot,可免费使用GPT-4、DALL-E 3
12月27日,微软的Copilot助手,可在谷歌应用商店下载。目前,只有安卓版,ios还无法使用。 Copilot是一款类ChatGPT助手支持中文,可生成文本/代码/图片、分析图片、总结内容等,二者的功能几乎没太大差别。 值…...
【STM32】程序在SRAM中运行
程序在RAM中运行 1、配置内存分配。 2、修改跳转文件 FUNC void Setup(void) { SP _RDWORD(0x20000000); PC _RDWORD(0x20000004); } LOAD RAM\Obj\Project.axf INCREMENTAL Setup(); 3、修改下载ROM地址和RAM地址; 中断向量表映射 中断向量表映射到SRA…...
深度解析开源Galgame社区:从零构建纯净视觉小说交流平台
深度解析开源Galgame社区:从零构建纯净视觉小说交流平台 【免费下载链接】kun-touchgal-next TouchGAL是立足于分享快乐的一站式Galgame文化社区, 为Gal爱好者提供一片净土! 项目地址: https://gitcode.com/gh_mirrors/ku/kun-touchgal-next TouchGAL是一个基…...
FPGA实战:手把手教你用Verilog给NAND Flash数据上把“安全锁”(附完整ECC代码)
FPGA实战:用Verilog为NAND Flash打造硬件级ECC防护系统 1. 为什么你的NAND Flash需要硬件ECC? NAND Flash存储芯片在工业控制、物联网终端和边缘计算设备中扮演着关键角色,但它的物理特性导致数据可靠性存在先天缺陷。想象一下,当…...
Linux 内核遍历宏介绍
Linux内核中的遍历宏全面详解 Linux内核中大量使用遍历宏(Iteration Macros)来简化数据结构的遍历操作。这些宏提供了类型安全、简洁且高效的遍历方式,是内核编程的核心范式之一。一、遍历宏的分类 1.1 按功能分类 Linux内核遍历宏 ├── 链…...
Apache Flink Agents 0.2.1版本发布,亮点几何?
Apache Flink社区宣布发布 Apache Flink Agents 0.2 系列的首个缺陷修复版本 0.2.1,包含3项缺陷和漏洞修复及小幅改进,还基于此构建了演示项目。版本发布情况Apache Flink社区很高兴地推出了 Apache Flink Agents 0.2.1 版本。此版本是 0.2 系列的首个缺…...
保姆级教程:NotaGen一键部署,小白也能生成贝多芬风格交响乐
保姆级教程:NotaGen一键部署,小白也能生成贝多芬风格交响乐 1. 引言:AI音乐创作新体验 你是否曾经梦想过创作一首属于自己的交响乐?现在,NotaGen让这个梦想变得触手可及。这个基于大语言模型(LLM)的音乐生成工具&…...
MongoDB高级面试:进阶面试题50题及答案详解
更多内容请见: 《深入掌握MongoDB数据库》 - 专栏介绍和目录 文章目录 一、高级查询优化与执行计划 (8题) 二、高级索引策略 (8题) 三、高级分片策略与优化 (8题) 四、性能调优与瓶颈分析 (7题) 五、高级复制集配置与故障处理 (6题) 六、高级事务与一致性模型 (5题) 七、安全高…...
树莓派Pico硬件重置失效?试试这个C语言强制重置方案(附完整代码)
树莓派Pico硬件重置失效?试试这个C语言强制重置方案(附完整代码) 当你在开发树莓派Pico项目时,可能会遇到这样的情况:硬件重置按钮突然失效,外围设备(比如LED)无法正常复位。传统的B…...
影刀+即刻:碎片化信息自动归类的联动玩法
影刀与即刻联动实现信息自动归类影刀RPA作为自动化工具,与即刻APP的推送功能结合,可高效管理碎片化信息。以下为具体实现方法:创建即刻机器人 在即刻APP中创建自定义机器人,设置关键词触发规则。例如设置"#工作""#…...
番茄小说下载器:Rust构建的高性能离线阅读解决方案
番茄小说下载器:Rust构建的高性能离线阅读解决方案 【免费下载链接】Tomato-Novel-Downloader 番茄小说下载器不精简版 项目地址: https://gitcode.com/gh_mirrors/to/Tomato-Novel-Downloader 在数字阅读时代,网络依赖、格式不兼容和下载效率低下…...
WSL 启动闪退问题排查
第一步:检查当前状态在开始折腾 BIOS 之前,我们先确认一下系统到底有没有识别到虚拟化。按下快捷键 Ctrl Shift Esc 打开任务管理器。点击左侧的“性能”图标,选择 “CPU”。看右下角的信息,找到 “虚拟化”:如果是“…...
