由UDP协议,完成多线程版本的一个聊天室

本地既可以是服务端,又可以客户端
其中服务端(收数据)由子线程开启;
客户端(发数据)部分一般实现即可;
具体实现如下、

客户端1import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class ClientThread {
     
    public static void main(String[] args) throws IOException {
     
        //接收端,相当于服务器;
        new Thread(){
     
            @Override
            public void run() {
     
                try {
     
                    DatagramSocket ds = new DatagramSocket(6666);
                    System.out.println("服务器正常开启,请发送数据:");
                    while (true){
     
                        //创建空数据报包用于存储传入的数据;
                        byte[] bytes = new byte[1024];
                        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                        ds.receive(dp);
                        //获取数据;
                        byte[] data = dp.getData();
                        int length = dp.getLength();
                        String str = new String(data, 0, length);

                        //获取地址
                        InetAddress address = dp.getAddress();
                        String hostName = address.getHostName();
                        String hostAddress = address.getHostAddress();

                        System.out.println(hostAddress+"回复你:"+str);
                        if (str.equals("bye")){
     
                            break;
                        }
                    }
                    ds.close();
                } catch (IOException e) {
     
                    e.printStackTrace();
                }
            }
        }.start();
        //发送端,相当于客户端;
        DatagramSocket ds = new  DatagramSocket();
        sendMsg(ds);
    }

    private static void sendMsg(DatagramSocket ds) throws IOException {
     
        Scanner sc = new Scanner(System.in);
        while (true){
     
            System.out.println("请输入发送内容:");
            String str = sc.nextLine();

            //创建数据报包;
            DatagramPacket dp = new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("192.168.10.103"), 8888);
            ds.send(dp);
            if (str.equals("bye")){
     
                break;
            }
        }
        //释放资源,前提是循环里面要有出口;
        ds.close();

    }

}
客户端2import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class ServerThread {
     
    public static void main(String[] args) throws IOException {
     
        //接收端,相当于服务器;
        new Thread() {
     
            @Override
            public void run() {
     
                try {
     
                    DatagramSocket ds = new DatagramSocket(8888);
                    System.out.println("服务器正常开启,请发送数据:");
                    while (true) {
     
                        //创建空数据报包用于存储传入的数据;
                        byte[] bytes = new byte[1024];
                        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
                        ds.receive(dp);
                        //获取数据;
                        byte[] data = dp.getData();
                        int length = dp.getLength();
                        String str = new String(data, 0, length);

                        //获取地址
                        InetAddress address = dp.getAddress();
                        String hostName = address.getHostName();
                        String hostAddress = address.getHostAddress();

                        System.out.println(hostAddress + "回复你:" + str);
                        if (str.equals("bye")) {
     
                            break;
                        }
                    }
                    ds.close();
                } catch (IOException e) {
     
                    e.printStackTrace();
                }
            }
        }.start();
        //发送端,相当于客户端;
        DatagramSocket ds = new DatagramSocket();
        sendMsg(ds);
    }

    private static void sendMsg(DatagramSocket ds) throws IOException {
     
        Scanner sc = new Scanner(System.in);
        while (true) {
     
            System.out.println("请输入发送内容:");
            String str = sc.nextLine();

            //创建数据报包;
            DatagramPacket dp = new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("192.168.10.103"), 6666);
            ds.send(dp);
            if (str.equals("bye")) {
     
                break;
            }
        }
        //释放资源,前提是循环里面要有出口;
        ds.close();

    }
}


你可能感兴趣的:(JAVA)