Android Studio项目——TCP客户端

目录

一、TCP客户端UI

1、UI展示

2、xml代码

二、TCP客户端数据发送

三、TCP客户端数据接收


一、TCP客户端UI

1、UI展示

2、xml代码

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="#59b1ef"
    tools:context=".MainActivity4">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_client_send"
        android:text="发送"
        android:textSize="25sp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:onClick="btn_client_send_clicked" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:layout_height="300dp"
        android:layout_below="@id/btn_client_send"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#ffffff" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_start"
        android:text="开始"
        android:layout_below="@+id/text_view"
        android:textSize="25sp"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:onClick="btn_start_clicked" />

    <TextView
        android:id="@+id/text_view2"
        android:layout_width="match_parent"
        android:textSize="30sp"
        android:text="0"
        android:layout_height="100dp"
        android:layout_below="@id/btn_start"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#ffffff" />


</RelativeLayout>

二、TCP客户端数据发送

java 复制代码
public void Client_SendMessage(){
        try {
            Socket client = new Socket("192.168.124.6",8089);
            OutputStream out = client.getOutputStream();
            out.write("meng".getBytes());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void btn_client_send_clicked(View v){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Client_SendMessage();
            }
        }).start();
    }

三、TCP客户端数据接收

java 复制代码
textView1 = findViewById(R.id.text_view);
textView1.setText("数据接收框");
textView2 = findViewById(R.id.text_view2);
textView2.setText("0");
java 复制代码
new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Socket client = new Socket("192.168.124.6",8089);
                        int len;
                        InputStream in = client.getInputStream();
                        byte[] data = new byte[128];
                        len = in.read(data);
                        String str = new String(data,0,len);
                        Message msg2 = new Message();
                        Bundle b = new Bundle();
                        b.putString("msg", str);
                        msg2.setData(b);
                        h2.sendMessage(msg2);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }).start();
java 复制代码
h2 = new Handler(){
            @Override
            public void handleMessage(Message msg2) {
                // TODO Auto-generated method stub
                super.handleMessage(msg2);
                Bundle b = msg2.getData();
                String string  = b.getString("msg");
                textView1.setText(string);
            }
        };
相关推荐
chengooooooo17 分钟前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦20 分钟前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个20 分钟前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
楼台的春风3 小时前
【MCU驱动开发概述】
c语言·驱动开发·单片机·嵌入式硬件·mcu·自动驾驶·嵌入式
南宫生3 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长4 小时前
Maven 基础环境搭建与配置(一)
java·maven
风与沙的较量丶5 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_748251725 小时前
SpringBoot3 升级介绍
java
simplepeng5 小时前
我的天,我真是和androidx的字体加载杠上了
android
极客先躯6 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分