Contenidos
Código en Android Studio
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/buttonSendMulticast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enviar Multicast" android:layout_centerInParent="true"/> <TextView android:id="@+id/textViewReceivedMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/buttonSendMulticast" android:layout_marginTop="16dp" android:text="Mensaje recibido: " android:layout_centerHorizontal="true"/> </RelativeLayout> |
AndroidManifest.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
MainActivity.kt
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 |
package com.example.myapplication import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.net.DatagramPacket import java.net.InetAddress import java.net.MulticastSocket class MainActivity : AppCompatActivity() { private lateinit var multicastSocket: MulticastSocket private lateinit var receiveThread: Thread override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val buttonSendMulticast: Button = findViewById(R.id.buttonSendMulticast) val textViewReceivedMessage: TextView = findViewById(R.id.textViewReceivedMessage) buttonSendMulticast.setOnClickListener { sendMessage("¡Hola a todos!") } receiveThread = Thread { try { val multicastGroup = "224.0.0.1" val multicastPort = 5000 val multicastAddress = InetAddress.getByName(multicastGroup) multicastSocket = MulticastSocket(multicastPort) multicastSocket.joinGroup(multicastAddress) while (!Thread.currentThread().isInterrupted) { val receiveBuffer = ByteArray(1024) val receivePacket = DatagramPacket(receiveBuffer, receiveBuffer.size) multicastSocket.receive(receivePacket) val receivedMessage = String(receivePacket.data, receivePacket.offset, receivePacket.length) runOnUiThread { // Actualizar la interfaz de usuario con el mensaje recibido textViewReceivedMessage.text = receivedMessage } } } catch (e: Exception) { e.printStackTrace() } } receiveThread.start() } private fun sendMessage(message: String) { Thread { try { val multicastGroup = "224.0.0.1" val multicastPort = 5000 val multicastAddress = InetAddress.getByName(multicastGroup) val sendBuffer = message.toByteArray() val sendPacket = DatagramPacket(sendBuffer, sendBuffer.size, multicastAddress, multicastPort) val sendSocket = MulticastSocket() sendSocket.send(sendPacket) sendSocket.close() } catch (e: Exception) { e.printStackTrace() } }.start() } override fun onDestroy() { super.onDestroy() receiveThread.interrupt() multicastSocket.close() } } |
Código en IntelliJ IDEA
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 |
import java.net.DatagramPacket import java.net.InetAddress import java.net.MulticastSocket fun main() { val multicastGroup = "224.0.0.1" // Grupo de multicast al que te quieres unir val multicastPort = 5000 // Puerto del grupo de multicast val messageToSend = "¡Hola a to2!" // Mensaje a enviar try { val multicastAddress = InetAddress.getByName(multicastGroup) val multicastSocket = MulticastSocket(multicastPort) multicastSocket.joinGroup(multicastAddress) // Unirse al grupo de multicast // Enviar mensaje val sendBuffer = messageToSend.toByteArray() val sendPacket = DatagramPacket(sendBuffer, sendBuffer.size, multicastAddress, multicastPort) multicastSocket.send(sendPacket) // Escuchar mensajes continuamente while (true) { val receiveBuffer = ByteArray(1024) val receivePacket = DatagramPacket(receiveBuffer, receiveBuffer.size) multicastSocket.receive(receivePacket) val receivedMessage = String(receivePacket.data, receivePacket.offset, receivePacket.length) println("Mensaje recibido: $receivedMessage") } } catch (e: Exception) { e.printStackTrace() } } |