DatagramSocket这个接受和发送数据报
UDP网络协议下的Socket对象:
UDP(User Datagram Protocol): 用户数据报协议
追求速度的网络协议
特点:1. 无连接(不需要知道发送方的IP)
2. 不可靠(只管发送,不管接收)
3. 不安全
创建UDP客户端程序的流程:
1. 创建客户端套接字
2. 发送 / 接收数据
3. 关闭套接字
datagramSocket是java封装的基于UDP网络协议的套接字对象
DatagramPacket进行数据的封装和解封装
- package com.openlab.day27;
-
- import java.io.IOException;
- import java.net.*;
-
- public class TestUDP {
- public static void main(String[] args) {
- DatagramSocket datagramSocket = null;
- try {
- // 注意,创建一个udp socket对象,默认占据了8080
- datagramSocket = new DatagramSocket(8080);
-
- String msg = "你好啊,socket对象";
- byte[] bytes = msg.getBytes();
- // 数据报对象
- DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("127.0.0.1", 8888));
-
- // 发送数据
- datagramSocket.send(packet);
-
- System.out.println("数据已经成功发送");
-
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- datagramSocket.close();
- }
- }
- }
发送:
- package com.openlab.day27;
-
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetSocketAddress;
- import java.net.SocketException;
- import java.util.Scanner;
-
- public class TestUDP03 {
-
- public static final String DES_IP = "127.0.0.1";
- public static final int DES_PORT = 8888;
-
- public static void main(String[] args) {
-
- DatagramSocket socket = null;
- Scanner sc = null;
- try {
- socket = new DatagramSocket(9999);
- sc = new Scanner(System.in);
- while (true) {
- System.out.print("请输入您要发送的数据:");
- String msg = sc.nextLine();
- byte[] buf = msg.getBytes();
- InetSocketAddress address = new InetSocketAddress(DES_IP, DES_PORT);
- DatagramPacket pd = new DatagramPacket(buf,0, buf.length, address);
- socket.send(pd);
- System.out.println("数据发送成功!!");
- }
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- socket.close();
- }
-
-
- }
- }
接收:
- package com.openlab.day27;
-
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.DatagramSocketImpl;
- import java.net.SocketException;
-
- public class TestUDP02 {
- public static void main(String[] args) {
- DatagramSocket socket = null;
- try {
- socket = new DatagramSocket(8888);
- while (true) {
- byte[] buf = new byte[1024];
- DatagramPacket pd = new DatagramPacket(buf, 0, buf.length);
- System.out.println("正在等待客户端发送数据……");
- // 等待接受数据
- socket.receive(pd);
- System.out.println("接收到数据……");
- // 将数据转换出来
- byte[] msg = pd.getData();
- String sendIP = pd.getAddress().getHostAddress();
- int sendPort = pd.getPort();
- System.out.println("接受到"+ sendIP +":"+ sendPort +"发送过来的数据,数据是:"+ new String(msg));
- }
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- socket.close();
- }
-
-
- }
- }
每个启动两个线程,主线程充当一个线程,自己再写一个线程,一个线程用来接收数据,一个线程用来发送数据。模拟两个人,只需改变IP,端口即可互相通信
第一个:
- package com.openlab.day27;
-
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetSocketAddress;
- import java.net.SocketException;
- import java.util.Scanner;
-
- public class TestUDPSever02 {
-
- public static final String DES_IP = "127.0.0.1";
- public static final int DES_PORT = 9999;
-
- public static void main(String[] args) {
- DatagramSocket socket = null;
- Scanner sc = null;
- try {
- socket = new DatagramSocket(8888);
-
- // 启动一个接收到线程,来进行数据接受
- UDPRreceiveMsg02 udpRreceiveMsg = new UDPRreceiveMsg02(socket);
- udpRreceiveMsg.start();
-
- sc = new Scanner(System.in);
- while (true) {
- System.out.print(">");
- String msg = sc.nextLine();
- byte[] buf = msg.getBytes();
- InetSocketAddress address = new InetSocketAddress(DES_IP, DES_PORT);
- DatagramPacket pd = new DatagramPacket(buf,0, buf.length, address);
- socket.send(pd);
- }
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- socket.close();
- }
-
- }
- }
-
- class UDPRreceiveMsg02 extends Thread {
- private DatagramSocket socket;
-
- public UDPRreceiveMsg02(DatagramSocket socket) {
- this.socket = socket;
- }
-
- @Override
- public void run() {
- try {
- receiveMsg();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- private void receiveMsg() throws IOException {
- while (true) {
- byte[] buf = new byte[1024];
- DatagramPacket pd = new DatagramPacket(buf, 0, buf.length);
- // 等待接受数据
- socket.receive(pd);
- // 将数据转换出来
- byte[] msg = pd.getData();
- String sendIP = pd.getAddress().getHostAddress();
- int sendPort = pd.getPort();
- System.out.println("接受到"+ sendIP +":"+ sendPort +"发送过来的数据,数据是:"+ new String(msg));
- }
- }
- }
第二个:
- package com.openlab.day27;
-
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetSocketAddress;
- import java.net.SocketException;
- import java.util.Scanner;
-
- public class TestUDPSever {
-
- public static final String DES_IP = "171.211.191.190";
- public static final int DES_PORT = 12340;
-
- public static void main(String[] args) {
- DatagramSocket socket = null;
- Scanner sc = null;
- try {
- socket = new DatagramSocket(9999);
-
- // 启动一个接收的线程,来进行数据接收
- UDPRreceiveMsg udpRreceiveMsg = new UDPRreceiveMsg(socket);
- udpRreceiveMsg.start();
-
- sc = new Scanner(System.in);
- while (true) {
- System.out.print(">");
- String msg = sc.nextLine();
- byte[] buf = msg.getBytes();
- InetSocketAddress address = new InetSocketAddress(DES_IP, DES_PORT);
- DatagramPacket pd = new DatagramPacket(buf,0, buf.length, address);
- socket.send(pd);
- }
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- socket.close();
- }
-
- }
- }
-
- class UDPRreceiveMsg extends Thread {
- private DatagramSocket socket;
-
- public UDPRreceiveMsg(DatagramSocket socket) {
- this.socket = socket;
- }
-
- @Override
- public void run() {
- try {
- receiveMsg();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- private void receiveMsg() throws IOException {
- while (true) {
- byte[] buf = new byte[1024];
- DatagramPacket pd = new DatagramPacket(buf, 0, buf.length);
- // 等待接受数据
- socket.receive(pd);
- // 将数据转换出来
- byte[] msg = pd.getData();
- String sendIP = pd.getAddress().getHostAddress();
- int sendPort = pd.getPort();
- System.out.println("接受到"+ sendIP +":"+ sendPort +"发送过来的数据,数据是:"+ new String(msg));
- }
- }
- }