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();
        }
    }
}

 

IT文库 » Java–NIO-UDP Socket
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址