Servidor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
package com.example.myapplication import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Canvas import androidx.compose.foundation.background import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.Text import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Canvas import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Paint import androidx.compose.ui.graphics.PaintingStyle import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import java.io.BufferedReader import java.io.InputStreamReader import java.net.ServerSocket class MainActivity : ComponentActivity() { private val port = 8080 // Puerto para la conexión private var receivedMessage by mutableStateOf("") // Mensaje recibido del cliente private var cursorPosition by mutableStateOf(0f to 0f) // Posición del cursor en la app override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Column( modifier = Modifier .fillMaxSize() .background(Color.White) .padding(16.dp) ) { Text("Received Message: $receivedMessage") // Muestra el mensaje recibido DrawCursor(cursorPosition) startServer() } } } @Composable private fun DrawCursor(position: Pair<Float, Float>) { Canvas(modifier = Modifier.fillMaxSize()) { drawCircle( color = Color.Red, center = Offset(position.first, position.second), radius = 20f, ) } } private fun startServer() { Thread { try { val serverSocket = ServerSocket(port) while (true) { val client = serverSocket.accept() val inputStream = client.getInputStream() val bufferedReader = BufferedReader(InputStreamReader(inputStream)) var message: String? while (bufferedReader.readLine().also { message = it } != null) { showMessage(message) } bufferedReader.close() client.close() } } catch (e: Exception) { e.printStackTrace() } }.start() } private fun showMessage(message: String?) { runOnUiThread { receivedMessage = message ?: "No se recibió ningún mensaje" // Mueve el cursor basado en el mensaje recibido when (message) { "derecha" -> moveCursor(50f, 0f) "izquierda" -> moveCursor(-50f, 0f) "arriba" -> moveCursor(0f, -50f) "abajo" -> moveCursor(0f, 50f) else -> {} // Puedes manejar otros casos o mensajes aquí } } } private fun moveCursor(deltaX: Float, deltaY: Float) { val (currentX, currentY) = cursorPosition cursorPosition = currentX + deltaX to currentY + deltaY } } |
Cliente
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import java.awt.MouseInfo import java.awt.Point import java.io.OutputStream import java.net.Socket fun main() { try { val socket = Socket("192.168.1.54", 8080) // Cambia a la dirección IP de tu servidor val outputStream: OutputStream = socket.getOutputStream() var lastX = MouseInfo.getPointerInfo().location.x var lastY = MouseInfo.getPointerInfo().location.y while (true) { val currentPoint: Point = MouseInfo.getPointerInfo().location val currentX = currentPoint.x val currentY = currentPoint.y val deltaX = currentX - lastX val deltaY = currentY - lastY val movement = when { deltaX > 0 -> "derecha" deltaX < 0 -> "izquierda" deltaY > 0 -> "abajo" deltaY < 0 -> "arriba" else -> "sin movimiento" } outputStream.write(movement.toByteArray()) outputStream.write("\n".toByteArray()) println(movement) outputStream.flush() lastX = currentX lastY = currentY Thread.sleep(100) } } catch (e: Exception) { e.printStackTrace() } } |