Created
October 17, 2019 12:12
-
-
Save ahmadrosid/b9ce85d4c108579246f1e2fe79b960db to your computer and use it in GitHub Desktop.
Send udp data text android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.streamaudio; | |
import android.util.Log; | |
import java.io.IOException; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
import java.net.InetAddress; | |
import java.net.SocketException; | |
public class SendText implements Runnable{ | |
private int port = 2019; | |
private String ip = "x.x.x.x"; | |
private String message; | |
public SendText(String message) { | |
this.message = message; | |
} | |
@Override | |
public void run() { | |
try { | |
DatagramSocket udpSocket = new DatagramSocket(port); | |
InetAddress serverAddr = InetAddress.getByName(ip); | |
byte[] buffer = this.message.getBytes(); | |
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, serverAddr, port); | |
udpSocket.send(packet); | |
System.out.println("SEND DATA : " + this.message); | |
} catch (SocketException e) { | |
Log.e("Udp:", "Socket Error:", e); | |
} catch (IOException e) { | |
Log.e("Udp Send:", "IO Error:", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment