UDP聊天功能实现

UDP通信:

客户端

package lesson02;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UdpClientDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket();
        String msg="你好,服务器";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port=9090;
        DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length,localhost,port);
        socket.send(packet);
        socket.close();
    }
}

服务端

package lesson02;

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

public class UdpServerDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(9090);
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        socket.receive(packet);
        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        socket.close();
    }
}

聊天实现

接收端

package chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class ReceiveDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(6666);
        while(true){ byte[] container = new byte[1024];
            DatagramPacket packet = new DatagramPacket(container, 0, container.length);
            socket.receive(packet);
            byte[] data2 = packet.getData();
            String receiveData= new String(data2,0,data2.length);
            System.out.println(receiveData);
            if (data2.equals("bye")){
            break;
            }
        }
        socket.close();
    }
}

发送端

package chat;

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

public class SenderDemo01 {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(8888);
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true){String data=reader.readLine();
            byte[] datas = data.getBytes();
            DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
            socket.send(packet);
            if (datas.equals("bye")){
                break;
            }
        }

        socket.close();
    }
}

URL:统一资源定位符

网络聊天:

发送功能:

package lesson03;

//import com.sun.org.apache.xpath.internal.operations.String;

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 TalkSend implements Runnable {
    DatagramSocket socket=null;
    BufferedReader reader=null;
    private int fromport;
    private String toIP;
    private  int toport;

    public TalkSend(int fromport,String toIP,int toport) {
        this.fromport=fromport;
        this.toIP=toIP;
        this.toport=toport;
        try {
            socket = new DatagramSocket(fromport);
            reader = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void run() {

        while (true){
            try {
                String data=reader.readLine();
                byte[] datas = data.getBytes();
                DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toport));
                socket.send(packet);
                if (datas.equals("bye")){
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        socket.close();
    }
}

接受功能:

package lesson03;

//import com.sun.org.apache.xpath.internal.operations.String;

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

public class TalkReceive implements Runnable {
    DatagramSocket socket=null;
    private  int port;
    private String msgFrom;
    public TalkReceive(int port,String msgFrom) {
        this.msgFrom=msgFrom;
        this.port=port;
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        while(true){
            try {
                byte[] container = new byte[1024];
                DatagramPacket packet = new DatagramPacket(container, 0, container.length);
                socket.receive(packet);
                byte[] data2 = packet.getData();
                String receiveData= new String(data2,0,data2.length);
                System.out.println(msgFrom+":"+receiveData);
                if (data2.equals("bye")){
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        socket.close();

    }
}

学生端

package lesson03;

public class TalkStudents {
    public static void main(String[] args) {
        new Thread(new TalkSend(7777,"localhost",9999)).start();
        new Thread(new TalkReceive(8888,"teacher")).start();
    }
}

教师端

package lesson03;

public class TalkTeachers {
    public static void main(String[] args) {
        new Thread(new TalkReceive(9999,"students")).start();
        new Thread(new TalkSend(6666,"localhost",8888)).start();
    }
}

你可能感兴趣的:(udp,网络协议,网络)