Update TCPClient.kt

master
Zhengyu Peng 3 years ago
parent 67ec4d4e29
commit c0f272f594

@ -1,6 +1,5 @@
package com.rookiedev.hexapod.network package com.rookiedev.hexapod.network
import com.rookiedev.hexapod.ControlActivity
import java.io.* import java.io.*
import java.net.* import java.net.*
@ -8,34 +7,33 @@ import java.net.*
class TCPClient( class TCPClient(
ip: String?, ip: String?,
port: Int, port: Int,
messagelistener: OnMessageReceived?, messageListener: OnMessageReceived?,
onconnected: OnConnectEstablished?, onConnect: OnConnectEstablished?,
ondisconnect: OnDisconnected? onDisconnect: OnDisconnected?
) : ) :
Thread() { Thread() {
private var TCPSocket: Socket? = null private var tcpSocket: Socket? = null
private var SERVERIP: InetAddress? = null private var serverIp: InetAddress? = null
private val SERVERPORT: Int private var serverPort = 0
private var serverAddr: InetSocketAddress? = null private var serverAddress: InetSocketAddress? = null
private var TCPOut: PrintWriter? = null private var tcpOut: PrintWriter? = null
private var TCPIn: BufferedReader? = null private var tcpIn: BufferedReader? = null
private var mMessageListener: OnMessageReceived? = null private var mMessageListener: OnMessageReceived? = null
private var onConnected: OnConnectEstablished? = null private var onConnected: OnConnectEstablished? = null
private var onDisconnected: OnDisconnected? = null private var onDisconnected: OnDisconnected? = null
private var isConnected = false private var isConnected = false
private var pause = false // if the thread is paused by system
override fun run() { override fun run() {
try { try {
this.TCPSocket = Socket() this.tcpSocket = Socket()
this.TCPSocket!!.soTimeout = 3000 this.tcpSocket!!.soTimeout = 3000
this.TCPSocket!!.connect(serverAddr, 3000) // connecting socket and set timeout in 3s this.tcpSocket!!.connect(serverAddress, 3000) // connecting socket and set timeout in 3s
onConnected!!.onConnected() onConnected!!.onConnected()
TCPOut = PrintWriter( tcpOut = PrintWriter(
BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())), BufferedWriter(OutputStreamWriter(this.tcpSocket!!.getOutputStream())),
true true
) )
TCPIn = BufferedReader(InputStreamReader(this.TCPSocket!!.getInputStream())) tcpIn = BufferedReader(InputStreamReader(this.tcpSocket!!.getInputStream()))
// sendMessage("test")
isConnected = true isConnected = true
while (isConnected) { while (isConnected) {
sleep(1000) sleep(1000)
@ -60,18 +58,16 @@ class TCPClient(
// newMessage = message // newMessage = message
// isNewData = true // isNewData = true
if (this.TCPOut != null && !this.TCPOut!!.checkError()) { if (this.tcpOut != null && !this.tcpOut!!.checkError()) {
println("send message") println("send message")
this.TCPOut!!.print(message) this.tcpOut!!.print(message)
this.TCPOut!!.flush() this.tcpOut!!.flush()
} }
} }
fun stopClient() { fun stopClient() {
// sendMessage(Constants.requestMessage(Constants.REQUEST_DISCONNECT))
pause = true
isConnected = false isConnected = false
this.TCPSocket!!.close() this.tcpSocket!!.close()
} }
interface OnMessageReceived { interface OnMessageReceived {
@ -87,18 +83,18 @@ class TCPClient(
} }
init { init {
SERVERPORT = port serverPort = port
mMessageListener = messagelistener mMessageListener = messageListener
onConnected = onconnected onConnected = onConnect
onDisconnected = ondisconnect onDisconnected = onDisconnect
try { try {
SERVERIP = InetAddress.getByName(ip) serverIp = InetAddress.getByName(ip)
serverAddr = InetSocketAddress(SERVERIP, SERVERPORT) serverAddress = InetSocketAddress(serverIp, serverPort)
} catch (e: UnknownHostException) { } catch (e: UnknownHostException) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace() e.printStackTrace()
} }
pause = false
} }
} }
Loading…
Cancel
Save