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">

    <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.mfbz.cn/a/274585.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

利用网络教育系统构建个性化学习平台

在现代教育中&#xff0c;网络教育系统作为一种创新的学习方式&#xff0c;为学生提供了更加个性化和灵活的学习体验。在本文中&#xff0c;我们将通过简单的技术代码&#xff0c;演示如何构建一个基础的网络教育系统&#xff0c;为学生提供个性化的学习路径和资源。 1. 环境…

python3处理docx并flask显示

前言&#xff1a; 最近有需求处理docx文件&#xff0c;并讲内容显示到页面&#xff0c;对world进行在线的阅读&#xff0c;这样我这里就使用flaskDocument对docx文件进行处理并显示&#xff0c;下面直接上代码&#xff1a; Document处理&#xff1a; 首先下载Document的库文…

k8s二进制最终部署(网络 负载均衡和master高可用)

k8s中的通信模式 1、pod内部之间容器与容器之间的通信&#xff0c;在同一个pod 中的容器共享资源和网络&#xff0c;使用同一个网络命名空间&#xff0c;可以直接通信的 2、同一个node节点之内&#xff0c;不同pod之间的通信&#xff0c;每个pod都有一个全局的真实的IP地址&a…

计算机网络——传输层(五)

前言&#xff1a; 最重要的网络层我们已经学习完了&#xff0c;下面让我们再往上一层&#xff0c;对网络层的上一层传输层进行一个学习与了解&#xff0c;学习网络层的基本概念和网络层中的TCP协议和UDP协议 目录 ​编辑一、传输层的概述&#xff1a; 1.传输层&#xff1a; …

使用Visual Studio 2022 winform项目打包成安装程序.exe

winform项目打包 1.安装扩展插件 Microsoft Visual Studio Installer Projects 20222.在解决方案上新建一个setup project 项目3.新建成功如下图&#xff0c;之后添加你的winform程序生成之后的debug下的文件4.在Application Folder上点击右键->Add->项目输出->主输出…

【稳定检索|投稿优惠】2024年绿色能源与电网电力系统国际会议(ICGEGPS 2024)

2024年绿色能源与电网电力系统国际会议(ICGEGPS 2024) 2024 International Conference on Green Energy and Grid Power Systems(ICGEGPS) 一、【会议简介】 2024年绿色能源与电网电力系统国际会议(ICGEGPS 2024)将在宜宾盛大召开。本次会议将聚焦绿色能源与电网电力系统的最新…

javaEE -19(9000 字 JavaScript入门 - 4)

一&#xff1a; jQuery jQuery是一个快速、小巧且功能丰富的JavaScript库。它旨在简化HTML文档遍历、事件处理、动画效果以及与后端服务器的交互等操作。通过使用jQuery&#xff0c;开发者可以以更简洁、更高效的方式来编写JavaScript代码。 jQuery提供了许多易于使用的方法和…

构建安全防线:SDLC中的供应链攻击防范最佳实践与Log360解决方案

在过去的12个月里&#xff0c;有10家公司发现了软件供应链风险。供应链中依赖关系的增加扩大了对手的攻击面。这也导致威胁行为者将注意力从仅影响最终用户的下游链转移到上游链&#xff0c;影响供应商、客户和最终用户。因此&#xff0c;让我们立即讨论如何使你的SOC团队在产品…

多输入多输出 | MATLAB实现SSA-CNN麻雀算法优化卷积神经网络多输入多输出预测

多输入多输出 | MATLAB实现SSA-CNN麻雀算法优化卷积神经网络多输入多输出预测 目录 多输入多输出 | MATLAB实现SSA-CNN麻雀算法优化卷积神经网络多输入多输出预测预测效果基本介绍模型背景程序设计参考资料 预测效果 基本介绍 MATLAB实现SSA-CNN麻雀算法优化卷积神经网络多输入…

CentOS 5/6/7 基于开源项目制作openssh 9.6p1 rpm包—— 筑梦之路

背景介绍 开源项目地址&#xff1a;https://github.com/boypt/openssh-rpms.git 该项目主要支持了centos 5 、6、7版本&#xff0c;针对使用了比较老的操作系统进行openssh安全加固&#xff0c;还是不错的项目&#xff0c;使用简单、一件制作&#xff0c;欢迎大家去支持作者。…

泰迪智能科技“供需对接就业育人项目”介绍

为帮助用人单位培养和招聘更多实用型、复合型和紧缺型人才,推动高校人才培养与就业有机联动、人才供需有效对接促进高校毕业生更加充分更高质量就业&#xff0c;经广东泰迪智能科技股份有限公司申报、全国高校毕业生就业创业指导委员会专家组审核&#xff0c;泰迪智能科技“供需…

案例189:基于微信小程序的高校教务管理系统设计与实现

文末获取源码 开发语言&#xff1a;Java 框架&#xff1a;springboot JDK版本&#xff1a;JDK1.8 数据库&#xff1a;mysql 5.7 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;Maven3.5.4 小程序框架&#xff1a;uniapp 小程序开发软件&#xff1a;HBuilder …

【操作系统】探究驱动奥秘:驱动程序设计的解密与实战

​&#x1f308;个人主页&#xff1a;Sarapines Programmer&#x1f525; 系列专栏&#xff1a;Linux专栏&#xff1a;《探秘Linux | 操作系统解密》⏰诗赋清音&#xff1a;月悬苍穹泛清辉&#xff0c;梦随星河徜徉辉。情牵天际云千层&#xff0c;志立乘风意自飞。 目录 &…

【kubernetes】集群网络(一):基础篇

Flannel 1 路由表 & arp & fdb 1.1 路由表 任何网络设备都需要路由表&#xff0c;路由表用来决定&#xff0c;当收到数据包时&#xff0c;该向哪里进行转发。路由表项通常会包含以下几个字段&#xff1a; Destination&#xff1a;目的地Gateway&#xff1a;网关Mas…

opencv和gdal的读写图片波段顺序问题

最近处理遥感影像总是不时听到 图片的波段错了&#xff0c;一开始不明就里&#xff0c;都是图片怎么就判断错了。 1、图像RGB波段顺序判断 后面和大家交流&#xff0c;基本上知道了一个判断标准。 一般来说&#xff0c;进入人眼的自然画面在计算机视觉中一般是rgb波段顺序表示…

模式识别与机器学习-无监督学习-聚类

无监督学习-聚类 监督学习&无监督学习K-meansK-means聚类的优点&#xff1a;K-means的局限性&#xff1a;解决方案&#xff1a; 高斯混合模型&#xff08;Gaussian Mixture Models&#xff0c;GMM&#xff09;多维高斯分布的概率密度函数&#xff1a;高斯混合模型&#xff…

React onClick 事件阻止冒泡

在 React 中&#xff0c;你可以通过使用 onClick 事件来处理点击事件&#xff0c;并且可以通过在事件处理函数中调用 stopPropagation() 方法来阻止事件冒泡。本文将为你提供 类组件 和 函数式组件 两种示例。 一、类组件示例 import React from react;class MyComponent exte…

C++ //例13.14 将一批数据以二进制形式存放在磁盘文件中。例13.15 将刚才以二进制形式存放在磁盘文件中的数据读入内存并在显示器上显示。

C程序设计 &#xff08;第三版&#xff09; 谭浩强 例13.14 例13.15 例13.14 将一批数据以二进制形式存放在磁盘文件中。 例13.15 将刚才以二进制形式存放在磁盘文件中的数据读入内存并在显示器上显示。 IDE工具&#xff1a;VS2010 Note: 使用不同的IDE工具可能有部分差异。…

透彻掌握GIT基础使用

网址 https://learngitbranching.js.org/?localezh_CN 清屏 clear重新开始reset

秋招复习篇之代码规范

目录 前言 1、变量命名 2、代码空格 1&#xff09;操作符左右一定有空格&#xff0c; 2&#xff09;分隔符&#xff08;, 和;&#xff09;前一位没有空格&#xff0c;后一位保持空格&#xff0c;例如&#xff1a; 3&#xff09;大括号和函数保持同一行&#xff0c;并有一个空格…
最新文章