Android Studio项目——TCP客户端

目录

一、TCP客户端UI

1、UI展示

2、xml代码

二、TCP客户端数据发送

三、TCP客户端数据接收


一、TCP客户端UI

1、UI展示

2、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客户端数据发送

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客户端数据接收

textView1 = findViewById(R.id.text_view);
textView1.setText("数据接收框");
textView2 = findViewById(R.id.text_view2);
textView2.setText("0");
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();
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);
            }
        };

相关推荐

  1. TCP、UDP客户

    2024-01-30 15:10:02       18 阅读
  2. Python3 TCP 客户

    2024-01-30 15:10:02       27 阅读
  3. go实现tcp客户

    2024-01-30 15:10:02       22 阅读
  4. C++客户服务器TCP创建

    2024-01-30 15:10:02       31 阅读
  5. Lwip之TCP客户示例记录

    2024-01-30 15:10:02       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-30 15:10:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-30 15:10:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-30 15:10:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-30 15:10:02       20 阅读

热门阅读

  1. LarkXR渲染服务器架构升级,探索XR新体验

    2024-01-30 15:10:02       39 阅读
  2. 前端自己整理的学习面试笔记

    2024-01-30 15:10:02       37 阅读
  3. XR虚拍技术:重塑微剧与短剧产业的创新引擎

    2024-01-30 15:10:02       43 阅读
  4. C#关键字ref和out

    2024-01-30 15:10:02       35 阅读
  5. 图的前向星表示2

    2024-01-30 15:10:02       38 阅读
  6. 大模型-文本扩展&聊天机器人

    2024-01-30 15:10:02       37 阅读
  7. 【美团】无人机-大数据开发工程师

    2024-01-30 15:10:02       39 阅读
  8. css 让 width = (100% - 30px)

    2024-01-30 15:10:02       40 阅读
  9. udp(无连接)客户端和服务端代码

    2024-01-30 15:10:02       48 阅读
  10. 快捷键:IDEA 清理无效导入依赖

    2024-01-30 15:10:02       35 阅读
  11. STL之stack 【栈】

    2024-01-30 15:10:02       29 阅读