AC Socket-based network programming UDP protocol simulation one to one online

First, demand:

Teachers and students one on one online

Second, the analysis:

  • Socket implemented using a protocol based on the UDP network programming;
  • It does not require the use of I / O streams to achieve data transmission;
  • Each data transmission unit are unified into a packaged as packets, the data sender;
  • Packet sent to the network, the packets in the network to find its destination;

Third, the transmitting end

Ideas:

1, using the specified port DatagramSocket, the transmitting side created;
2, will be transferred into a byte array type base;
3, DatagramPacket encapsulated package, requiring a destination;
4, a package sent Send (DatagramPacket P);
. 5, the release of resources;

Code:

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();
    }
}

Fourth, the recipient

Ideas:

Note: the same repeat next protocol port No;
1, using the specified port DatagramSocket, creating the receiving end;
2, to prepare a packaged DatagramPacket wrap;
3, receive packages blocking the receive (DatagramPacket P);
. 4, the analysis of data bytes reduced to the corresponding array type;
5, release of resources;

Code:

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();
    }
}

Five test

Note that the port number does not conflict;

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 a little too simple ...
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/Daria_/article/details/90703907
Recommended