UDP发送类
属于那种发完就跑的
一些不要求接收的,发送频率也不大的就用这个好点
也可以参考下,具体的发送流程
package utils;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UdpUtils
{
public static void send( String ip, int port, String msg )
{
byte[] bytes = null;
try { bytes = msg.getBytes("utf8"); }
catch( Exception e) {bytes = null;}
if( bytes != null ) send(ip, port, bytes);
}
public static void send( String ip, int port, byte[] bytes )
{
send(ip, port, bytes, 0, bytes.length);
}
public static void send( String ip, int port, byte[] bytes, int offset, int len )
{
try
{
DatagramSocket ds = new DatagramSocket();
DatagramPacket pack = new DatagramPacket(bytes, 0, len, InetAddress.getByName(ip), port);
ds.send(pack);
ds.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}