鱼欲遇雨:每日都学习一点,持之以恒,天道酬勤!不能用电脑时,提前补上!(2012.9.6)
UDP
1 不可靠的2 效率高
3 数据报/非连接
-----------音频
-----------视频
-----------。。。。
// TestUDPServer.java
import java.net.*;
public class TestUDPServer {
public static void main(String args[]) throws Exception{
byte buf[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket ds = new DatagramSocket(5678);
while(true) {
ds.receive(dp);
System.out.println(new String(buf, 0, dp.getLength()));
}
}
}
// TestUDPClient.java
import java.net.*;
public class TestUDPClient {
public static void main(String args[]) throws Exception{
byte[] buf = (new String("Hello, world!")).getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
}
}