android_socket_fr

1.界面布局 

用baseActivity,修改 。反馈通讯用线程,命令通讯用服务,程序通讯用意图服务

2.socket服务类

参考参考2              修改如下

  使用Socket与服务端进行通信      asm4

class Connect {
    //普通数据交互接口
    private val sc: Socket? = null

//    //图片交互接口
//    private val ImageSocket: Socket? = null

    //普通交互流
    private var dout: OutputStream? = null
    private var din: InputStreamReader? = null

//    //图片交互流
//    private val imageInputStream: InputStream? = null
//    private val imageFileOutputSteam: DataOutputStream? = null

    //已连接标记
    var isConnect = false
//    var ImageConncet = false

    /**
     * 初始化普通交互连接
     */
    fun InitConnect(ip:String,port:Int){
        try {
            val sc:Socket = Socket(ip, port) //通过socket连接服务器
            val din = InputStreamReader(sc.getInputStream(), "gb2312")  //获取输入流并转换为StreamReader,约定编码格式
            val dout =OutputStreamWriter(sc.getOutputStream())    //获取输出流
            sc.setSoTimeout(10000)  //设置连接超时限制
            if (sc != null && din != null && dout != null) {    //判断一下是否都连上,避免NullPointException
                isConnect = true
                System.out.println("connect server successful")
            } else {
                System.out.println("connect server failed,now retry...")
                InitConnect(ip, port)
            }
        } catch (e: IOException) {      //获取输入输出流是可能报IOException的,所以必须try-catch
            e.printStackTrace()
        }
    }

    /**
     * 发送数据至服务器
     * @param message 要发送至服务器的字符串
     */
    fun sendMessage(message: String?) {
        var message = message
        try {
            if (isConnect) {
                if (dout != null && message != null) {        //判断输出流或者消息是否为空,为空的话会产生nullpoint错误
                    message = """
                        $message

                        """.trimIndent() //末尾加上换行让服务器端有消息返回
                    val me = message.toByteArray()
                    dout!!.write(me)
                    dout!!.flush()
                } else {
                    System.out.println("The message to be sent is empty or have no connect")
                }
                System.out.println("send message successful")
            } else {
                System.out.println("no connect to send message")
            }
        } catch (e: IOException) {
            System.out.println("send message to cilent failed")
            e.printStackTrace()
        }
    }

    fun receiveMessage(): String? {
        var message: String? = ""
        try {
            if (isConnect) {
                System.out.println("开始接收服务端信息")
                val inMessage = CharArray(1024)     //设置接受缓冲,避免接受数据过长占用过多内存
                val a = din!!.read(inMessage) //a存储返回消息的长度
                if (a <= -1) {
                    return null
                }
                System.out.println("reply length:$a")
                message = String(inMessage, 0, a) //必须要用new string来转换
                System.out.println(message)
            } else {
                System.out.println("no connect to receive message")
            }
        } catch (e: IOException) {
            System.out.println("receive message failed")
            e.printStackTrace()
        }
        return message
    }
    /**
     * 登陆方法
     * @param name 用户名
     * @param password  登陆密码
     */
//    fun login(name: String?, password: String?) {
//        InitConnect()		//仅是演示的时候将初始化和关闭放在具体方法中,实际使用中需要多次重复使用Socket,所以不能这样使用
//        val job = JSONObject() //创建一个json对象存放login信息
//        job.put("name", name)   //装填数据
//        job.put("password", password)
//        job.put("login", "1")
//        job.put("type", "search")
//        var Msend: String = ""
//        Msend = job.toJSONString(); //转换为jsonstring
//        System.out.println("编码成功,编码结果为:"+Msend);
//        sendMessage(Msend)  //发送消息
//        var reply: String? = ""
//        reply = receiveMessage() //接收服务器返回信息
//        /**
//         * @TODO 收到服务端返回的信息后进行相关处理
//         */
//        CloseConnect()
//    }

    /**
     * 关闭连接
     */
    public fun CloseConnect() {
        try {
            if (din != null) {
                din!!.close()
            }
            if (dout != null) {
                dout!!.close()
            }
            if (sc != null) {
                sc!!.close()
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        isConnect = false
        System.out.println("关闭连接")
    }

//    companion object {
//        //服务端地址及接口
//        private const val ip = "127.0.0.1"  //本地服务端地址
//        private const val port = 2333
//    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值