程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java--NIO-UDP Socket

Java--NIO-UDP Socket

編輯:關於JAVA

Java--NIO-UDP Socket。本站提示廣大學習愛好者:(Java--NIO-UDP Socket)文章只能為提供參考,不一定能成為您想要的結果。以下是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();
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved