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);
            }
        };
相关推荐
Dddle11 分钟前
C++:this指针
java·c语言·开发语言·c++
阿乾之铭36 分钟前
Spring Boot 参数验证
java·数据库·mysql
佩奇的技术笔记37 分钟前
Java学习手册:微服务设计原则
java·微服务
jiunian_cn40 分钟前
【c++】异常详解
java·开发语言·数据结构·c++·算法·visual studio
柯南二号43 分钟前
【后端】SpringBoot用CORS解决无法跨域访问的问题
java·spring boot·后端
千里马学框架1 小时前
重学安卓14/15自由窗口freeform企业实战bug-学员作业
android·framework·bug·systrace·安卓framework开发·安卓窗口系统·自由窗口
purrrew2 小时前
【Java ee初阶】初始网络
java·网络
程序员Bears2 小时前
从零打造个人博客静态页面与TodoList应用:前端开发实战指南
java·javascript·css·html5
Helibo443 小时前
GESPC++六级复习
java·数据结构·算法
柒七爱吃麻辣烫3 小时前
在Linux中安装JDK并且搭建Java环境
java·linux·开发语言