0

I am currently trying to send ble data from the app to my PC using sockets. As of now, I am able to send data to the server, but the server only receives one data and then stops receiving. I am suspecting that there is something wrong with my client side code. Any help would be appreciated

My SocketClass code:

inner class SocketClass{
    fun doInBackground(message: String): String {
        try {
            socket = Socket(SERVER_IP, SERVERPORT)
            val outputStream = OutputStreamWriter(socket?.getOutputStream())
            val printwriter = BufferedWriter(outputStream)
            printwriter.write(message)
            printwriter.flush()
            printwriter.close()
            socket?.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }catch (e: UnknownHostException){

        }
        return ""
    }
}

and in the blereadcharacteristic I call the class in a thread like this:

private fun readDeviceCharacteristic(characteristic: BluetoothGattCharacteristic) {
        var hashval:MutableMap<String, String> = mutableMapOf()
        var mainThreadList :MutableList<String> = mutableListOf()
        var currentTime: String = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(
            ZoneId.of("Asia/Seoul")).format(Instant.now())  // Get current Timestamp
        if(idlist.size != 0) {
            readval = characteristic.getStringValue(0)
            valdata = readval.subSequence(3, readval.length).toString()
            idNo = readval.subSequence(0, 2).toString()
            parseList.add(readval)
            mainThreadList = parseList.toMutableList().distinctBy{it.subSequence(0, 2)} as MutableList<String>
            if(mainThreadList.size == maxConnLimit) {
                bleDataList = mainThreadList
                bleDataList.forEach{
                    hashval[it.subSequence(0, 2).toString()] = it.subSequence(3, it.length).toString()
                }
                loghash = hashval.toSortedMap()
                txtRead = bleDataList.toString()
                parseList.clear()
                if(isChecked) {
                    savehash["Time"]?.add("$sec.$millis")
                    bleDataList.forEach {
                        savehash[it.subSequence(0, 2).toString()]?.add(
                            it.subSequence(
                                3,
                                it.length
                            ).toString()
                        )
                    }

                }else{
                    time = 0
                    sec = 0
                    millis = ""
                }
                if(isInsert){
                    Thread{
                        SocketClass().doInBackground("$loghash\r\n\r\n")
                    }.start()
                }

            }
        }
        isTxtRead = true
        bleDataList.clear()
    }

My server socket python code:

    import socket
import sys

host = '19.201.12.12'
port = 30001
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)

print("Listening for client . . .")
conn, address = server_socket.accept()
print("Connected to client at ", address)                                                  
while True:
    output = conn.recv(2048);
    if output.strip() == "disconnect":
        conn.close()
        sys.exit("Received disconnect message.  Shutting down.")

    elif output:
        print("Message received from client:")
        print(output)

I am able to get the first data sent, but do not receive anything after. Any help would be greatly appreciated thankyou!

1 Answer 1

1

Your server code only accepts one client connection. So, here's the timelime.

  1. Python waits at accept.
  2. Kotlin connects
  3. Python moves into while True loop.
  4. Kotlin writes, then closes the socket.

At that point, nothing further will be received by the Python code, but because you never call accept again, you can't accept another connection.

You need to have the accept in a loop. You handle that connection until it closes, then you loop to another accept.

You might consider using the socketserver module, which automates some of this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.