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);
            }
        };
相关推荐
陈大爷(有低保)4 分钟前
UDP Socket聊天室(Java)
java·网络协议·udp
kinlon.liu18 分钟前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证
王哲晓39 分钟前
Linux通过yum安装Docker
java·linux·docker
java66666888843 分钟前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存44 分钟前
源码分析:LinkedList
java·开发语言
执键行天涯1 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
Jarlen1 小时前
将本地离线Jar包上传到Maven远程私库上,供项目编译使用
java·maven·jar
蓑 羽1 小时前
力扣438 找到字符串中所有字母异位词 Java版本
java·算法·leetcode
Reese_Cool1 小时前
【C语言二级考试】循环结构设计
android·java·c语言·开发语言