标签:
UdpSend.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class UdpSend {
public static void main(String args[]){
DatagramSocket ds = null;
DatagramPacket dp = null;
try{
ds = new DatagramSocket(3000); //实例化一个UDP的套接字并绑定3000绑定
}catch(SocketException e){
}
String str = "hello world";
try{
dp = new DatagramPacket(str.getBytes(),str.length(),InetAddress.getByName("localhost"),9000);
}
catch(UnknownHostException ex1){
}
try{
ds.send(dp); //发送数据到本机的9000
}catch(IOException e){
}
ds.close();
}
}
标签:
原文地址:http://www.cnblogs.com/wangtingyi/p/4766243.html