Yes there are ways to bind to server ports with official APIs, example can be requested to Fable, Claude, or whoever you feel like it.
Also, you can't install things on termux if you have android 6 or below. You must install postmarketos and thus an unlocked bootloader.
<uses-permission android:name="android.permission.INTERNET" />
Then you need to write the server handling code on a background application thread, with perfectly working server sockets.
This only works on wlan, or local network when using bluethooth, but who wants data modem for a server anyway.
With Gemini's compliment,
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.ServerSocket
import java.net.Socket
import java.net.SocketException
class AndroidTcpServer {
private var serverSocket: ServerSocket? = null
private var isRunning = false
// Call this function from a CoroutineScope (e.g., lifecycleScope.launch)
async suspend fun startServer(port: Int = 8080) {
withContext(Dispatchers.IO) {
try {
serverSocket = ServerSocket(port)
isRunning = true
// Loop to continuously accept incoming client connections
while (isRunning) {
val clientSocket = serverSocket?.accept() // Blocks until a client connects
if (clientSocket != null) {
// Handle each client connection in a separate background routine
handleClient(clientSocket)
}
}
} catch (e: SocketException) {
// Triggered when stopServer() closes the socket
println("Server socket closed: ${e.message}")
} catch (e: Exception) {
e.printStackTrace()
} finally {
stopServer()
}
}
}
private suspend fun handleClient(socket: Socket) {
withContext(Dispatchers.IO) {
try {
val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
val outputStream = socket.getOutputStream()
// Read incoming text from client
val incomingMessage = reader.readLine()
println("Received from client: $incomingMessage")
// Send a simple response back to the client
val response = "Hello from Android Server!\n"
outputStream.write(response.toByteArray())
outputStream.flush()
} catch (e: Exception) {
e.printStackTrace()
} finally {
// Always close individual client connections
socket.close()
}
}
}
fun stopServer() {
isRunning = false
try {
serverSocket?.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
As you might imagine by my comment history, I am a big fan of letting Android be Android, with Android native apps, not stuffing GNU/Linux into an APK.Containers are nice, but they're not strictly necessary. On Google Pixel 6 and newer phones, you can take it a step further and root your phone and run a KVM-accellerated arm64 Linux virtual machine on your phone. There's a port of Limbo PC emulator that includes support for the KVM accelleration, and it works quite well! It even allows you to pass through USB devices to the guest.
> Java and Kotlin as regular Android app already provide all the juice required to do the job.
To do what job? What job can Java and Kotlin do on a phone to replace a Hetzner VPS?