TCP与UDP 区别:TCP面向连接,先建立连接通道经过三次握手,传输可靠可以大量传输数据传输效率低 UDP面向无连接,数据包限制在64k,需要多次传输,不可靠传输速度快 UDP的使用DategramSocket对象此类表示用来发送和接收数据报包的套接字。(用来接收和发送数据包,相当于建立一个服务,包括定义一个本地的接口)DatagramPacket此类表示数据报包。将数据封装到包中(包括IP地址、接收端的端口号) 使用UDP发送数据例子:
public static void sendUDPInfo() {
try {
// 建立一个soket服务 参数为指定的本地发送端口
DatagramSocket ds = new DatagramSocket(10005);
byte[] by = "我是要发送的数据".getBytes();
// 指定要发送的数据包,包含IP地址,对方接收的商端口号与要发送的信息
// 其中IP地址中的 最后一位如果是255的话,就是向全网段发送广播消息 如果是0的话代表一个网络段
DatagramPacket dp = new DatagramPacket(by, by.length, InetAddress
.getByName("192.168.1.117"), 10006);
ds.send(dp);
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
使用UPD接收数据例子:
public static void receUDPInfo() {
try {
DatagramSocket ds = new DatagramSocket(10006);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
System.out.println("ip=" + dp.getAddress().getHostAddress()
+ " Data="
+ new String(dp.getData(), 0, dp.getData().length));
ds.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
使用UDP键盘录入不间断的向指定端口发送数据:
public static void sendUDPInfo() throws Exception {
// 建立一个soket服务 参数为指定的本地发送端口
DatagramSocket ds = new DatagramSocket(10005);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String len = null;
while ((len = br.readLine()) != null) {
// 指定要发送的数据包,包含IP地址,对方接收的商端口号与要发送的信息
// 其中IP地址中的 最后一位如果是255的话,就是向全网段发送广播消息 如果是0的话代表一个网络段
DatagramPacket dp = new DatagramPacket(len.getBytes(), len
.getBytes().length, InetAddress.getByName("192.168.1.117"),
10007);
ds.send(dp);
}
ds.close();
}
使用UDP不间断的等待接收发送到本端口的数据:
public static void receUDPInfo() {
try { //开启的服务一定要放到循环外面。否则循环执行完一遍之的事还要重新再开启指定的商品,会报端口占用异常
DatagramSocket ds = new DatagramSocket(10006);
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
System.out.println("ip=" + dp.getAddress().getHostAddress()
+ " Data="
+ new String(dp.getData(), 0, dp.getData().length));
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
UDP即时聊天:
public static void main(String[] args) throws Exception {
new Thread() {
public void run() {
try {
sendUDPInfo();
} catch (Exception e) {
new RuntimeException();
}
};
}.start();
new Thread() {
public void run() {
try {
receUDPInfo();
} catch (Exception e) {
new RuntimeException();
}
};
}.start();
}
public static void sendUDPInfo() throws Exception {
// 建立一个soket服务 参数为指定的本地发送端口
DatagramSocket ds = new DatagramSocket(10005);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String len = null;
while ((len = br.readLine()) != null) {
// 指定要发送的数据包,包含IP地址,对方接收的商端口号与要发送的信息
// 其中IP地址中的 最后一位如果是255的话,就是向全网段发送广播消息 如果是0的话代表一个网络段
DatagramPacket dp = new DatagramPacket(len.getBytes(), len
.getBytes().length, InetAddress.getByName("192.168.1.117"),
10007);
ds.send(dp);
}
ds.close();
}
public static void receUDPInfo() throws Exception {
// 开启的服务一定要放到循环外面。否则循环执行完一遍之的事还要重新再开启指定的商品,会报端口占用异常
DatagramSocket ds = new DatagramSocket(10007);
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
System.out.println("ip=" + dp.getAddress().getHostAddress()
+ " Data=" + new String(dp.getData(), 0, dp.getLength()));
}
}