zoukankan      html  css  js  c++  java
  • Java--NIO-UDP Socket

    1、首先使用DatagramSocket实现UDP Socket客户端,并且使用DatagramPacket封装要发送和接收的数据

    package com.seeyon.nio.UDP;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    /**
     * Created by yangyu on 2017/2/23.
     */
    public class UdpClient {
    
        public static void main(String[] args) {
            try(DatagramSocket datagramSocket = new DatagramSocket()) {
                InetAddress inetAddress = InetAddress.getByName("localhost");
                datagramSocket.connect(inetAddress,8777);
    
                byte[] bytes = "this is first message".getBytes("UTF-8");
                DatagramPacket request = new DatagramPacket(bytes,bytes.length);
                byte[] bytes1 = "this is second message...........".getBytes("UTF-8");
    
                DatagramPacket response = new DatagramPacket(new byte[1024],1024);
    
                datagramSocket.send(request);
                request.setData(bytes1);
                request.setLength(bytes1.length);
                datagramSocket.send(request);
                datagramSocket.receive(response);
    
                System.out.println(new String(response.getData(),0,response.getLength(),"UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    2、使用DatagramSocket 实现UDP Socket服务端,并且使用DatagramPacket封装需要接收与发送的数据

    package com.seeyon.nio.UDP;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    
    /**
     * Created by yangyu on 2017/2/23.
     */
    public class UdpServer {
    
        public static void main(String[] args) {
            try(DatagramSocket datagramSocket = new DatagramSocket(8777)) {
                DatagramPacket request = new DatagramPacket(new byte[1024],1024);
                System.out.println(request.getLength());
    
                while (true){
                    datagramSocket.receive(request);
                    System.out.println(request.getLength());
    
                    System.out.println(request.getSocketAddress());
                    System.out.println(request.getAddress()+":"+request.getPort());
                    System.out.println(new String(request.getData(),0,request.getLength(),"UTF-8"));
    
                    byte[] bytes = "你好,我是服务器".getBytes("UTF-8");
                    DatagramPacket response = new DatagramPacket(bytes,bytes.length,request.getAddress(),request.getPort());
                    datagramSocket.send(response);
    
                    /**
                     * Java网络编程(第四版)上说这里需要重设packet大小,因为接收第一个数据报以后会将DatagramPacket中buffer大小设置为第一个数据报的大小
                     *
                     * 其实并不是这样,在JDK1.8中,DatagramPacket中的length只是接收到的数据报的length,而其中还含有一个bufferLength
                     * 而bufferLength才是缓冲区大小,根据源码所得而且在接收数据报时,只是更改了DatagramPacket中的length而并没有更改
                     * bufferLength,所以并不会影响下一个数据报的接收;
                     *
                     * 所以数据报能否接收完整,跟第一个数据报大小无关,而跟DatagramPacket中buffer大小有关
                     */
                    //request.setLength(1024);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    mac下如何全量删除短信内容
    git log --oneline --graph的读法
    nginx bind() to 0.0.0.0:**** failed (13: Permission denied)
    nginx安装和配置
    pycharm的插件pylint报错:java.lang.Throwable: Write-unsafe context! Model changes are allowed from write-safe contexts only. Please ensure you're using invokeLater/invokeAndWait with a correct modality stat
    python编码规范、js编码规范及IDE的检查插件pylint/eslint等
    Flask 在 Debug 模式下初始化2次
    python的globals()使用
    删除整张表数据但是空间没有减少
    DBCC DBREINDEX重建索引提高SQL Server性能
  • 原文地址:https://www.cnblogs.com/eoss/p/6478402.html
Copyright © 2011-2022 走看看