0

how do i make a application in android studio that creates a udpclient then you can enter the ip port then message of the datagram like anything you typed in the string or somthing then click the send buton.

try
{
socket.connect(host, port);
}

like socket.connect in c# for java in android studio. socket = new

Socket(addressfamily.internetwork, sockettype.dgram, protocaltype.udp);
byte[] bytes2 = encoding.utf8.getbytes(s);
try{
socket.connect(host, port);
}
socket.send(bytes2);

thats an example of it in c# on windows but i wanna know how to do the same thing but for android in android studio.

J. Doe
  • 1
  • 2
  • Possible duplicate of [How to use UDP sockets in android?](https://stackoverflow.com/questions/12652261/how-to-use-udp-sockets-in-android) – user3486184 Oct 15 '18 at 21:37

1 Answers1

0

Here are some code excerpts I have used previously. Hopefully this might get you started.

import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

// Code to place inside class/function...

udpStr = "My String";
int msg_length = udpStr.length();
byte[] message = udpStr.getBytes();

try {
  DatagramSocket s = new DatagramSocket();
} catch (SocketException e) {
  e.printStackTrace();
}

DatagramSocket s = null;
try {
  s = new DatagramSocket();
} catch (SocketException e) {
  e.printStackTrace();
}

DatagramPacket p = new DatagramPacket(message, msg_length, local, server_port);
try {
  s.send(p);
} catch (IOException e) {
  e.printStackTrace();
}
Tim D
  • 650
  • 1
  • 12
  • 18