• 网络编程应用:基于UDP协议【实现聊天程序】--练习


    要求:

    使用UDP协议实现一个聊天程序
    

    代码:


    发送端:

    package UDP聊天程序;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    public class Send {
        public static void main(String[] args) {
            System.out.println("发送端已启动!");
    
            DatagramSocket socket=null;
            Scanner scanner=new Scanner(System.in);
            try {
                //1.创建Datagramsocket对象
                socket=new DatagramSocket();
    
                while(true){
                    //2.要发送的数据
                    String string=scanner.nextLine();
    
                    //3.创建数据包
                    DatagramPacket packet=new DatagramPacket(string.getBytes(), string.getBytes().length,InetAddress.getLocalHost(),6666);
                    //4.发送数据
                    socket.send(packet);
                    //若我方发送了bye,马上结束聊天
                    if(string.equals("bye")){
                        System.out.println("bye,聊天结束!");
                        break;
                    }
    
                    //5.接收--接收端回复的数据
                    byte[] bs=new byte[1024];
                    DatagramPacket packet2=new DatagramPacket(bs, bs.length);
                    socket.receive(packet2);
                    String string2=new String(packet2.getData(), 0, packet2.getLength());
                    //若对方发送了bye,马上结束聊天
                    if(string2.equals("bye")){
                        System.out.println("bye,聊天结束!");
                        break;
                    }
                    System.out.println("接收端回复:"+string2);
                }
    
    
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if(socket!=null){
                    socket.close();
                }
            }
    
        }
    }
    

    接收端:

    package UDP聊天程序;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;
    import java.util.Scanner;
    
    public class Receive {
        public static void main(String[] args) {
            System.out.println("接收端已启动!");
    
            DatagramSocket socket=null;
            Scanner scanner=new Scanner(System.in);
            try {
                //1.创建Datagrams对象
                socket=new DatagramSocket(6666);
    
                while(true){
                    //2.创建数据包packet
                    byte[] buf=new byte[1024];
                    DatagramPacket packet=new DatagramPacket(buf, buf.length);
                    //3.接收数据
                    socket.receive(packet);
                    String string=new String(packet.getData(), 0, packet.getLength());
                    //若对方发送了bye,马上结束聊天
                    if(string.equals("bye")){
                        System.out.println("bye,聊天结束!");
                        break;
                    }
                    System.out.println("发送端说:"+string);
    
                    //4.给发送端回复
                    String string2=scanner.nextLine();
                    DatagramPacket packet2=new DatagramPacket(string2.getBytes(), string2.getBytes().length,packet.getAddress(),packet.getPort());
                    socket.send(packet2);
                    //若我方发送了bye,马上结束聊天
                    if(string2.equals("bye")){
                        System.out.println("bye,聊天结束!");
                        break;
                    }
                }
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if(socket!=null){
                    socket.close();
                }
            }
    
        }
    }
    

    运行结果:


    接收端聊天列表:

    这里写图片描述


    发送端聊天列表:

    这里写图片描述

  • 相关阅读:
    在Matlab2018b中配置MinGW-w64 C/C++ 编译器
    电脑忽然黑屏
    Linux中drwxr-xr-x.的意思和权限
    tensorflow 和cuda对应关系
    apt-get update 升级错误
    修改模型参数名
    tensor转化为ndarray
    Ubuntu GitLab仓库服务器搭建
    友元
    常函数 常对象
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6854006.html
Copyright © 2020-2023  润新知