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…...
反向工程与模型迁移:打造未来商品详情API的可持续创新体系
在电商行业蓬勃发展的当下,商品详情API作为连接电商平台与开发者、商家及用户的关键纽带,其重要性日益凸显。传统商品详情API主要聚焦于商品基本信息(如名称、价格、库存等)的获取与展示,已难以满足市场对个性化、智能…...
智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql
智慧工地管理云平台系统,智慧工地全套源码,java版智慧工地源码,支持PC端、大屏端、移动端。 智慧工地聚焦建筑行业的市场需求,提供“平台网络终端”的整体解决方案,提供劳务管理、视频管理、智能监测、绿色施工、安全管…...
在HarmonyOS ArkTS ArkUI-X 5.0及以上版本中,手势开发全攻略:
在 HarmonyOS 应用开发中,手势交互是连接用户与设备的核心纽带。ArkTS 框架提供了丰富的手势处理能力,既支持点击、长按、拖拽等基础单一手势的精细控制,也能通过多种绑定策略解决父子组件的手势竞争问题。本文将结合官方开发文档,…...
Leetcode 3577. Count the Number of Computer Unlocking Permutations
Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接:3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯,要想要能够将所有的电脑解锁&#x…...
Python爬虫(二):爬虫完整流程
爬虫完整流程详解(7大核心步骤实战技巧) 一、爬虫完整工作流程 以下是爬虫开发的完整流程,我将结合具体技术点和实战经验展开说明: 1. 目标分析与前期准备 网站技术分析: 使用浏览器开发者工具(F12&…...
CocosCreator 之 JavaScript/TypeScript和Java的相互交互
引擎版本: 3.8.1 语言: JavaScript/TypeScript、C、Java 环境:Window 参考:Java原生反射机制 您好,我是鹤九日! 回顾 在上篇文章中:CocosCreator Android项目接入UnityAds 广告SDK。 我们简单讲…...
解决本地部署 SmolVLM2 大语言模型运行 flash-attn 报错
出现的问题 安装 flash-attn 会一直卡在 build 那一步或者运行报错 解决办法 是因为你安装的 flash-attn 版本没有对应上,所以报错,到 https://github.com/Dao-AILab/flash-attention/releases 下载对应版本,cu、torch、cp 的版本一定要对…...
docker 部署发现spring.profiles.active 问题
报错: org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property spring.profiles.active imported from location class path resource [application-test.yml] is invalid in a profile specific resource [origin: class path re…...
Springboot社区养老保险系统小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,社区养老保险系统小程序被用户普遍使用,为方…...
NPOI操作EXCEL文件 ——CAD C# 二次开发
缺点:dll.版本容易加载错误。CAD加载插件时,没有加载所有类库。插件运行过程中用到某个类库,会从CAD的安装目录找,找不到就报错了。 【方案2】让CAD在加载过程中把类库加载到内存 【方案3】是发现缺少了哪个库,就用插件程序加载进…...
