Java学习笔记52(网络编程:UDP协议案例)

Posted xuyiqing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java学习笔记52(网络编程:UDP协议案例)相关的知识,希望对你有一定的参考价值。

InetAddress类:

表示互联网中的IP地址,示例:

技术分享图片
package demo;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressDemo {
    public static void main(String[] args) throws UnknownHostException {
        function1();
        function2();
    }

    public static void function1() throws UnknownHostException {
        InetAddress inet = InetAddress.getLocalHost();
        System.out.println(inet.getHostName());// 获取主机名
        // 输出:DESKTOP-Q3O8AEO
        System.out.println(inet.getHostAddress());// 获取主机IP
        // 输出:192.168.87.1
    }

    public static void function2() throws UnknownHostException {
        // 获得其他IP
        InetAddress inet = InetAddress.getByName("www.baidu.com");
        System.out.println(inet);
        // 输出:www.baidu.com/111.13.100.92
    }
}
View Code

 

UDP协议:无连接通信协议,最大64KB,不安全

TCP协议:面向连接通信协议,可靠安全,三次握手以确认连接,相对速度慢,适合大数据传输

 

UDP示例:

发送端:

技术分享图片
package demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSend {
    public static void main(String[] args) {
        try {
            byte[] data = "你好".getBytes();
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            DatagramPacket dp = new DatagramPacket(data, data.length, inet, 6000);
            DatagramSocket ds = new DatagramSocket();
            ds.send(dp);
            ds.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
View Code

 

接收端:

技术分享图片
package demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceive {
    public static void main(String[] args) {
        try {
            DatagramSocket ds = new DatagramSocket(6000);
            byte[] data = new byte[1024];
            DatagramPacket dp = new DatagramPacket(data, data.length);
            ds.receive(dp);
            int length = dp.getLength();
            String ip = dp.getAddress().getHostAddress();
            System.out.println(ip + ":" + new String(data, 0, length));
            ds.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
View Code

先运行接收端,再运行发送端,结果:

输出:127.0.0.1:你好

 

功能可以提升,实现键盘输入的聊天:

发送端:

技术分享图片
package demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPSend {
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            DatagramSocket ds = new DatagramSocket();
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            while (true) {
                String message = sc.nextLine();
                byte[] data = message.getBytes();
                DatagramPacket dp = new DatagramPacket(data, data.length, inet, 7000);
                ds.send(dp);
            }
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
View Code

接收端:

技术分享图片
package demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceive {
    public static void main(String[] args) {
        try {
            DatagramSocket ds = new DatagramSocket(7000);
            byte[] data = new byte[1024];
            while (true) {
                DatagramPacket dp = new DatagramPacket(data, data.length);
                ds.receive(dp);
                int length = dp.getLength();
                String ip = dp.getAddress().getHostAddress();
                System.out.println(ip + ":" + new String(data, 0, length));
            }
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
View Code

 

先后运行,在发送端发送的消息可以随时在接收端收到,这里就实现了一个简单的聊天功能

同个局域网下的多台电脑之间也可以聊天,只要修改成相应的IP地址即可

 

以上是关于Java学习笔记52(网络编程:UDP协议案例)的主要内容,如果未能解决你的问题,请参考以下文章

Socket网络编程学习笔记 UDP案例-消息接收回送与局域网广播回送

Java讲课笔记34:网络编程

JAVA Socket 编程学习笔记

Java学习笔记53(网络编程:TCP协议案例)

java网络编程

java网络编程