基于UDP协议的Socket的网络编程模拟一对一的在线交流

一、需求:

老师和学生在线一对一交流

二、分析:

  • 使用基于UDP协议的Socket网络编程实现;
  • 不需要利用I/O流实现数据的传输;
  • 每个数据发送单元被统一封装成数据包的方式,发送方将数据;
  • 包发送到网络中,数据包在网络中去寻找它的目的地;

三、发送端

思路:

1、使用DatagramSocket 指定端口,创建发送端;
2、将基本类型 转成字节数组;
3、封装成DatagramPacket包裹,需要指定目的地;
4、发送包裹 send(DatagramPacket p);
5、释放资源;

代码:

package network.Test.UDP.talkthread;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Scanner;

/**
 * @Author Daria
 * @Description
 * @Date 2019/5/27 -- 20:03
 */
public class TalkSend implements Runnable{
    private DatagramSocket clientSocket;
    private Scanner scanner;
    private String toIP;
    private int toPort;
    public TalkSend(int port, String toIP, int toPort) throws SocketException {
        this.toIP = toIP;
        this.toPort = toPort;
        this.clientSocket = new DatagramSocket(port);
        this.scanner = new Scanner(System.in);
    }
    @Override
    public void run() {
        while (true) {
            String datas = scanner.nextLine();
            byte[] bytes = datas.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,
                    new InetSocketAddress(this.toIP,this.toPort));

            try {
                clientSocket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (datas.equals("exit")) {
                break;
            }
        }
        clientSocket.close();
    }
}

四、接收方

思路:

注意:同一个协议下不允许端口重复;
1、使用DatagramSocket 指定端口,创建接收端;
2、准备容器 封装成DatagramPacket包裹;
3、阻塞式接收包裹 receive(DatagramPacket p);
4、分析数据 将字节数组还原为对应的类型;
5、释放资源;

代码:

package network.Test.UDP.talkthread;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @Author Daria
 * @Description
 * @Date 2019/5/27 -- 20:02
 */
public class TalkReceive implements Runnable {
    private DatagramSocket serverSocket;
    private String from;
    public TalkReceive(int port, String from) throws SocketException {
        this.serverSocket = new DatagramSocket(port);
        this.from = from;
    }
    @Override
    public void run() {
        while (true) {
            byte[] bytes = new byte[1024*6];
            DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
            try {
                serverSocket.receive(packet);
                byte[] datas = packet.getData();
                int length = datas.length;
                String data = new String (datas,0,length);
                System.out.println(from + data);
                if (data.equals("exit")) {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        serverSocket.close();
    }
}

五、测试

注意端口号不能冲突;

public class TalkTeacher {
    public static void main(String[] args) throws SocketException {
        new Thread(new TalkReceive(9998,"学生:")).start(); //接收
        new Thread(new TalkSend(5555,"127.0.0.1",8888)).start();
    }
}
public class TalkStudent {
    public static void main(String[] args) throws SocketException {
        new Thread(new TalkSend(7777,"127.0.0.1",9998)).start();
        new Thread(new TalkReceive(8888,"老师:")).start();
    }
}

emmmm有点过于简陋…
基于UDP协议的Socket的网络编程模拟一对一的在线交流_第1张图片
基于UDP协议的Socket的网络编程模拟一对一的在线交流_第2张图片

你可能感兴趣的:(网络编程)