java-udp编程

Posted 哎哟我去a

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java-udp编程相关的知识,希望对你有一定的参考价值。

TCP/IP UDP都是基于传输层的;而udp发送数据会出现丢包的情况,发送一个数据不管对方接收不接收,发送过去就完事了;

udp的特点:将数据源和目的封装成数据包中,不要建立连接;(DatagramPacket)

每个数据报的大小在限制64K以内

因无连接,是不可靠协议

不需要建立连接,速度快;

下面将编写一代代码展现UDP

一、client 端

  

public class UDPSocketClient {
    
    public static void main(String[] args) throws Throwable {
    
            //send();
        
            keySend();
    
    }

    private static void keySend() throws Throwable {
         
        try {
            
            //1、创建UDP服务
            DatagramSocket socket=new DatagramSocket();
            
            BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
            
             String message=null;
            
            while((message=reader.readLine())!=null) {
                
                
                if(message.equals("886"))  
                 
                    break;
                
                //2、 封装数据包                    
                DatagramPacket send=new DatagramPacket(message.getBytes(),message.getBytes().length,InetAddress.getByName("localhost"), 8088);
                //阻塞式 3、发送数据
                socket.send(send);
            }
            //4、释放资源
            socket.close();
            
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

二、SERVER端

public class UDPSocketServer {
    
    public static void main(String[] args) throws Throwable {
        //receive();
        Keyreceive();
    }

    private static void Keyreceive() throws Throwable {
         
        //1、创建udp socket并指定端口
        DatagramSocket socket = new DatagramSocket(8088);
        
        while(true) {
             //2、定义数据 要接收的报文
             byte[] bytes = new byte[1024];
             DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
             socket.receive(packet);//阻塞式
             String message=new String(packet.getData(),0,packet.getData().length);
             System.out.println("Server收到的消息为:"+message);
        }
        
         
    }
}

三、测试结果;

先把server端启动,然后开启client

客户端发送消息

 

服务端接收消息

 

 多线程进行聊天

一、客户端

public class UDPSocketClientThread implements Runnable{
    
    private DatagramSocket  datagramSocket;
    public UDPSocketClientThread(DatagramSocket  datagramSocket) {
        this.datagramSocket=datagramSocket;
    }

    @Override
    public void run() {
        
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        
         String message=null;
        
        try {
            while((message=reader.readLine())!=null) {
                
                if(message.equals("886"))  
                 
                    break;
                
                //2、发送数据
                DatagramPacket send=new DatagramPacket(message.getBytes(),message.getBytes().length,InetAddress.getByName("192.168.43.255"), 8088);
                //阻塞式
                this.datagramSocket.send(send);
            }
        } catch (UnknownHostException e) {
            
            e.printStackTrace();
        } catch (IOException e) {
            
            e.printStackTrace();
        }
    }
    
}

二、服务端

public class UDPSocketServerThread implements Runnable{
    
    DatagramSocket socket;
    
    public UDPSocketServerThread(DatagramSocket socket) {
        this.socket=socket;
    }
 

    @Override
    public void run() {
        
        while(true) {
             //2、定义数据 要接收的报文
             byte[] bytes = new byte[1024];
             DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
             try {
                socket.receive(packet);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//阻塞式
             String message=new String(packet.getData(),0,packet.getData().length);
             InetAddress inetAddress=null;
            try {
                inetAddress = InetAddress.getLocalHost();
                 System.out.println(inetAddress.getHostAddress()+"的消息为:"+message);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
    }
 
}

三、测试运行

public class UDPSocketRun {
    
    public static void main(String[] args) throws Throwable {
        DatagramSocket send=new DatagramSocket();
        DatagramSocket receive=new DatagramSocket(8088);
        new Thread(new UDPSocketClientThread(send)).start();
        new Thread(new  UDPSocketServerThread(receive)).start(); 
    }
}

 

 

以上是关于java-udp编程的主要内容,如果未能解决你的问题,请参考以下文章

JAVA-UDP

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

使用 Pygments 检测代码片段的编程语言

面向面试编程代码片段之GC

如何在 Django Summernote 中显示编程片段的代码块?