当前位置: 首页 > news >正文

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布局设计&#xff1a; <?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&#xff08;GET_UTC_DATE&#xff09;二、使用步骤1.跨年日期2.跨月日期&#xff08;判断 闰年 or 平年、用于计算2月天数&#xff0c;正常大小月&#xff09;3.跨日日期3.正常日期 总…...

[BUG] Hadoop-3.3.4集群yarn管理页面子队列不显示任务

1.问题描述 使用yarn调度任务时&#xff0c;在CapacityScheduler页面上单击叶队列&#xff08;或子队列&#xff09;时&#xff0c;不会显示应用程序任务信息&#xff0c;root队列可以显示任务。此外&#xff0c;FairScheduler页面是正常的。 No matching records found2.原…...

Java 缓存中间件

Java 缓存中间件 关键词&#xff1a;Spring Cache、J2Cache、JetCache 一 、JSR 107 JSR107 中制订了 Java 缓存的规范。 因此&#xff0c;在很多缓存框架、缓存库中&#xff0c;其 API 都参考了 JSR 107 规范。 img Java Caching 定义了 5 个核心接口 CachingProvider - 定义…...

使用cmake配置matplotlibcpp生成VS项目

https://gitee.com/feboreigns/matplotlibcpp 这篇文章需要一些cmake基础&#xff0c;python基础&#xff0c;visualstudio基础 准备环境 注意如果在VS平台使用必须要手动下载python&#xff0c;不能使用conda里面的&#xff0c;比如3.8版本&#xff0c;因为conda里面没有py…...

Android : 画布绘制矩形和文字 让其居中显示简单应用

示例图&#xff1a; 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…...

科研学习|论文解读——融合类目偏好和数据场聚类的协同过滤推荐算法研究

论文链接&#xff08;中国知网&#xff09;&#xff1a; 融合类目偏好和数据场聚类的协同过滤推荐算法研究 - 中国知网 (cnki.net) 摘要&#xff1a;[目的/意义]基于近邻用户的协同过滤推荐作为推荐系统应用最广泛的算法之一&#xff0c;受数据稀疏和计算可扩展问题影响&#x…...

算法学习系列(十五):最小堆、堆排序

目录 引言一、最小堆概念二、堆排序模板&#xff08;最小堆&#xff09;三、模拟堆 引言 这个堆排序的话&#xff0c;考的还挺多的&#xff0c;主要是构建最小堆&#xff0c;并且在很多情况下某些东西还用得着它来优化&#xff0c;比如说迪杰斯特拉算法可以用最小堆优化&#…...

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 包&#xff1b; GitHub - elastic/elasticsearch-hadoop at 6.8 编译 7.17 版本时很正常&#xff0c;注意设置下环境变量就好&#xff0c;JAVA8_HOME/.... 编译 6.8 版本时&#xff08;要求jdk8 / jdk9&#xff09;&#xff0c;出现…...

面试经典150题(50-53)

leetcode 150道题 计划花两个月时候刷完&#xff0c;今天&#xff08;第二十二天&#xff09;完成了4道(50-53)150&#xff1a; 50.&#xff08;141. 环形链表&#xff09;题目描述&#xff1a; 给你一个链表的头节点 head &#xff0c;判断链表中是否有环。 如果链表中有某个…...

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原子性&#xff1a;事务中的操作要么全部完成&#xff0c;要么全部失败。 Consistency一致性&#xff1a;事务操作前后&#xff0c;数据满足完整性约束。 Isolation隔离性&#xff1a;允许并发执行事务&#xff0c;每个事务都有自己的数据空间&…...

高阶组件和高阶函数是什么

高阶组件和高阶函数都是在函数式编程中常见的概念。 高阶组件&#xff08;Higher-Order Component, HOC&#xff09;是一种函数&#xff0c;接受一个组件作为参数&#xff0c;并返回一个新的组件。它可以用来增强现有的组件&#xff0c;给它添加额外的功能或属性。高阶组件在R…...

初步认识API安全

一、认识API 1. 什么是API API(应用程序接口)&#xff1a;是一种软件中介&#xff0c;它允许两个不相关的应用程序相互通信。它就像一座桥梁&#xff0c;从一个程序接收请求或消息&#xff0c;然后将其传递给另一个程序&#xff0c;翻译消息并根据 API 的程序设计执行协议。A…...

MybatisX逆向工程方法

官方文档链接&#xff1a;MybatisX快速开发插件 | MyBatis-Plus (baomidou.com) 使用MybatisX可以快速生成mapper文件&#xff0c;实体类和service及实现 效果 方法&#xff1a;首先下载mybatisX插件 然后创建数据库信息 然后选中表&#xff0c;右键&#xff0c;点击Mybatis…...

每日一题:LeetCode-LCR 179. 查找总价格为目标值的两个商品

每日一题系列&#xff08;day 16&#xff09; 前言&#xff1a; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f308; &#x1f50e…...

SpringBoot 3.2.0 基于Logback定制日志框架

依赖版本 JDK 17 Spring Boot 3.2.0 工程源码&#xff1a;Gitee 日志门面和日志实现 日志门面&#xff08;如Slf4j&#xff09;就是一个标准&#xff0c;同JDBC一样来制定“规则”&#xff0c;把不同的日志系统的实现进行了具体的抽象化&#xff0c;只提供了统一的日志使用接…...

微软发布安卓版Copilot,可免费使用GPT-4、DALL-E 3

12月27日&#xff0c;微软的Copilot助手&#xff0c;可在谷歌应用商店下载。目前&#xff0c;只有安卓版&#xff0c;ios还无法使用。 Copilot是一款类ChatGPT助手支持中文&#xff0c;可生成文本/代码/图片、分析图片、总结内容等&#xff0c;二者的功能几乎没太大差别。 值…...

【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地址&#xff1b; 中断向量表映射 中断向量表映射到SRA…...

SciencePlots——绘制论文中的图片

文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了&#xff1a;一行…...

大型活动交通拥堵治理的视觉算法应用

大型活动下智慧交通的视觉分析应用 一、背景与挑战 大型活动&#xff08;如演唱会、马拉松赛事、高考中考等&#xff09;期间&#xff0c;城市交通面临瞬时人流车流激增、传统摄像头模糊、交通拥堵识别滞后等问题。以演唱会为例&#xff0c;暖城商圈曾因观众集中离场导致周边…...

可靠性+灵活性:电力载波技术在楼宇自控中的核心价值

可靠性灵活性&#xff1a;电力载波技术在楼宇自控中的核心价值 在智能楼宇的自动化控制中&#xff0c;电力载波技术&#xff08;PLC&#xff09;凭借其独特的优势&#xff0c;正成为构建高效、稳定、灵活系统的核心解决方案。它利用现有电力线路传输数据&#xff0c;无需额外布…...

系统设计 --- MongoDB亿级数据查询优化策略

系统设计 --- MongoDB亿级数据查询分表策略 背景Solution --- 分表 背景 使用audit log实现Audi Trail功能 Audit Trail范围: 六个月数据量: 每秒5-7条audi log&#xff0c;共计7千万 – 1亿条数据需要实现全文检索按照时间倒序因为license问题&#xff0c;不能使用ELK只能使用…...

【机器视觉】单目测距——运动结构恢复

ps&#xff1a;图是随便找的&#xff0c;为了凑个封面 前言 在前面对光流法进行进一步改进&#xff0c;希望将2D光流推广至3D场景流时&#xff0c;发现2D转3D过程中存在尺度歧义问题&#xff0c;需要补全摄像头拍摄图像中缺失的深度信息&#xff0c;否则解空间不收敛&#xf…...

TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案

一、TRS收益互换的本质与业务逻辑 &#xff08;一&#xff09;概念解析 TRS&#xff08;Total Return Swap&#xff09;收益互换是一种金融衍生工具&#xff0c;指交易双方约定在未来一定期限内&#xff0c;基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...

Python ROS2【机器人中间件框架】 简介

销量过万TEEIS德国护膝夏天用薄款 优惠券冠生园 百花蜂蜜428g 挤压瓶纯蜂蜜巨奇严选 鞋子除臭剂360ml 多芬身体磨砂膏280g健70%-75%酒精消毒棉片湿巾1418cm 80片/袋3袋大包清洁食品用消毒 优惠券AIMORNY52朵红玫瑰永生香皂花同城配送非鲜花七夕情人节生日礼物送女友 热卖妙洁棉…...

CVE-2020-17519源码分析与漏洞复现(Flink 任意文件读取)

漏洞概览 漏洞名称&#xff1a;Apache Flink REST API 任意文件读取漏洞CVE编号&#xff1a;CVE-2020-17519CVSS评分&#xff1a;7.5影响版本&#xff1a;Apache Flink 1.11.0、1.11.1、1.11.2修复版本&#xff1a;≥ 1.11.3 或 ≥ 1.12.0漏洞类型&#xff1a;路径遍历&#x…...

计算机基础知识解析:从应用到架构的全面拆解

目录 前言 1、 计算机的应用领域&#xff1a;无处不在的数字助手 2、 计算机的进化史&#xff1a;从算盘到量子计算 3、计算机的分类&#xff1a;不止 “台式机和笔记本” 4、计算机的组件&#xff1a;硬件与软件的协同 4.1 硬件&#xff1a;五大核心部件 4.2 软件&#…...

[ACTF2020 新生赛]Include 1(php://filter伪协议)

题目 做法 启动靶机&#xff0c;点进去 点进去 查看URL&#xff0c;有 ?fileflag.php说明存在文件包含&#xff0c;原理是php://filter 协议 当它与包含函数结合时&#xff0c;php://filter流会被当作php文件执行。 用php://filter加编码&#xff0c;能让PHP把文件内容…...