Http Server / Client communication #7

Merged
faraphel merged 24 commits from test-http into main 2024-05-17 17:22:56 +02:00
Showing only changes of commit 08956df9c8 - Show all commits

View file

@ -28,7 +28,6 @@ class MainActivity : ComponentActivity() {
// ----- CONNECTION
/*
/*
val intentFilter = IntentFilter()
@ -41,6 +40,7 @@ class MainActivity : ComponentActivity() {
this.registerReceiver(MyBroadcastReceiver(this), intentFilter)
*/
/*
// get the WiFi direct manager
val manager: WifiP2pManager? = this.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager?
if (manager == null) {
@ -121,5 +121,35 @@ class MainActivity : ComponentActivity() {
}
}
*/
Log.d("socket", "Test")
Thread {
try {
Log.d("socket", "Starting server")
ServerSocket(26203).use { serverSocket ->
while (true) {
val clientSocket = serverSocket.accept()
val data = clientSocket.getInputStream().read()
Log.i("socket", "Data received (${clientSocket.inetAddress}) : $data")
clientSocket.close()
}
}
} catch (e: Exception) {
Log.d("socket", "server already started. Becoming client")
while (true) {
Socket("localhost", 26203).use { socket ->
socket.outputStream.write("test".toByteArray())
Log.i("socket", "Data sent")
}
}
}
}.start()
}
}