kotlin,socket网络传输数据

MainActivity.kt

Kotlin 复制代码
package com.example.applicationtest2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
//import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.TextView
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileInputStream
import java.net.Socket


class MainActivity : AppCompatActivity() {
    private lateinit var transferButton: Button
    private lateinit var resultTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        transferButton = findViewById(R.id.transferButton)
        resultTextView = findViewById(R.id.resultTextView)

        transferButton.setOnClickListener {
            Thread {
                val serverAddress = "127.0.0.1" // Kotlin 服务器的 IP 地址
                val serverPort = 12345 // Kotlin 服务器的端口号
                val filePath = "/path/to/file" // 待传输的文件路径

                try {
                    val socket = Socket(serverAddress, serverPort)
                    val file = File(filePath)

                    val outputStream = BufferedOutputStream(socket.getOutputStream())
                    val inputStream = BufferedInputStream(FileInputStream(file))

                    // 发送文件名和文件大小给 Kotlin 服务器
                    val fileNameBytes = file.name.toByteArray()
                    val fileSizeBytes = file.length().toString().toByteArray()
                    outputStream.write(fileNameBytes.size)
                    outputStream.write(fileNameBytes)
                    outputStream.write(fileSizeBytes.size)
                    outputStream.write(fileSizeBytes)
                    outputStream.flush()

                    // 接收服务器确认
                    val confirmation = socket.getInputStream().read()
                    if (confirmation == 1) {
                        // 开始传输文件数据
                        val buffer = ByteArray(8192)
                        var bytesRead: Int
                        while (inputStream.read(buffer).also { bytesRead = it } != -1) { //从输入流读取数据到缓冲区中
                            outputStream.write(buffer, 0, bytesRead)  //将缓冲区的数据写入到输出流中
                            outputStream.flush()

                            // 等待对方确认
                            val acknowledgement = socket.getInputStream().read()
                            if (acknowledgement != 1) {
                                runOnUiThread {
                                    resultTextView.text = "传输失败,未收到对方确认"
                                }
                                break
                            }
                        }

                        socket.shutdownOutput()

                        // 接收并显示传输结果
                        val result = socket.getInputStream().bufferedReader().readLine()
                        runOnUiThread {
                            resultTextView.text = result
                        }
                    } else {
                        runOnUiThread {
                            resultTextView.text = "传输失败,对方拒绝接收"
                        }
                    }

                    inputStream.close()
                    outputStream.close()
                    socket.close()
                } catch (e: Exception) {
                    e.printStackTrace()
                    runOnUiThread {
                        resultTextView.text = "传输失败:" + e.message
                    }
                }
            }.start()
        }
    }
}

activity_mian.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <Button
        android:id="@+id/transferButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="传输" />

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />


</LinearLayout>
相关推荐
海洲探索-Hydrovo2 小时前
TTP Aether X 天通透传模块丨国产自主可控大数据双向通讯定位模组
网络·人工智能·科技·算法·信息与通信
触想工业平板电脑一体机2 小时前
【触想智能】工业安卓一体机在人工智能领域上的市场应用分析
android·人工智能·智能电视
2501_915921433 小时前
iOS 是开源的吗?苹果系统的封闭与开放边界全解析(含开发与开心上架(Appuploader)实战)
android·ios·小程序·uni-app·开源·iphone·webview
allk554 小时前
OkHttp源码解析(一)
android·okhttp
allk554 小时前
OkHttp源码解析(二)
android·okhttp
半梦半醒*4 小时前
zabbix安装
linux·运维·前端·网络·zabbix
南尘NCA86665 小时前
企业微信防封防投诉拦截系统:从痛点解决到技术实现
java·网络·企业微信
程序猿费益洲6 小时前
Docker 网络详解:(三)四大网络模式
网络·docker·容器
2501_915909067 小时前
原生 iOS 开发全流程实战,Swift 技术栈、工程结构、自动化上传与上架发布指南
android·ios·小程序·uni-app·自动化·iphone·swift
Mr_Meng_De7 小时前
网络安全认证培训机构的痛点
网络