当前位置: 首页 > 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…...

wordpress后台更新后 前端没变化的解决方法

使用siteground主机的wordpress网站&#xff0c;会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后&#xff0c;网站没有变化的情况。 不熟悉siteground主机的新手&#xff0c;遇到这个问题&#xff0c;就很抓狂&#xff0c;明明是哪都没操作错误&#x…...

TDengine 快速体验(Docker 镜像方式)

简介 TDengine 可以通过安装包、Docker 镜像 及云服务快速体验 TDengine 的功能&#xff0c;本节首先介绍如何通过 Docker 快速体验 TDengine&#xff0c;然后介绍如何在 Docker 环境下体验 TDengine 的写入和查询功能。如果你不熟悉 Docker&#xff0c;请使用 安装包的方式快…...

Xshell远程连接Kali(默认 | 私钥)Note版

前言:xshell远程连接&#xff0c;私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...

8k长序列建模,蛋白质语言模型Prot42仅利用目标蛋白序列即可生成高亲和力结合剂

蛋白质结合剂&#xff08;如抗体、抑制肽&#xff09;在疾病诊断、成像分析及靶向药物递送等关键场景中发挥着不可替代的作用。传统上&#xff0c;高特异性蛋白质结合剂的开发高度依赖噬菌体展示、定向进化等实验技术&#xff0c;但这类方法普遍面临资源消耗巨大、研发周期冗长…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

【配置 YOLOX 用于按目录分类的图片数据集】

现在的图标点选越来越多&#xff0c;如何一步解决&#xff0c;采用 YOLOX 目标检测模式则可以轻松解决 要在 YOLOX 中使用按目录分类的图片数据集&#xff08;每个目录代表一个类别&#xff0c;目录下是该类别的所有图片&#xff09;&#xff0c;你需要进行以下配置步骤&#x…...

【决胜公务员考试】求职OMG——见面课测验1

2025最新版&#xff01;&#xff01;&#xff01;6.8截至答题&#xff0c;大家注意呀&#xff01; 博主码字不易点个关注吧,祝期末顺利~~ 1.单选题(2分) 下列说法错误的是:&#xff08; B &#xff09; A.选调生属于公务员系统 B.公务员属于事业编 C.选调生有基层锻炼的要求 D…...

什么是Ansible Jinja2

理解 Ansible Jinja2 模板 Ansible 是一款功能强大的开源自动化工具&#xff0c;可让您无缝地管理和配置系统。Ansible 的一大亮点是它使用 Jinja2 模板&#xff0c;允许您根据变量数据动态生成文件、配置设置和脚本。本文将向您介绍 Ansible 中的 Jinja2 模板&#xff0c;并通…...

20个超级好用的 CSS 动画库

分享 20 个最佳 CSS 动画库。 它们中的大多数将生成纯 CSS 代码&#xff0c;而不需要任何外部库。 1.Animate.css 一个开箱即用型的跨浏览器动画库&#xff0c;可供你在项目中使用。 2.Magic Animations CSS3 一组简单的动画&#xff0c;可以包含在你的网页或应用项目中。 3.An…...

uniapp 实现腾讯云IM群文件上传下载功能

UniApp 集成腾讯云IM实现群文件上传下载功能全攻略 一、功能背景与技术选型 在团队协作场景中&#xff0c;群文件共享是核心需求之一。本文将介绍如何基于腾讯云IMCOS&#xff0c;在uniapp中实现&#xff1a; 群内文件上传/下载文件元数据管理下载进度追踪跨平台文件预览 二…...