UDP之JAVA实现双方通信

采用多线程实现
发送方线程:

package ThreadUDP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class ThreadSend implements Runnable {
    private DatagramSocket datagramSocket;
    private int fromport;
    private int toport;
    private String fromip;

    public ThreadSend(int fromport, int toport, String fromip) throws SocketException {
        this.fromport = fromport;
        this.toport = toport;
        this.fromip = fromip;
         this.datagramSocket = new DatagramSocket(fromport);
    }

    @Override
    public void run() {


        while (true){
            BufferedReader Reader = new BufferedReader(new InputStreamReader(System.in));

            String data = null;
            try {
                data = Reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            byte[] dataBytes = data.getBytes();

            DatagramPacket datagramPacket = new DatagramPacket(dataBytes,0,dataBytes.length,new InetSocketAddress(fromip,toport));

            try {
                datagramSocket.send(datagramPacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (data.equals("bye")){
                break;
            }
        }
        datagramSocket.close();
    }

    }

接收方线程:

package ThreadUDP;

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

public class ThreadReceive implements Runnable {
    private DatagramSocket datagramSocket;
    private int fromport;
    private String msgFrom;

    public ThreadReceive( String msgFrom, int fromport) throws SocketException {
        this.msgFrom=msgFrom;
        this.fromport = fromport;
       this.datagramSocket = new DatagramSocket(this.fromport);
    }

    @Override
    public void run() {


        while (true){
            byte[] bytes = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(bytes,0,bytes.length);
            try {
                datagramSocket.receive(datagramPacket);
            } catch (IOException e) {
                e.printStackTrace();
            }
            byte[] data = datagramPacket.getData();
            String receivedata = new String(data,0,data.length);

            System.out.println(msgFrom+":"+receivedata);
            if(receivedata.equals("bye")){
                break;

            }
        }
        datagramSocket.close();
    }

    }

以学生和老师对话为例
学生:

package ThreadUDP;

import java.net.SocketException;

public class Student {
    public static void main(String[] args) throws SocketException {
        try {
            new Thread(new ThreadSend(3333,6666,"localhost")).start();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        new Thread(new ThreadReceive("老师",8888)).start();
    }
}

老师:

package ThreadUDP;

import java.net.SocketException;

public class Teacher {
    public static void main(String[] args) {
        try {
            new Thread(new ThreadReceive("学生",6666)).start();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        try {
            new Thread(new ThreadSend(2222,8888,"localhost")).start();
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}

此次学习巩固了多线程,并且更了解JAVA的UDP的发送接收方式

上一篇:java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized...


下一篇:JAVA获取本机IP和Mac地址